Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10657106
  • 博文数量: 2905
  • 博客积分: 20098
  • 博客等级: 上将
  • 技术积分: 36298
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-23 05:00
文章存档

2012年(1)

2011年(3)

2009年(2901)

分类: LINUX

2009-04-11 01:33:12

文件锁、信号等进程通信方法无法实现在进程见传递大量数据,如果使用文件锁,进程见信息的传递将受到磁盘读取速度的限制。管道作为最早的进程间通信机制,实现了父子进程或者兄弟进程之间的数据读写。
 
Linux系统中的管道都是半双工的,所以要实现双工通信就需要创建2个管道来完成了;某些UNIX版本的管道是全双工的。由于管道只能在父子进程或者兄弟进程之间创建,非父子、兄弟进程之间的通信可以用命名管道或者消息队列了。
 
1、pipe函数与进程通信
调用格式int pipe(int filedes[2]);
函数产生一对指向管道inode节点的文件描述符,放在数组filedes中,filedes[0]用于读取管道数据,filedes[1]用于向管道写入数据;
函数执行成功返回0,失败返回-1。
 
【程序实例】
父子进程通信:子进程将命令行参数字符串写入管道,父进程读出。
#include
#include
#include
#include
#define BUFFER_SIZE 100
int main(int argc,char* argv[])
{
 int f_des[2];  //f_des[0]:for read, f_des[1]:for write.
 pid_t pid;
 char msg[BUFFER_SIZE];
 if(argc != 2)
 {
  printf("Usage: %s message.\n",argv[0]);
  return 1;
 }
 if(pipe(f_des)==-1)
 {
  printf("Can't creat pipe.\n");
  return 1;
 }
 pid = fork();
 if(pid<0)
 {
  perror("Fail to creat child process.\n");
  return 1;
 }
 else if(pid==0)
 {
  //child process,read data
  //关闭不需要的文件描述符,避免在读取数据无法获得结束位置。
  close(f_des[1]);
  if(read(f_des[0],msg,BUFFER_SIZE)==-1)
  {
   printf("Fail to read message from pipe.\n");
   return 1;
  }
  else
  {
   sleep(1);   
   printf("Child process received message %s.\n",msg);
  }
  _exit(0);
 }
 else
 {
  //parent process,write data  
  close(f_des[0]);
  if(write(f_des[1],argv[1],strlen(argv[1]))==-1)
  {
   printf("Fail to write data to pipe.\n");
   return 1;
  }
  else
  {
   printf("father process send message %s.\n",argv[1]);
  }
  wait(NULL);
  _exit(0);
 }
 return 0;
}
【执行结果】
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ gcc -o pipe1 systemcall2.c
gaolu@gaolu-desktop:~$ ./pipe1 Hello
father process send message Hello.
Child process received message Hello.
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
 
2、popen函数和pclose函数
popen()函数很大程度上简化了管道的使用,不需要像使用pipe那样创建管道、创建子进程甚至有时做重定向等操作。该函数的实际过程就是调用pipe()创建单向通信的管道,再调用fork()创建子进程,启动shell执行command命令。
函数调用格式如下:FILE* popen(const char*command,const char *type);
参数tpye可以为"r"或者"w",表示读取管道数据,或者向管道写数据:如果参数为"r",表示子进程写、父进程读,从子进程的标准输出中读取数据到文件流stream中;参数为"w",表示父进程写、子进程读,将文件流中的数据写入子进程的标准输入。
函数成功返回指向FILE的指针,失败返回NULL(中间pipe或者fork等某个步骤失败);
 
【代码实例】
创建管道,参数为"w",即向管道写入数据,启动shell执行sort 命令对字符进行排序。
#include
#include
int main()
{
 FILE* fs = popen("sort","w");
 if(NULL == fs)
 {
  perror("Creat pipe failed.\n");
  return 1;
 }
 fprintf(fs,"Linux.\n");
 fprintf(fs,"Hello.\n");
 fprintf(fs,"Test program.\n");
 fprintf(fs,"Bash shell.\n");
 
 pclose(fs);
 return 0;
}
 
【执行结果】
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ gcc -o pipe2 systemcall2.c
gaolu@gaolu-desktop:~$ ./pipe2
Bash shell.
Hello.
Linux.
Test program.
gaolu@gaolu-desktop:~$

http://keren.blog.51cto.com/720558/144929

阅读(826) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~