1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<wait.h>
int main(int argc,char *argv[]) { int fd[2] = {0}; int ret = pipe(fd);
if(ret == -1){ perror("pipe error"); exit(-1); } printf("pipe size [%ld]\n",fpathconf(fd[0],_PC_PIPE_BUF)); pid_t pid = fork(); if(pid == -1) { perror("fork error"); exit(-1); } if(pid > 0) { close(fd[0]); write(fd[1],"hello pipe!",sizeof("hello pipe!")); close(fd[1]); wait(NULL); } if(pid == 0){ char buf[1024]; close(fd[1]); read(fd[0],buf,1024); printf("%s\n",buf); } return 0; }
|