Quantcast
Channel: Is there a neat way to handle pipe (FIFO) locking in C? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Is there a neat way to handle pipe (FIFO) locking in C?

$
0
0


Hi!
I have a C assignment where I need to make a named pipe (FIFO), and let 2 child processes write lines into it while the parent process reads those lines (and processes the received data). I'm new to this topic - fd locking, pipes, etc. - and I don't really understand the basic pipeline. Now I'm working with this structure:

  • program makes the pipe (mkfifo)
  • parent makes the 2 child processes (fork)
  • children write lines into pipe simultaneously while parent reads from the pipe, both following these steps on every iteration:
    • open file descriptor
    • write/read it
    • close file descriptor

It seems to me that somehow they get tangled and the parent process does not get all lines written into the pipe (at least when I don't make the children sleep so they don't work simultaneously).
Is there a neat way to lock the pipe after a write so that the other process can't access it, but the parent can for reading?

Thanks!


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images