Jump to content
  • Advertisement
Sign in to follow this  
_meh_

unix pipe in C

This topic is 4325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently stuck on a simple program that requires unix pipe. I'm have never programmed with unix pipe before, so if anyone can point me to the right direction will be greatly appreciated! I'm suppose to write a program that the parent spawns many child processes and each of the child process are trying to decrement the same variable. This can be easily done with pthreads, since variables are shared in threads but not processes. I'm required to use the unix pipe facility and pipes only, so pthreads, sockets, shared memory are out of the question. Since I'm new to this unix pipe thing, I did some readings online. Most of the tutorials are only talking about one child process trying to communicate with one parent process using an one-way pipe. There aren't any examples that talks about multiple child processes communicating with the parent. Heck, all I need to know is how to make all these child processes access to the same variable and avoid race conditions. I did thought about using a semaphore to do the locking and a file to store the variable, however I don't know where the pipe will come in in this approach (since I'm required need to use pipes to do the program). Thanks in advance! [Edited by - _meh_ on October 16, 2006 7:07:48 PM]

Share this post


Link to post
Share on other sites
Advertisement
I got to go to sleep now, so I'll only throw a topic to read:
select - this will make the parent able to read from multiple pipes.

And I'm pretty sure that if you're not allowed to use pthreads, sockets and SHM, you're not supposed to use mutexes either - only pipes.

Share this post


Link to post
Share on other sites
In my opinion, threads and pipes are a separate issue. Having separate threads handling the IO with the children through the pipes would be acceptable. But, well, if you were told not to ... *shrug*.

Beside using select() to wait on multiple file descriptors at once, you can enable asynchronous IO or non-blocking IO (your choice) with an ioctl() call (FIOASYNC or FIONBIO, respectively).

Quote:
I'm have never programmed with unix pipe before, so if anyone can point me to the right different will be greatly appreciated!


You can think of a pipe as something like stdin and stdout, except with another program at the end rather than a console. What you write on the pipe will be read by the other program, and conversely.

Share this post


Link to post
Share on other sites
Thanks for the replies. And yeah we are not allowed to use threads. So if I were to create three child processes I should also create three pipes so that each child can communicate with their parent? If so how would that prevent race conditions? And can anyone explain the use of select() I couldn't find any nice tutorials on it. Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by _meh_
So if I were to create three child processes I should also create three pipes so that each child can communicate with their parent?


Correct.

Quote:
If so how would that prevent race conditions?


Well, if the parent isn't threaded, it will only be reading from one pipe at a time. In such a situation, there can be no race condition.

Quote:
And can anyone explain the use of select() I couldn't find any nice tutorials on it.


Look at networking tutorials. select() is commonly used with sockets. Although, of course, any kind of file descriptor will work.

When using select(), you build two (or three) sets of file descriptors, indicating which file descriptors you want to know are ready to be read from, and which are ready to be written to. Then you call select(), with an appropriate timeout parameter (how long to wait if no fd can be read from/written to), and it will update the sets accordingly.

Then you can loop over those file descriptors that have been marked as, let's say, readable, and read whatever data might be waiting for you in the pipe. Since you are looping over and considering pipes one at a time, there can be no race condition.

Share this post


Link to post
Share on other sites
Ah it makes sense now. So if I were to have a global variable "a" which all these processes need to access and update it. All I have to do in the parent is to check which process is ready to read and feed it to that process so it can update the variable and send it back to parent, then parent will send it to the next available process?

Thanks for your help!

Share this post


Link to post
Share on other sites
Awesome, thanks a lot for your help! Now I just need to make sure that a deadlock won't occur when I program the thing due to two pipes per process...

Share this post


Link to post
Share on other sites
A few things for you to consider:
* What if a child grabs the value you give it, but never sends back the updated value? Or what if it dies?
* How to prevent the nth child from inheriting the pipes for children 0..n-1?

Share this post


Link to post
Share on other sites
What are your atomic commands going to be?

Some popular ones:
atomic increment/decriment/add/subtract and return new value

atomic read

atomic compare and set (Set value to 5 if the value was 2. If the value wasn't 2, return an error)

Of the above, CAS (compare and set) is the most powerful, althought atomic add does allow for wait-free algorithms.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!