LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2006, 03:21 PM   #1
smart girl
LQ Newbie
 
Registered: Mar 2006
Posts: 6

Rep: Reputation: 0
pipes


hi all

I am trying to be much familier with pipes

I tried to write a c program that generates the following output

child will print the 0th character and send it to parent
a : from child .. waiting for parent to print same character
a : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
b : from child .. waiting for parent to print same character
b : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
c : from child .. waiting for parent to print same character
c : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
d : from child .. waiting for parent to print same character
d : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
e : from child .. waiting for parent to print same character
e : from parent .. waiting for next character from child


but what I get was this
child will print the 0th character and send it to parent
a : from child .. waiting for parent to print same character
a : from parent .. waiting for next character from child


and here is my code

Code:
#include<stdio.h>
#include<string.h>
int main (void)
{
	int fd[2],nbytes,i;
	int childpid;
	char  *f1[5]={"a","b","c","d","e"};
	char f2[2];

	pipe(fd);
   
        childpid=fork();
	
	if(childpid==0)//start child process
	{

	     for(i=0;i<5;i++)
	     {
	    
		printf("child will print the %dth  character and send it to parent\n",i);
		printf("%s : from child .. waiting for parent to print same character\n",f1[i]);

  		
		
		close(fd[0]);

		write(fd[1],f1[i],(strlen(f1[i])+1));
		
		exit(0);
	      
	     }
	 }


	else
	{
		wait(NULL);

		close(fd[1]);

		nbytes=read(fd[0],f2,sizeof(f2));

		printf("%s : from parent .. waiting for next character from child\n",f2);
		
	}

	return 0;

}


I need your help to correct my code and thanx
 
Old 04-27-2006, 03:50 PM   #2
geeman2.0
Member
 
Registered: Feb 2005
Location: Ontario, Canada
Distribution: Gentoo, Slackware
Posts: 345

Rep: Reputation: 30
Likely this is because you put an exit(0) inside the for loop.
 
Old 04-27-2006, 03:58 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,575

Rep: Reputation: 181Reputation: 181
The biggest problem I see in the code is that you don't get an info when parent prints a character (the child gets no notification). Pipe is one-way. You can use another one for notifications. Without the second pipe (or any other mechanism with the same result) all you can get is something like:
Code:
child will print the 0th  character and send it to parent
a : from child .. waiting for parent to print same character
child will print the 1th  character and send it to parent
b : from child .. waiting for parent to print same character
child will print the 2th  character and send it to parent
c : from child .. waiting for parent to print same character
child will print the 3th  character and send it to parent
d : from child .. waiting for parent to print same character
child will print the 4th  character and send it to parent
e : from child .. waiting for parent to print same character
a : from parent .. waiting for next character from child
b : from parent .. waiting for next character from child
c : from parent .. waiting for next character from child
d : from parent .. waiting for next character from child
e : from parent .. waiting for next character from child
I have modified your code - removed exit(), added loop in child code.
Code:
#include<stdio.h>
#include<string.h>
int main (void)
{
  int fd[2],nbytes,i;
  int childpid;
  char  *f1[5]={"a","b","c","d","e"};
  char f2[2];

  pipe(fd);

  childpid=fork();

  if(childpid==0)//start child process
  {
    close(fd[0]);
    for(i=0;i<5;i++)
    {
      printf("child will print the %dth  character and send it to parent\n",i);
      printf("%s : from child .. waiting for parent to print same character\n",f1[i]);

      write(fd[1],f1[i],(strlen(f1[i])+1));
//    exit(0);
    }
  }  else  {
    wait(NULL);
    close(fd[1]);
    for(i=0;i<5;i++)
    {
                                                                                
      nbytes=read(fd[0],f2,sizeof(f2));
                                                                                
      printf("%s : from parent .. waiting for next character from child\n",f2);
    }                                                                            
   }                                                                           
   return 0;                                                                            
}
 
Old 04-27-2006, 06:49 PM   #4
smart girl
LQ Newbie
 
Registered: Mar 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by geeman2.0
Likely this is because you put an exit(0) inside the for loop.
even if I remove it, it does not give me what I want


Mara

thanx for explanning the mechanism

I have another question

if I have the following code
Code:
#include<stdio.h>
#include<string.h>

int main(void){
	int childpid,fd[2],nb,i,j;
	char line[BUFSIZ]="I want to print this line twice";
	char word[BUFSIZ] ;
	

	pipe(fd);

	childpid=fork();

	if(childpid==0)
		{
			printf("from child:\n");
			
			close(fd[0]);

			char *token=strtok(line," ");

			 while(token!=NULL)
			 {
				 printf(" %s\n",token);

				 write(fd[1],token,(strlen(token)+1));

				 token=strtok(NULL," ");

			}
	
		}

	else
	{

		wait(NULL);
		printf("from parent:\n");
		
		

		close(fd[1]);
		
	for( i=0;i<7;i++){
		nb=read(fd[0],word,sizeof(word));
		printf("%s\n",word);
	
	}

	}

	return 0;

}
I want the output to be:

from child:
I
want
to
print
this
line
twice

from parent:
I
want
to
print
this
line
twice


what I get is
from child:
I
want
to
print
this
line
twice

from parent:
I
I
I
I
I
I
I



I think I am doing as you said, but instead of the first for loop I've used a while loop but I dont know why I did not get the correct results


thanx again for help

Last edited by smart girl; 04-27-2006 at 06:51 PM.
 
Old 04-28-2006, 03:19 AM   #5
smart girl
LQ Newbie
 
Registered: Mar 2006
Posts: 6

Original Poster
Rep: Reputation: 0
any one help
 
Old 04-28-2006, 08:25 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
man strtok

strok splits a string up, and places a NULL \0 where the spaces are.
Hence when you print it you only see the I. The string STOPS PRINTING with the \0.

use strchr(line, ' ') instead.

if you are playing it may be easier to use printf() or puts() and gets() for the pipe.

I can't see why you are splitting the string.
strtok is a dangerous function if you don't know what you are doing
 
Old 04-30-2006, 11:20 AM   #7
smart girl
LQ Newbie
 
Registered: Mar 2006
Posts: 6

Original Poster
Rep: Reputation: 0
It is ok for me to use strtok since I am dealing with rubbish data

I found a solution for that problem


thanx for your reply
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pipes in python? SciYro Programming 9 08-02-2006 10:35 AM
about pipes kpachopoulos Programming 1 10-15-2005 01:37 PM
Question regarding pipes alltime Programming 3 04-15-2005 08:54 PM
devices and pipes siemens Linux - General 1 03-23-2005 04:30 AM
pipes on Suse AdamTheFirst Linux - Software 1 11-04-2004 03:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration