【发布时间】:2013-05-06 05:19:09
【问题描述】:
“服务器”程序端:
#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);
}
程序打印输出:
Server: Couldn’t create response FIFO.
我对 FIFO 知之甚少,因为我的教授没有教它。这就是我通过阅读他的例子和讲义所能做到的。我试过没有 O_NONBLOCK 标志,但这只会导致程序挂起,所以它是必需的。不明白为什么读FIFO没问题,写FIFO打不开。
【问题讨论】:
-
您应该在错误消息中报告
mkfifo()调用中使用的名称——所以respfifo而不是RESP_FIFO_NAME。当前目录(程序运行时的)中存在哪些 FIFO?您确定要在 FIFO 名称中包含尖括号(还是来自不同版本代码的错误消息)?您可以在输出中包含errno和strerror(errno);这将有助于诊断问题所在(EEXIST vs EPERM vs ...)。 -
使用
perror或strerror了解确切的错误。 -
RESP_FIFO_NAME是一个预处理器#define'd 宏常量。我现在补充一下。我认为这并不重要,因为它只是一个带有直接文本替换的字符串文字。是的,我现在就用 perror 看看实际的问题是什么。 -
perror(RESP_FIFO_NAME)输出<RESP_FIFO_NAME>: No such device or address显然是ENXIO -
有趣:在 Mac OS X 上,ENODEV 未列为来自
mkfifo(2)的可能错误。您是否执行过任何chdir()操作?我看到删除尖括号不会改变结果。FIFO_MODE是什么?命令FIFO是否已经存在?
标签: c unix file-io fifo mkfifo