Question

'Server' program side:

#define RESP_FIFO_NAME "response"

/* Global Variables */
char *cmdfifo = CMD_FIFO_NAME; /* Name of command FIFO. */
char *respfifo = RESP_FIFO_NAME; /* Name of response FIFO. */

int main(int argc, char* argv[]) {
int infd, outfd; /* FIFO file descriptors. */

... // blah blah other code here

/* Create command FIFO. */
if (mkfifo(cmdfifo, FIFO_MODE) == -1) {
    if (errno != EEXIST) {
        fprintf(stderr, "Server: Couldn’t create %s FIFO.\n", CMD_FIFO_NAME);
        exit(1);
    }
}

/* Create response FIFO. */
if (mkfifo(respfifo, FIFO_MODE) == -1) {
    if (errno != EEXIST) {
        fprintf(stderr, "Server: Couldn’t create %s FIFO.\n", RESP_FIFO_NAME);
        exit(1);
    }
}
/* Open the command FIFO for non-blocking reading. */
if ((infd = open(cmdfifo, O_RDONLY | O_NONBLOCK)) == -1) {
    fprintf(stderr, "Server: Failed to open %s FIFO.\n", CMD_FIFO_NAME);
    exit(1);
}

        /* Open the response FIFO for non-blocking writes. */
if ((outfd = open(respfifo, O_WRONLY | O_NONBLOCK)) == -1) {
            fprintf(stderr, "Server: Failed to open %s FIFO.\n", RESP_FIFO_NAME);
            perror(RESP_FIFO_NAME);
            exit(1);
        }

The program prints an output of:

Server: Couldn’t create response FIFO.

I understand very little on FIFOs as my professor didn't teach it. This was all I was able to manage from reading his examples and lecture notes. I tried without O_NONBLOCK flag but that just causes the program to hang, so it is required. I don't understand why the read FIFO is fine but the write FIFO fails to open.

Was it helpful?

Solution

From the man-page :

A process can open a FIFO in non-blocking mode. In this case, opening for read only will succeed even if noone has opened on the write side yet; opening for write only will fail with ENXIO (no such device or address) unless the other end has already been opened.

You should open this one in the 'client'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top