RixinHuang
全部博文(11)
IPC(3)
process(0)
2011年(1)
2010年(5)
2009年(5)
格伯纳
talent87
zhang815
king_of_
分类: LINUX
2010-01-03 22:37:01
/* pipe02.c */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <limits.h> int main (void) { int fd[2]; int fdin; char read_pipe_buf[PIPE_BUF]; char read_file_buf[PIPE_BUF]; pid_t pid; int read_pipe_len; int read_file_len; /* creat a pipe */ if ((pipe(fd)) < 0){ perror("pipe error"); exit(1); } pid = fork(); if (pid < 0){ perror("fork error"); exit(1); } /* child */ if (0 == pid){ close(fd[1]); /* close wite pipe */ while((read_pipe_len = read(fd[0], read_pipe_buf, PIPE_BUF)) > 0){ read_pipe_buf[read_pipe_len] = '\0'; printf("read %d bytes\n", read_pipe_len); printf("%s\n", read_pipe_buf); } close(fd[0]); } /* parent */ if (pid > 0){ close(fd[0]); fdin = open("pipe01.c", O_RDONLY); if (fdin < 0){ perror("open error"); exit(1); } } read_file_len = read(fdin, read_file_buf, PIPE_BUF); if (read_file_len > 0){ write(fd[1], read_file_buf, read_file_len); } close(fdin); close(fd[1]); waitpid(pid, NULL, 0); /* wait for child */ exit(0); }
上一篇:VMware6.5 + ubuntu8.04 构建嵌入式linux开发环境
下一篇:pipe02
登录 注册