1

I create a named pipe with mkfifo(2) . But I want to set its size to maximum. I use fnctl() and PIPE_BUF but I think I couldn't do that.

This is my piece of code:

#define PIPE_BUF 1048576
#define _GNU_SOURCE

int writeSomeStuffToFifo (){
    int fd;

    mkfifo(myfifo, 0666); 
    fcntl(F_SETPIPE_SZ,PIPE_BUF);

    fd = open(myfifo, O_WRONLY);
    write(fd,"1",strlen("1"));
    close(fd);
}
javac
  • 441
  • 4
  • 20
  • *but I think I couldn't do that* - why do you think so? – Eugene Sh. Mar 28 '19 at 16:31
  • @EugeneSh. beacuse, it is blocked when I write something to FIFO. – javac Mar 28 '19 at 16:31
  • `fcntl` should take file descriptor. – Eugene Sh. Mar 28 '19 at 16:33
  • @EugeneSh. is second argument true? – javac Mar 28 '19 at 16:43
  • Yeah, you want `F_SETPIPE_SZ` – Eugene Sh. Mar 28 '19 at 16:45
  • @EugeneSh. I am very confused. should it be `fcntl(fd,PIPE_BUF);` or `fcntl(fd,F_SETPIPE_SZ);` ? – javac Mar 28 '19 at 16:47
  • 2
    `fcntl(fd,F_SETPIPE_SZ, PIPE_BUF )`. After `fd` is open of course. – Eugene Sh. Mar 28 '19 at 16:48
  • Does something have the other end open for reading? If not, yeah, it's going to block on the open until that's the case. – Shawn Mar 28 '19 at 18:09
  • This is the wrong approach. [You were already told how to solve the actual problem.](https://stackoverflow.com/questions/55395453/how-does-a-parent-process-read-a-fifo-after-the-child-process-finished-the-writi) Follow the advice you were given there. – Eric Postpischil Mar 28 '19 at 18:18
  • @EricPostpischil Setting the buffer size is an adequate way to solve the problem, if the amount of data is known. – user253751 Mar 28 '19 at 23:32
  • @immibis: OP is working on a school exercise (or similar tutorial) intended to teach them how to use pipes properly. A kludgy workaround will not accomplish the intended purpose of the exercise. Additionally, they are having trouble with this workaround because the parent and child are both opening the FIFO without synchronization, which is apparently interfering with setting the size. Working around the the problem in their workaround will be more trouble than just writing the correct code in the first place, which is simply for the parent to loop, repeatedly reading available data. – Eric Postpischil Mar 29 '19 at 00:10

0 Answers0