命名管道 - write() 和 read() 问题
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中基本上至少有四个错误。创建 FIFO 时,必须使用“open”调用返回的文件描述符。但是,您将其与 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:
The correct code should look like this:
Or like this, if you insist of saving one line of code:
And exactly the same story with reading:
Correct code:
In your code that writes to FIFO there exactly the same two errors.
问题出在您的代码中:
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 asfifo = (open(myFIFO, O_WRONLY) < 0)
. You get zero because the valueopen()
returns is>= 0
.