Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ linux中管道间的通信_C++_Linux_Pipe - Fatal编程技术网

C++ linux中管道间的通信

C++ linux中管道间的通信,c++,linux,pipe,C++,Linux,Pipe,我有两个函数writer()和reader()。我正在从writer()函数将消息写入管道,并从reader()函数读取消息。我面临的问题是,信息被写在管道中,但没有被读取。打开管道阅读可能有问题。代码是: #include<iostream> #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl

我有两个函数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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//编辑
int fifo=mkfifo(“/tmp/mypipe”,S|IWUSR | S|irsr | S|IRGRP | S|IROTH);
空写器()
{
char message[]=“这是一条消息”;
int-fd;
fd=打开(“管道”,仅限O_wr);
写入(fd、消息、sizeof(消息));

cout这是关于命名管道在posix中的工作方式。如果已经有人在读取它,则只能在其中写入。如果没有,则会阻止write()操作,直到有人不读取为止

最简单的解决方案是,如果

  • 您使用了非阻塞I/O
  • 您在不同的进程(线程)中实现了读卡器和写卡器,并在写卡器之前调用了读卡器

  • 我猜您没有以正确的方式创建管道。请查看。对于umask值,请查看

    类似于
    mkfifo(“/tmp/pipe”,0666)
    ,然后在读写器中打开
    /tmp/pipe

    另请看以下内容:

    内核只为每个FIFO特殊类型维护一个管道对象 至少由一个进程打开的文件。必须打开FIFO 在传输数据之前,在两端(读和写)。 通常,打开FIFO块,直到另一端也打开

    现在的问题是,
    open(…,O_WRONLY)
    阻塞,直到读取器打开文件

    要进行测试,只让读取器运行,然后使用
    echo“test”>/tmp/pipe

    更新:

    或者使用线程,我只是尝试了一下

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

    您还必须
    #包括
    ,添加此编译器标志:
    -std=c++0x
    ,并将以下库添加到链接器:
    -lpthread
    请确保检查函数调用的返回,因为它们可以告诉您问题所在

    包括错误编号h:

    #include <errno.h>
    #include <string.h>
    
    #包括
    #包括
    
    当您的写或读打开尝试返回错误时,请检查errno:

    fd = open("pipe" , O_WRONLY);
    if (fd < 0)
    {   
        cout << "writer open failed: " << errno << "(" << strerror(errno) << ")\n";
        /* exit */
    }   
    
    fd=打开(“管道”,仅限O_wr);
    如果(fd<0)
    {   
    
    我可以创建fifo,但现在终端上没有输出。这就是我创建fifo的方式:int res=mkfifo(“/tmp/mypipe”,0666);“在调用writer/reader之前,在main方法中创建fifo。尝试在main()中创建fifo”,给出输出:消息已写入:这是一条消息消息消息是:它工作了,很抱歉,在main()中创建fifo后,我没有在读写器函数中更改fifo的名称,现在已更改,它工作得非常好。感谢您的帮助