Linux多任务多线程编程-试编.ppt

* 从父进程到子进程的管道 * 对管道的操作 如果要直接存取管道,可以使用和低级的文件I / O同样的系统调用,因为在系统内核中管道实际上是由一个有效的索引节点表示的。 如果希望向管道中发送数据,可以使用系统调用write( ),反之,如果希望从管道中读取数据,可以使用系统调用read( )。 * 创建管道 pipe 如果要使用C语言创建一个简单的管道,可以使用系统调用pipe( )。 #include unistd.h 原型:int pipe( int fd[2] ); 其中fd[0]为读打开,fd[1]为写打开; 返回值:如果系统调用成功,返回0 如果系统调用失败返回- 1 * 管道的通信实现 #include sys/types.h #include unistd.h #include stdio.h #include stdlib.h #include string.h int main() { int fd[2]; pid_t pid; char buf[64] = Im parent process!\n; // 父进程要写入管道的信息 char line[64]; if (0!=pipe(fd)) { // 创建管道并检查结果 fprintf(stderr, Fail to create pipe!\n);

文档评论(0)

1亿VIP精品文档

相关文档