-1

In this code my program crashes in when I am opening the pipe for writing.

char pipe[30];
int fd, tmp = 2;
sprintf(pipe, "root_%d", getpid());
ret_val = mkfifo(pipe, 0666);
fd = open(pipe, O_WRONLY); //HERE IS CRASHING - SUDDENLY FREEZES
write(fd, &tmp, sizeof(int));
close(fd)

All seems good, but where is my mistake;

Nick Stavr
  • 13
  • 4

1 Answers1

1

It is an expected behavior. From man 7 fifo:

Normally, opening the FIFO blocks until the other end is opened also.

So your open does not return until somebody opens the same pipe for reading. You may want to add O_NONBLOCK flag (and likely get SIGPIPE on writing), or revisit the design.

user58697
  • 7,808
  • 1
  • 14
  • 28