简体   繁体   中英

LINUX FIFO named pipe (IPC) stops writing/reading message at specific file descriptor

I'm communicating between two non-related process (not folked from the same parent) using Named Pipe (fifo mode). The program works. However, the pile file seem to be limited

The OUTPUT of the writer:

Wrote to file. fd = 3
Wrote to file. fd = 3
...... other output ...
Wrote to file. fd = 3
Wrote to file. fd = 3

The OUTPUT of the reader is:

fd: 3. Received buffer: hello
fd: 4. Received buffer: hello
...... other output ...
fd: 1021. Received buffer: hello
fd: 1022. Received buffer: hello
fd: 1023. Received buffer: hello

MY PROBLEM: At fd 1023, the reader seems to STOP READING from file (there is no fd 1024, 1025, etc.). When it happens, the writer STOP WRITING (log "Wrote to file. fd = 3" stops display)

I try different msg value ("hi", "where are you" ...), but the writer always stop at fd 1023

I really don't understand why. Could you explain for me? Thanks you so much!

Here is my code: The writer:

#include <stdio.h>
#include <stdlib.h> /* exit functions */
#include <unistd.h> /* read, write, pipe, _exit */
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char msg[] = "hello";

    /* create the FIFO (named pipe) */
    mkfifo(myfifo, 0666);

    fd = open(myfifo, O_WRONLY);
    if (fd < 0) {
        return;
    }

    while(1) {
        write(fd, msg, sizeof(msg));
        printf("Wrote to file. fd = %d\n", fd);
        sleep(1);
    }
    close(fd);

    /* remove the FIFO */
    unlink(myfifo);

    return 0;
}

The reader:

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_BUF 500

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[MAX_BUF];

    while(1) {
        /* open, read, and display the message from the FIFO */
        fd = open(myfifo, O_RDONLY);

        if (fd != -1) {
            if (read(fd, buf, sizeof(buf)) > 0) {
                printf("fd: %d. Received buffer: %s\n", fd, buf);
            }
        }
        sleep(0.2);
    }
    close(fd);

    return 0;
}

I really don't understand why. Could you explain for me?

There's a limit on the number of open file descriptors on your system. The limit can be checked in shell with ulimit -n . It is configurable. See help ulimit and man limits.conf .

On your system the maximum number of open file descriptors seems to be at 1024 (on mine too!) Opening more will return -1 with errno=EMFILE .

Your reader program opens new file descriptors in a loop. After reaching the limit of open file descriptors, open starts retuning -1 , which because of if (fd != -1) { makes the program stop outputting anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM