سؤال

PRETEND THEY'RE NOT PARENT AND CHILD PROCESSES EVEN THOUGH THEY ARE.

MAKE FIFO:

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

Fork:

/* 3. Fork the client process. */
switch (fork()) {

/* Fork failed. */
case (pid_t) -1:
    fprintf(stderr, "Call to fork failed.\n"); 
    exit(1);

/* Client (child) process. */
case 0:
    execlp(CLIENT_NAME, CLIENT_NAME, argv[SERVER_CMD_FILE_ARG], argv[SERVER_LOG_FILE_ARG], NULL);

/* Server (parent) Process */
default:
    sleep(1);
    server(infd, outfd, argv[INIT_DB_ARG], argv[FINAL_DB_ARG]);
} /* End of switch. */

server function:

int server(int infd, int outfd, char *init_db_name, char *final_db_name) {
...
if ((outfd = open(RESP_FIFO_NAME, O_WRONLY)) == -1) {
            fprintf(stderr, "Server: Failed to open %s FIFO.\n", RESP_FIFO_NAME);
            perror(RESP_FIFO_NAME);
            exit(1);
        }
...
}

client program:

printf("RESP_FIFO FILE DESCRIPTOR: %d\n", infd);
/* Open the response FIFO for reading. */
if ((infd = open(RESP_FIFO_NAME, O_RDONLY)) == -1) {
    fprintf(stderr, "Client: Failed to open %s FIFO.\n", RESP_FIFO_NAME);
    exit(1);
}
else printf("RESP_FIFO FILE DESCRIPTOR: %d\n", infd);

TL;DR The open for reading call in client program is not being executed before the open for writing call in the server program.

هل كانت مفيدة؟

المحلول 4

I wrote into a FIFO on one end without reading out of the other end. Just having the files open for reading and writing is not enough, you have to actually read the text out of the FIFO or the program will incur an error (ENXIO if you have O_NONBLOCK flag set).

نصائح أخرى

Are you opening the response fifo for writing before its other end is open for reading? See fex. Having a trouble with opening FIFO in C

Either wait until you know the FIFO is open for reading or make the open blocking, to wait for the client. Also make sure the server has write permission for the FIFO file.

What about pipe() for this

From pipe(2):

Create descriptor pair for interprocess communication.

The pipe() function creates a pipe (an object that allows unidirectional data flow) and allocates a pair of file descriptors. The first descrip- tor connects to the read end of the pipe; the second connects to the write end.

Data written to fildes1 appears on (i.e., can be read from) fildes[0].

You can look how memcached use it

Yes, without a reader, an open() of a filesystem FIFO for writing will either block or, in the nonblocking case, fail with ENXIO.

You have at least two easy options.

First, you could open the "command" FIFO, nonblocking, for reading in addition to writing, either O_RDWR or with two separate file descriptors, one O_RDONLY and one O_WRONLY.

Second, you could use a filesystem socket instead, and have the server listen there. That gives you a bi-directional communication channel over one ofile.

UNIX gives you other options, too — message queues or files or shared memory segments, perhaps using signals for one interlocutor to prod the other, come to mind — but the above are quite straightforward.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top