Thread: pipe

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    pipe

    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

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Look at what your code is doing.. my comments in red:
    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));
    		/* You have a for loop above, but inside the loop you call exit below, so it will only write one character. */
    		exit(0);
    	      
    	     }
                 /* exit(0); should be here instead */
    	 }
    
    
    	else
    	{
    		wait(NULL);
    
    		close(fd[1]);
                    /* again here, you're only reading one character, there is no loop */
    		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
    If you understand what is wrong, you should be able to correct it yourself.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    thanx for help

    the code should be

    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]);
                    /* again here, you're only reading one character, there is no loop */
    
    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;
    
    }
    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 in child process I've used a while loop but I dont know why I did not get the correct results


    thanx again for help

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Your code is logically sound, but you need to realise that read and write have no concept of start/end. Example, if you write 5 bytes, then write 7 bytes, it may as well be the same as just writing 12 bytes in one go.

    In the case of your code above, the first time the parent reads, it gets the entire buffer that the child wrote out, which is "I\0want\0\to\0print\0this\0line\0twice\0". So of course printing the output will just result in "I", since there is a null terminator straight after.

    So, one work around to this would be to convert the \0's each time into \n's. You should only need to do one read, but you should check the return value. Something like:
    Code:
    while ( (n = read(fd, buf, size)) > 0)
    {
        printf("read: %s\n", buf);
    }
    In general, you should check the return values of all system calls, even if you think they'll always work.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    this is the way I was looking for


    thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  2. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  3. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  4. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM
  5. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM