命名管道 - write() 和 read() 问题

发布于 2024-09-13 16:29:05 字数 1376 浏览 3 评论 0原文

我正在 LINUX 下使用 C++ 进行编程。 我有两个独立的进程。我应该使用命名管道提供通信。

读者: - 使用 mkfifo 创建 FIFO - status = mkfifo(myFIFO, 0666) - 使用 open 打开管道 - fifo = open (myFIFO,O_RDONLY) -从管道读取 - num = read(fifo, temp, sizeof(temp))

编写者: - 打开管道 - fifo = open(myFIFO, O_WRONLY); -写入管道 - num = write(fifo, string, strlen(string));

我注意到文件描述符返回用于读取过程 和 write process 都是 0。另外,在命令 write 之后,我可以在终端上看到应该写入管道的字符串。我不知道为什么它显示在终端上...另外,写入的字节数是 0...

请帮帮我吗? 谢谢你!!!

// read.cpp:

#define myFIFO "/temp/FIFO"

int main(){
    int num, fifo, status;
    char temp[32];

    if (status = mkfifo(myFIFO, 0666) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

     if (fifo = open(myFIFO, O_RDONLY) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

     if (num= read(fifo, temp, sizeof(temp)) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

    printf("In FIFO is %s \n", temp);
}

在另一个文件中:

// write.cpp:

#define myFIFO "/temp/FIFO"

int main() {
    int status, num, fifo;
    char string[]="Testing...";

     if (fifo = open(myFIFO, O_WRONLY) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

     if (num= write(fifo, string, strlen(string)) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }
}

I am doing programming in C++, under LINUX.
I have two independent processes. I should provide communication using named pipe.

Reader:
-creates FIFO using mkfifo - status = mkfifo(myFIFO, 0666)
-opens the pipe using open - fifo = open (myFIFO,O_RDONLY)
-reads from the pipe - num = read(fifo, temp, sizeof(temp))

Writer:
-opens pipe - fifo = open(myFIFO, O_WRONLY);
-writes to the pipe - num = write(fifo, string, strlen(string));

I have noticed that the file descriptor returned for read process
and write process are 0. Also, after command write, I can see on my terminal, the string which should be written to the pipe. I don't know why it is shown on terminal... Also, the number of bytes that are written is 0...

Would you please help me?
Thank you!!!

// read.cpp:

#define myFIFO "/temp/FIFO"

int main(){
    int num, fifo, status;
    char temp[32];

    if (status = mkfifo(myFIFO, 0666) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

     if (fifo = open(myFIFO, O_RDONLY) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

     if (num= read(fifo, temp, sizeof(temp)) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

    printf("In FIFO is %s \n", temp);
}

And in another file:

// write.cpp:

#define myFIFO "/temp/FIFO"

int main() {
    int status, num, fifo;
    char string[]="Testing...";

     if (fifo = open(myFIFO, O_WRONLY) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }

     if (num= write(fifo, string, strlen(string)) < 0) { 
     printf("\n %s \n", strerror(errno));
     return 0;
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-09-20 16:29:05

您的代码中基本上至少有四个错误。创建 FIFO 时,必须使用“open”调用返回的文件描述符。但是,您将其与 0 进行比较,并将比较结果分配给用于保存文件描述符的变量:

if (fifo = open(myFIFO, O_RDONLY) < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

正确的代码应该如下所示:

fifo = open(myFIFO, O_RDONLY);
if (fifo < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

或者像这样,如果您坚持保存一行代码:

if ((fifo = open(myFIFO, O_RDONLY)) < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

并且与阅读完全相同的故事:

if (num= read(fifo, temp, sizeof(temp)) < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

正确的代码:

num = read(fifo, temp, sizeof(temp));
if (num < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

在写入 FIFO 的代码中存在完全相同的两个错误。

You basically have at least four errors in your code. When you create a FIFO, you have to use a file descriptor returned by "open" call. However, you are comparing it with 0 and assigning a result of a comparison to a variable that was meant to hold a file descriptor:

if (fifo = open(myFIFO, O_RDONLY) < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

The correct code should look like this:

fifo = open(myFIFO, O_RDONLY);
if (fifo < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

Or like this, if you insist of saving one line of code:

if ((fifo = open(myFIFO, O_RDONLY)) < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

And exactly the same story with reading:

if (num= read(fifo, temp, sizeof(temp)) < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

Correct code:

num = read(fifo, temp, sizeof(temp));
if (num < 0) { 
    printf("\n %s \n", strerror(errno));
    return 0;
}

In your code that writes to FIFO there exactly the same two errors.

童话 2024-09-20 16:29:05

问题出在您的代码中: fifo = open(myFIFO, O_WRONLY) < 0 的计算结果为 fifo = (open(myFIFO, O_WRONLY) < 0)。您得到零,因为 open() 返回的值是 >= 0

The problem is in your code: fifo = open(myFIFO, O_WRONLY) < 0 evaluates as fifo = (open(myFIFO, O_WRONLY) < 0). You get zero because the value open() returns is >= 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文