《基于Linux的C编程与内核导读》连载(11)
时间:2007-05-07 来源:gaowp
本节我们学习利用管道实现进程间的通信。这中间用到一个关键的系统调用pipe()函数。其格式为:
int fd[2]; pipe(fd); |
其中,fd[2]是供进程使用的文件描述符数组,fd[0]用于写,fd[1]用于读。为了协调双方的通信,管道通信机制必须提供以下三方面的协调能力:
1、互斥。当一个进程正在对pipe进行读/写操作时,另外一个进程必须等待,程序中使用lockf(fd[1],1,0)函数实现对管道的加锁操作,用lockf(fd[1],0,0)解除管道的锁定。
2、同步。当写进程把一定数量的数据写入pipe后,便去睡眠等待,直到读进程取走数据后,再把它唤醒。当读进程试图从一空管道读取数据时,也应睡眠等待,直至写进程将数据写入管道后,才将其唤醒。
3、判断对方是否存在。只有确定写进程和读进程都存在的情况下,才能通过管道进行通信。
下面我们就具体编写一个进程间管道通信的例子。
程序3.3如下:
/*************本程序用于进程间的管道通信*****************/ #include #include #include int pid1,pid2; main() { int fd[2]; char OutPipe[100],InPipe[100]; pipe(fd); /*创建管道 */ while((pid1 = fork()) == -1); if(pid1 == 0) /*子进程1执行管道写操作 */ { lockf(fd[1],1,0); /*锁定管道 */ /*****给OutPipe数组赋值******/ sprintf(OutPipe,"Child process 1 is sending message!\n"); write(fd[1],OutPipe,50); /*向管道中写入数据 */ sleep(5); lockf(fd[1],0,0); /*管道解锁*/ exit(0); } else { while((pid2 = fork()) == -1); if(pid2 == 0) /*子进程2执行管道写操作 */ { lockf(fd[1],1,0); /*锁定管道 */ sprintf(OutPipe,"Child process 2 is sending message!\n"); write(fd[1],OutPipe,50); /*向管道中写入数据 */ sleep(5); lockf(fd[1],0,0); /*管道解锁*/ exit(0); } else /*父进程执行管道读操作*/ { wait(0); /*等待子进程1结束*/ read(fd[0],InPipe,50); /*从管道中读出数据*/ printf("%s\n",InPipe); wait(0); /*等待子进程2结束*/ read(fd[0],InPipe,50); /*从管道中读出数据*/ printf("%s\n",InPipe); exit(0); } } } |
结果分析:
子进程1将字符串“Child process 1 is sending message!”写入管道;子进程2将字符串“Child process 2 is sending message!”写入管道。父进程分别使用wait(0)函数来等待子进程执行结束,然后从管道中读出数据,并分别在屏幕上显示出来。所以此程序执行的结果是:
Child process 1 is sending message! Child process 2 is sending message! |