首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Linux中的断管(IPC)

Linux中的断管(IPC)
EN

Stack Overflow用户
提问于 2013-05-29 05:30:44
回答 1查看 1.3K关注 0票数 1

我已经为pipe编写了一个基本的示例程序(linux ),但我将broken pipe作为输出。

以下是代码:

代码语言:javascript
运行
复制
#include<stdio.h> 

#include<unistd.h> 

#include<sys/types.h> 

#include"iostream" 

using namespace std; 

int main() 
{ 
        int fd[2],n; 
        char arr[50] = "Sample program"; 
        char buf[50] = {0}; 

        if (0 ==  pipe(fd)) 
        { 
                cout<<"Pipe created with fd[0] - "<<fd[0]<<" and fd[1] - "<<fd[1]<<endl; 
        } 

        int pid; 

        if (pid = fork() == -1) 
        { 
                cout<<"Error in FORK"<<endl; 
                exit(1); 
        } 

        if (pid == 0) 
        { 
                cout<<"In Child Process"<<endl; 

                close(fd[0]); 

                write(fd[1], arr, sizeof(arr)); 

                exit(0); 
        } 
    else{ 

                cout<<"In Parent Process"<<endl; 
                close(fd[1]); 

                n = read(fd[0], buf, sizeof(buf)); 

                cout<<"Total bytes read is : "<<n<<endl<<"Buffer is : "<<buf<<endl; 
        }         

        return 0; 
} 

编译:

c++ pipe.cpp -g -o pipe

输出:

使用fd-3和fd1 -4创建的管道。

在儿童过程中

在儿童过程中

断管

如何解决这个问题,或者我做了什么错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-29 05:46:45

你的情况才是问题所在。如果((pid =()) == -1),则改为

这个程序运行得很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16806160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档