现在的位置: 首页 > 综合 > 正文

Linux环境编程–进程通信

2013年07月10日 ⁄ 综合 ⁄ 共 3242字 ⁄ 字号 评论关闭

实验内容

编写程序实现进程的管道通信。用系统调用pipe( )建立一管道,二个子进程P1和P2分别向管道各写一句话:

    Child 1 is sending a message!

    Child 2 is sending a message!

父进程从管道中读出二个来自子进程的信息并显示(要求先接收P1,后P2)。

 

实验指导

一、什么是管道

UNIX系统在OS的发展上,最重要的贡献之一便是该系统首创了管道(pipe)。这也是UNIX系统的一大特色。

所谓管道,是指能够连接一个写进程和一个读进程的、并允许它们以生产者—消费者方式进行通信的一个共享文件,又称为pipe文件。由写进程从管道的写入端(句柄1)将数据写入管道,而读进程则从管道的读出端(句柄0)读出数据。

 


句柄fd[0]

 

 

 

 

句柄fd[1]

 读出端

 

 

 

 

写入端

二、管道的类型:

1、有名管道

一个可以在文件系统中长期存在的、具有路径名的文件。用系统调用mknod( )建立。它克服无名管道使用上的局限性,可让更多的进程也能利用管道进行通信。因而其它进程可以知道它的存在,并能利用路径名来访问该文件。对有名管道的访问方式与访问其他文件一样,需先用open( )打开。

2、无名管道

一个临时文件。利用pipe( )建立起来的无名文件(无路径名)。只用该系统调用所返回的文件描述符来标识该文件,故只有调用pipe( )的进程及其子孙进程才能识别此文件描述符,才能利用该文件(管道)进行通信。当这些进程不再使用此管道时,核心收回其索引结点。

二种管道的读写方式是相同的,本文只讲无名管道。

3、pipe文件的建立

分配磁盘和内存索引结点、为读进程分配文件表项、为写进程分配文件表项、分配用户文件描述符

4、读/写进程互斥

内核为地址设置一个读指针和一个写指针,按先进先出顺序读、写。

为使读、写进程互斥地访问pipe文件,需使各进程互斥地访问pipe文件索引结点中的直接地址项。因此,每次进程在访问pipe文件前,都需检查该索引文件是否已被上锁。若是,进程便睡眠等待,否则,将其上锁,进行读/写。操作结束后解锁,并唤醒因该索引结点上锁而睡眠的进程。

三、所涉及的系统调用   

1、pipe( )

建立一无名管道。

系统调用格式

              pipe(filedes)

参数定义

int  pipe(filedes);

int  filedes[2];

其中,filedes[1]是写入端,filedes[0]是读出端。

该函数使用头文件如下:

#include <unistd.h>

#inlcude <signal.h>

#include <stdio.h>

   2、read( )

  系统调用格式

                  read(fd,buf,nbyte)

  功能:从fd所指示的文件中读出nbyte个字节的数据,并将它们送至由指针buf所指示的缓冲区中。如该文件被加锁,等待,直到锁打开为止。

  参数定义

                  int  read(fd,buf,nbyte);

                  int  fd;

                  char *buf;

                  unsigned  nbyte;

  3、write( )

系统调用格式

                  read(fd,buf,nbyte)

功能:把nbyte 个字节的数据,从buf所指向的缓冲区写到由fd所指向的文件中。如文件加锁,暂停写入,直至开锁。

参数定义同read( )。

 

 

〈任务〉

    编制一段程序,实现进程的管道通信。使用系统调用pipe()建立一条管道线。两个子进程p1和p2分别向通道个写一句话:

  child1 process is sending message!

child2 process is sending message!

而父进程则从管道中读出来自两个进程的信息,显示在屏幕上。

 

程序一:

#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char* p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));
if(pipe(pipe_fd)<0){
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{
printf("\n");
close(pipe_fd[1]);
sleep(2);
if((r_num=read(pipe_fd[0],buf_r,100))>0){
   printf("%d numbers read from the pipe is %s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid>0)
{
close(pipe_fd[0]);
if(write(pipe_fd[1],"Hello",5)!=-1)
printf("parent writel success!\n");
if(write(pipe_fd[1],"pipe",5)!=-1)
   printf("parent write2 success!\n");
close(pipe_fd[1]);
sleep(3);
waitpid(pid,NULL,0);
exit(0);
}
}

效果:

parent writel success!
parent write2 success!
10 numbers read from the pipe is Hellopipe

 

更复杂的例子,程序二:

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include<string.h>
int pid1,pid2;
 
main( )
{ 
int fd[2];
char outpipe[100],inpipe[100];
pipe(fd);                       /*创建一个管道*/
while ((pid1=fork( ))==-1);
/*lockf()函数允许将文件区域用作信号量(监视锁),或用于控制对锁定进程的访问(强制模式记录锁定)。试图访问已锁定资源的其他进程将返回错误或进入休眠状态,直到资源解除锁定为止。当关闭文件时,将释放进程的所有锁定,即使进程仍然有打开的文件。当进程终止时,将释放进程保留的所有锁定。*/
if(pid1==0)
  {
lockf(fd[1],1,0);
    sprintf(outpipe,"child 1 process is sending message!"); 
	/*把串放入数组outpipe中*/
    write(fd[1],outpipe,50);     /*向管道写长为50字节的串*/
    sleep(5);                 /*自我阻塞5秒*/
    lockf(fd[1],0,0);
    exit(0);
   }
else
  {
while((pid2=fork( ))==-1);
    if(pid2==0)
{ 
lockf(fd[1],1,0);           /*互斥*/
        sprintf(outpipe,"child 2 process is sending message!");
        write(fd[1],outpipe,50);
        sleep(5);
        lockf(fd[1],0,0);
        exit(0);
     }
     else
     {  
wait(0);              /*同步*/
         read(fd[0],inpipe,50);   /*从管道中读长为50字节的串*/
         printf("%s\n",inpipe);
         wait(0);
         read(fd[0],inpipe,50);
         printf("%s\n",inpipe);
        exit(0);
    }
  }
}

 

运行结果:

child 1 process is sending message!
child 2 process is sending message!

抱歉!评论已关闭.