匿名管道

来源:互联网 发布:联通软件研究院怎么样 编辑:程序博客网 时间:2024/04/30 04:13
//匿名管道//实现父子进程间的进程通信//发送简单少量无格式数据#include <stdio.h>#include <unistd.h>int main(){        pid_t child;        int fd[2];        int iRead;        int iWrite;        //create pipe        pipe(fd);        //create child        child = fork();        if(child == 0)        {                 close(fd[1]);                while(1)                {                        read(fd[0],&iWrite,sizeof(iWrite));                        printf("Shu zi %d \n",iWrite);                }                close(fd[0]);                exit(0);        }        else        {                close(fd[0]);                while(1)                {                        printf("shu ru\n");                        scanf("%d",&iRead);                        write(fd[1],&iRead,sizeof(iRead));                }                close(fd[1]);        }        return 0;}

0 0