linux中的管道之间的通信 [英] Communication between pipes in linux

查看:131
本文介绍了linux中的管道之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个函数 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 [] =这是一条消息;
int fd;
fd = open(pipe,O_WRONLY);
write(fd,message,sizeof(message));
cout<<message wrote:<< message<<\\\
; //给出输出
} //消息写道:这是一个消息

void reader()
{
char buffer [100];
int fd;
fd = open(pipe,O_RDONLY);
read(fd,buffer,100);
cout<<message is:<< buffer<<\\\
; //给出输出
} //消息是:

int main()
{
writer();
reader();
return 0;
}

我已经调试它,我认为问题是,fifo不是正确创建。我不知道如何解决这个。需要更多帮助。



/ p>

感谢您的帮助。

解决方案

没有以正确的方式创建管道。请查看 mkfifo手册页。有关umask值,请参阅 umask手册页



mkfifo(/ tmp / pipe,0666) > / tmp / pipe 。



还要看看 fifo手册页


内核为每个由至少一个进程打开的FIFO特殊
文件保持一个管道对象。 FIFO必须在两端(读和写)打开
,然后才能传递数据。
通常,打开FIFO块直到另一端打开。


现在你的问题是, code> open(...,O_WRONLY)阻塞,直到读者打开文件。



读取器运行,然后使用 echotest> / tmp / pipe



更新:



或使用主题, / p>

  int main(){
mkfifo(fifo_name.c_str(),0666);
std :: thread w(writer);
std :: thread r(reader);
w.join();
r.join();
unlink(fifo_name.c_str());
return 0;
}

您还必须 #include< thread> ,添加此编译器标志: -std = c ++ 0x 并将以下库添加到链接器: -lpthread


I have two functions writer() and reader(). I am writing message into the pipe from writer() function and reading it from the reader() function. The problem I am facing is that message is being written in the pipe, but it's not getting read. Maybe there is problem in opening pipe for reading. The code is:

#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;
}

I've debugged it, and I think the problem is, fifo is not being created correctly. I don't know how to resolve this. Need more help.

Thank you for any help.

解决方案

My guess is that you have not created the pipe in a correct way. Take a look at the mkfifo man page. For the umask value take a look at the umask man page.

something like mkfifo("/tmp/pipe", 0666), before you open /tmp/pipe in the reader/writer.

Also take a look at the fifo man page:

The kernel maintains exactly one pipe object for each FIFO special file that is opened by at least one process. The FIFO must be opened on both ends (reading and writing) before data can be passed. Normally, opening the FIFO blocks until the other end is opened also.

So your problem now is, that the open(..., O_WRONLY) blocks until the reader opens the file.

To try it out, let just the reader run and then use echo "test" > /tmp/pipe.

Update:

Or use threads, i just tried it out.

int main() {
    mkfifo(fifo_name.c_str(), 0666);
    std::thread w(writer);     
    std::thread r(reader); 
    w.join();
    r.join();
    unlink(fifo_name.c_str());
    return 0;
}

you also have to #include <thread>, add this compiler flag: -std=c++0x and add the following library to the linker: -lpthread.

这篇关于linux中的管道之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆