1. 程式人生 > >Pipe匿名管道

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;
}