exebook
exebook

Reputation: 33890

Need to close linux FIFO on the read side after each message

Here is what I did, and it works:

"server" side (reader) pseudocode

mkfifo()
while (true){
    open()
    select(...NULL)
    while (select... timeval(0)) {
        read()
    }
    close()
}

"client" side (writer) real C code

int fd;
char * myfifo = "/tmp/saveterm.fifo";
char *msg = "MESSAGE";
fd = open(myfifo, O_WRONLY);
write(fd, msg, strlen(msg));
close(fd);

Now you see I need to open/close the fifo on the server after each event read. Is this supposed to be so? At first I only opened it once before the loop, but after the first message, the 'select' call would never block again until closed and reopened, and was always returning 1 and ISSET for the descriptor.

Upvotes: 0

Views: 1702

Answers (1)

that other guy
that other guy

Reputation: 123410

Yes, it's supposed to be like this.

Named pipes behave the same way as anonymous pipes: they both represent a single stream that is terminated when the last writer closes it. Specifically, the reader is not meant to hang forever just in case some future program decides to open the pipe and continue writing.

If you want packet based communication through a file, how about using a Unix socket in datagram mode instead?

Upvotes: 1

Related Questions