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> I need your help to correct my code and thanx ;) |
Likely this is because you put an exit(0) inside the for loop.
|
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 Code:
#include<stdio.h> |
Quote:
Mara thanx for explanning the mechanism I have another question if I have the following code Code:
#include<stdio.h> from child: I want to this line twice from parent: I want to this line twice what I get is from child: I want to 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 |
any one help
|
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 ;) |
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. |