【发布时间】:2014-03-28 13:54:35
【问题描述】:
我有两个函数 writer() 和 reader()。我正在从 writer() 函数将消息写入管道并从 reader() 函数读取它。我面临的问题是消息正在写入管道中,但没有被读取。可能打开管道读取有问题。代码是:
#include<iostream>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
using namespace std;
//edit
int fifo = mkfifo("/tmp/mypipe", S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
void writer()
{
char message[] = "this is a message";
int fd;
fd = open("pipe" , O_WRONLY);
write(fd , message , sizeof(message));
cout<<"message wrote: "<<message<<"\n"; // gives output
} // message wrote: this is a message
void reader()
{
char buffer[100];
int fd;
fd = open("pipe" , O_RDONLY);
read(fd , buffer , 100);
cout<<"message is: "<<buffer<<"\n"; // gives output
} // message is:
int main()
{
writer();
reader();
return 0;
}
我已经调试过了,我认为问题是,fifo 没有被正确创建。我不知道如何解决这个问题。需要更多帮助。
感谢您的帮助。
【问题讨论】:
-
在调用写入器/读取器之前,在 main 方法中创建 fifo。
-
尝试在 main() 中创建 fifo,给出输出:消息写道:这是一条消息消息是:
-
成功了,抱歉,在 main() 中创建 fifo 后,我没有在读写器函数中更改 fifo 的名称,现在改了,效果很好。谢谢你的帮助