4

I'm having a trouble in opening FIFOs in C.. first I created them using mkfifo() function with permission : 0777, and when I tried to open them, it succeeded in opening the first FIFO only, then the process will stuck in opening the second FIFO, and this is my code :

fd1 = open("FIFO1_PATH", O_WRONLY );
fd2 = open("FIFO2_PATH", O_WRONLY );

This will not be executed, but once I comment the second line, it executes ! Is there a limit for the number of opening FIFOs per process ? I don't know why is this happening.. I just spent 3 Hours trying to figure out what the problem is, but without any results :(

tshepang
  • 12,111
  • 21
  • 91
  • 136
ObjProg
  • 429
  • 1
  • 7
  • 19
  • 1
    LOL .. I GOT the problem :D .. I was supposed to open them with "O_NONBLOCK" option, because it will block in the first open() forever (if we suppose no other process will open for reading) .. Thanks – ObjProg Dec 30 '10 at 10:47
  • @user Good for you! Please post your solution as an answer. – moinudin Dec 30 '10 at 10:49
  • I Got another problem :(, the open will execute but will return -1.. – ObjProg Dec 30 '10 at 10:53
  • if stuff returns -1, inspect errno (or e.g. call perror() ) to learn why the thing failed – nos Dec 30 '10 at 11:03
  • 1
    Note that you never want to execute a FIFO, so there is no good reason to set the 'x' bit on the permissions; use 666 permission if you must; worry about whether all the world and his dog should be able to write to your FIFO (sometimes the answer is yes, they should; sometimes, it is not a good idea). – Jonathan Leffler Jan 01 '11 at 00:01

2 Answers2

5

I just got it right :)

I have to let the opening process wait until some other process opens the FIFO for reading (and it will be a blocked reading).. by doing sleep on the writing process I will ensure that the other process will opens for reading ..

shaveenk
  • 1,983
  • 7
  • 25
  • 37
ObjProg
  • 429
  • 1
  • 7
  • 19
0

To answer you question about limits - default in Linux is 1024 file descriptors for a process. Your problem is probably not opening the second FIFO for reading so open for writing blocks.

Marek Sapota
  • 20,103
  • 3
  • 34
  • 47