FIFO带有并发进程的问题

编程入门 行业动态 更新时间:2024-10-26 10:34:23
本文介绍了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带有并发进程的问题

本文发布于:2023-10-28 00:44:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   FIFO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!