c - How to check EOF when read() on FIFO

2019-08-12 13:25发布

问题:

In a client-server program, need check EOF for read() on a FIFO?

Questions:

  • Does EOF in FIFO return 0, or -1 with errno set?
  • Does the rule also apply to other IPC facilities?

@Update

I still found the result wield, so need to continue ask about it.


Following are the source code:

cs_fifo.h:

// fifo header
#ifndef _CS_FIFO
#define _CS_FIFO

#define CLIENT_DATA_SIZE 2
#define SERVER_DATA_SIZE 10

#define SERVER_FIFO_PATH "/tmp/server_fifo"

#define CLIENT_COUNT 3

#endif

fifo_server.c:

// client - server fifo, server part,
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "cs_fifo.h"

int fifo_server() {
    int flag;
    int fd;

    char buf[CLIENT_DATA_SIZE];

    // remove fifo, before create
    remove(SERVER_FIFO_PATH);

    // create fifo
    mode_t mode = 0644;
    if((flag = mkfifo(SERVER_FIFO_PATH, mode)) == -1) {
        printf("error while mkfifo(): %s\n", strerror(errno));
        return -1;
    }
    printf("server fifo created, path: %s\n", SERVER_FIFO_PATH);

    // open for read
    if((fd = open(SERVER_FIFO_PATH, O_RDONLY)) == -1) {
        printf("error while open(): %s\n", strerror(errno));
        exit(-1);
    }

    // loop to receive data from client,
    while(1) {
        // read from fifo
        if((flag = read(fd, buf, CLIENT_DATA_SIZE)) == -1) {
            printf("error while read(): %s\n", strerror(errno));
            exit(0);
        } else if(flag == 0) { // no data
            printf("no data\n");
            sleep(1);
            continue;
        }
        // data received,
        printf("receive data: %s\n", buf);

        // send data back to client's fifo,
        // TODO
    }

    // remove fifo, after finish using,
    remove(SERVER_FIFO_PATH);

    return 0;
}

int main(int argc, char * argv[]) {
    return fifo_server();
}

fifo_client.c:

// client - server fifo, client pool part,
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "cs_fifo.h"

int fifo_client_pool() {
    int flag;
    int server_fd;

    char data[CLIENT_DATA_SIZE];

    int i = 0;
    pid_t cpid;
    char identity;

    // open for write
    if((server_fd= open(SERVER_FIFO_PATH, O_WRONLY)) == -1) {
        printf("error while open(): %s\n", strerror(errno));
        exit(-1);
    }

    // create child processes as clients,
    while(i < CLIENT_COUNT) {
        switch(cpid=fork()) {
            case -1: // failed
                printf("error while fork(): %s\n", strerror(errno));
                exit(errno);
            case 0: // success, child process goes here
                printf("child process created, pid [%d], parent pid [%d]\n",(int)getpid(), (int)getppid());
                identity = i + 65; // start from 'A'

                // prepare data
                data[0] = identity;
                data[1] = '\0';

                // write to fifo
                if((flag = write(server_fd, data, CLIENT_DATA_SIZE)) == -1) {
                    printf("[%c] error while write(): %s\n", identity, strerror(errno));
                    _exit(-1);
                }
                printf("[%c] send data to server\n", identity);

                _exit(0);

                break;
            default: // success, parent process goes here
                // sleep a while,
                sleep(1);
                break;
        }

        i++;
    }

    if((flag = close(server_fd)) != 0) {
        printf("error while close(): %s\n", strerror(errno));
    }

    return 0;
}

int main(int argc, char * argv[]) {
    return fifo_client_pool();
}

Compile:

Server: gcc -Wall fifo_server.c -o server

Client: gcc -Wall fifo_client_pool.c -o client_pool

Execute:

First start server: ./server

Then start client pool: ./client_pool

Result:

  • Server start, and blocks before client start.

  • Then client start, and server receive 1 request from each of the 3 clients, that's 3 in total.

  • Then all client processes terminated, then server's read() continue return 0 without blocked.


The future question is:

  • After all clients terminated, shouldn't server's read() block? Since it's in blocking mode?

回答1:

All reads from a descriptor where read returns zero means "closed" or "end".

If you have a blocking descriptor (the default) then read will block if there's currently nothing to read. If the descriptor is non-blocking then read returns -1 with errno set to EAGAIN or EWOULDBLOCK if there's nothing to read.



标签: c linux ipc eof fifo