FIFO带有并发进程的问题 [英] FIFO Issue with concurrent processes

查看:334
本文介绍了FIFO带有并发进程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MAKE FIFO:

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. */

服务器功能:

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);
        }
...
}

客户端程序: / p>

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 客户端程序中的读取调用未打开

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

推荐答案

我在一端写入一个FIFO而不读出服务器程序中的另一端。只是让文件打开进行读取和写入是不够的,你必须实际读取FIFO中的文本或程序将引发错误(ENXIO,如果你有O_NONBLOCK标志设置)。

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).

这篇关于FIFO带有并发进程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆