Pipe匿名管道

#include <stdio.h>
#include <unistd.h>
int main(int argc,char **argv)
{
        int fds[2],num=0,n;/*fds读数据fds[0],写数据fds[1]*/
        pid_t pid;
        pipe(fds);
        pid = fork();/*创建子进程*/
        srand(time(NULL));
        if(pid == 0){
                close(fds[1]);
                read(fds[0],&num,4);
                printf("num = %d\n",num);
        }
        if(pid > 0){
                close(fds[0]);
                n = rand()%100;
                write(fds[1],&n,4);
                wait(NULL);
                return 0;
        }
        return 0;
}

猜你喜欢

转载自blog.csdn.net/hutiewei2008/article/details/85002241