打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
匿名管道
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<wait.h>

#define MAX_LINE 80
void main()
{
    int MyPipe[2],ret;
    char buf[MAX_LINE + 1];
    const char *testbuf = "test string!";
    if( pipe ( MyPipe ) == 0 ){
       if( fork() == 0){
         close(MyPipe[1]);
         sleep(2);
         ret = read( MyPipe[0] , buf , MAX_LINE );
         buf[ret] = 0;
         printf("Child read %s \n", buf);
         printf("子进程读取数据成功!\n");
         close(MyPipe[1]);
         exit(1);
       }   
    else
         if( fork() == 0){
         close(MyPipe[0]);
         ret = write( MyPipe[1] , testbuf , strlen(testbuf) );
         printf("父进程写管道成功!\n");
         close(MyPipe[1]);
         printf("父进程关闭写管道成功!\n");
         sleep(2);   
       }   
    }
    return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Linux之匿名管道
进程间通信之管道(pipe、fifo)
多进程编程通信——管道
linux execlp
C语言进程间通信(三)
linux下多进程写入文件的原子性
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服