LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   pipes (https://www.linuxquestions.org/questions/programming-9/pipes-439432/)

smart girl 04-27-2006 02:21 PM

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 ;)

geeman2.0 04-27-2006 02:50 PM

Likely this is because you put an exit(0) inside the for loop.

Mara 04-27-2006 02:58 PM

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;                                                                           
}


smart girl 04-27-2006 05:49 PM

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 :confused:


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 :confused: :confused:


thanx again for help

smart girl 04-28-2006 02:19 AM

any one help

bigearsbilly 04-28-2006 07:25 AM

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 ;)

smart girl 04-30-2006 10:20 AM

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


All times are GMT -5. The time now is 01:56 PM.