Linux进程间通信――使用匿名管道
睿丰德科技 专注RFID识别技术和条码识别技术与管理软件的集成项目。质量追溯系统、MES系统、金蝶与条码系统对接、用友与条码系统对接
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值。这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据。
一、什么是管道
如果你使用过Linux的命令,那么对于管道这个名词你一定不会感觉到陌生,因为我们通常通过符号“|"来使用管道,但是管理的真正定义是什么呢?管道是一个进程连接数据流到另一个进程的通道,它通常是用作把一个进程的输出通过管道连接到另一个进程的输入。
举个例子,在shell中输入命令:ls -l | grep string,我们知道ls命令(其实也是一个进程)会把当前目录中的文件都列出来,但是它不会直接输出,而是把本来要输出到屏幕上的数据通过管道输出到grep这个进程中,作为grep这个进程的输入,然后这个进程对输入的信息进行筛选,把存在string的信息的字符串(以行为单位)打印在屏幕上。
二、使用popen函数
1、popen函数和pclose函数介绍
有静就有动,有开就有关,与此相同,与popen函数相对应的函数是pclose函数,它们的原型如下:
[cpp] view plaincopyprint?
- #include <stdio.h>
- FILE* popen (const char *command, const char *open_mode);
- int pclose(FILE *stream_to_close);
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- FILE *read_fp = NULL;
- FILE *write_fp = NULL;
- char buffer[BUFSIZ + 1];
- int chars_read = 0;
- //初始化缓冲区
- memset(buffer, '\0', sizeof(buffer));
- //打开ls和grep进程
- read_fp = popen("ls -l", "r");
- write_fp = popen("grep rwxrwxr-x", "w");
- //两个进程都打开成功
- if(read_fp && write_fp)
- {
- //读取一个数据块
- chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
- while(chars_read > 0)
- {
- buffer[chars_read] = '\0';
- //把数据写入grep进程
- fwrite(buffer, sizeof(char), chars_read, write_fp);
- //还有数据可读,循环读取数据,直到读完所有数据
- chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
- }
- //关闭文件流
- pclose(read_fp);
- pclose(write_fp);
- exit(EXIT_SUCCESS);
- }
- exit(EXIT_FAILURE);
- }
- #include <unistd.h>
- int pipe(int file_descriptor[2]);
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- int data_processed = 0;
- int filedes[2];
- const char data[] = "Hello pipe!";
- char buffer[BUFSIZ + 1];
- pid_t pid;
- //清空缓冲区
- memset(buffer, '\0', sizeof(buffer));
- if(pipe(filedes) == 0)
- {
- //创建管道成功
- //通过调用fork创建子进程
- pid = fork();
- if(pid == -1)
- {
- fprintf(stderr, "Fork failure");
- exit(EXIT_FAILURE);
- }
- if(pid == 0)
- {
- //子进程中
- //读取数据
- data_processed = read(filedes[0], buffer, BUFSIZ);
- printf("Read %d bytes: %s\n", data_processed, buffer);
- exit(EXIT_SUCCESS);
- }
- else
- {
- //父进程中
- //写数据
- data_processed = write(filedes[1], data, strlen(data));
- printf("Wrote %d bytes: %s\n", data_processed, data);
- //休眠2秒,主要是为了等子进程先结束,这样做也只是纯粹为了输出好看而已
- //父进程其实没有必要等等子进程结束
- sleep(2);
- exit(EXIT_SUCCESS);
- }
- }
- exit(EXIT_FAILURE);
- }
- #include <unistd.h>
- int dup(int file_descriptor);
- int dup2(int file_descriptor_one, int file_descriptor_two);
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- int data_processed = 0;
- int pipes[2];
- const char data[] = "123";
- pid_t pid;
- if(pipe(pipes) == 0)
- {
- pid = fork();
- if(pid == -1)
- {
- fprintf(stderr, "Fork failure!\n");
- exit(EXIT_FAILURE);
- }
- if(pid == 0)
- {
- //子进程中
- //使标准输入指向fildes[0]
- close(0);
- dup(pipes[0]);
- //关闭pipes[0]和pipes[1],只剩下标准输入
- close(pipes[0]);
- close(pipes[1]);
- //启动新进程od
- execlp("od", "od", "-c", 0);
- exit(EXIT_FAILURE);
- }
- else
- {
- //关闭pipes[0],因为父进程不用读取数据
- close(pipes[0]);
- data_processed = write(pipes[1], data, strlen(data));
- //写完数据后,关闭pipes[1]
- close(pipes[1]);
- printf("%d - Wrote %d bytes\n", getpid(), data_processed);
- }
- }
- exit(EXIT_SUCCESS);
- }