匿名管道

//匿名管道
//实现父子进程间的进程通信
//发送简单少量无格式数据
#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;
}

你可能感兴趣的:(匿名管道)