进程间通信(IPC):管道(Pipe)
来源:互联网 发布:姚明0809赛季数据 编辑:程序博客网 时间:2024/06/10 19:00
管道:一个进程连接数据流到另一个程序
pipe函数的原型:
#include <unistd.h>int pipe(int file_descriptor[2]);该闪身在数组中填上两个新的文件描述符后返回0,如果失败则返回-1。写到file_descriptor[1]的所有数据都可以从file_descriptor[0]读回来。这里使用的是文件描述符而不是文件流,所以我们必须用底层的read和write调用来访问数据,而不是用文件流库函数fread和fwrite
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(){ int data_processed; int file_pipes[2]; const char some_data[] = "123"; char buffer[BUFSIZ + 1]; memset(buffer, '\0', sizeof(buffer)); if (pipe(file_pipes) == 0) { data_processed = write(file_pipes[1], some_data, strlen(some_data)); printf("Wrote %d bytes\n", data_processed); data_processed = read(file_pipes[0], buffer, BUFSIZ); printf("Read %d bytes: %s\n", data_processed, buffer); exit(EXIT_SUCCESS); } exit(EXIT_FAILURE);}~输出:
Wrote 3 bytesRead 3 bytes: 123
可以通过管道在两个进程之间传递数据
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(){ int data_processed; int file_pipes[2]; const char some_data[] = "123"; char buffer[BUFSIZ + 1]; pid_t fork_result; memset(buffer, '\0', sizeof(buffer)); if (pipe(file_pipes) == 0) { fork_result = fork(); if (fork_result == -1) { fprintf(stderr, "Fork failure"); exit(EXIT_FAILURE); }// We've made sure the fork worked, so if fork_result equals zero, we're in the child process. if (fork_result == 0) { data_processed = read(file_pipes[0], buffer, BUFSIZ); printf("Read %d bytes: %s\n", data_processed, buffer); exit(EXIT_SUCCESS); }// Otherwise, we must be the parent process. else { data_processed = write(file_pipes[1], some_data, strlen(some_data)); printf("Wrote %d bytes\n", data_processed); } } exit(EXIT_SUCCESS);}
Wrote 3 bytesjessica@ubuntu:~$ Read 3 bytes: 123
这个程序实现了再不同的进程之间进行读写操作。但仅仅是运行一个相同的程序。
接下来的程序中子进程中运行一个与其父进程完全不同的另外一个程序。
下面是pipe3.c
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(){ int data_processed; int file_pipes[2]; const char some_data[] = "123"; char buffer[BUFSIZ + 1]; pid_t fork_result; memset(buffer, '\0', sizeof(buffer)); if (pipe(file_pipes) == 0) { fork_result = fork(); if (fork_result == (pid_t)-1) { fprintf(stderr, "Fork failure"); exit(EXIT_FAILURE); } if (fork_result == 0) { sprintf(buffer, "%d", file_pipes[0]); (void)execl("pipe4", "pipe4", buffer, (char *)0); exit(EXIT_FAILURE); } else { data_processed = write(file_pipes[1], some_data, strlen(some_data)); printf("%d - wrote %d bytes\n", getpid(), data_processed); } } exit(EXIT_SUCCESS);}
下面试pipe4.c
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){ int data_processed; char buffer[BUFSIZ + 1]; int file_descriptor; memset(buffer, '\0', sizeof(buffer)); sscanf(argv[1], "%d", &file_descriptor); data_processed = read(file_descriptor, buffer, BUFSIZ); printf("%d - read %d bytes: %s\n", getpid(), data_processed, buffer); exit(EXIT_SUCCESS);}
pipe3在程序中调用pipe4,pipe4程序从参数字符串中提取出文件描述符数字,然后读取该文件描述符来获取数据。
- 进程间通信(IPC):管道(Pipe)
- 深入理解Linux进程间通信(IPC)-- 管道pipe
- 进程间通信-管道 IPC pipe
- 进程间通信IPC之--无名管道(pipe)和有名管道(fifo)
- 进程间通信:管道(pipe)
- 进程间通信--无名管道(pipe)
- 进程间通信(IPC)之管道
- 进程间通信(IPC)1 ------ 管道
- Linux--进程间通信(一)-管道(pipe)通信
- Linux进程通信IPC--管道Pipe/Named Pipe
- Linux进程间通信IPC(一)-- pipe
- 进程通信之管道(PIPE)
- Linux进程通信-管道(pipe)
- 进程间通信IPC --pipe
- 1、进程间通信-PIPE(无名管道)
- 进程间通信(一): 管道(pipe+fifo)
- linux进程间通信之管道(pipe与fifo)
- 进程间通信-半双工管道(pipe)
- mysql编码问题
- 软件开发过程二 编码
- 键盘码、ASCII码表
- Devexpress立即结束GridControl的编辑并更新到数据源
- 混合高斯模型(Mixtures of Gaussians)和EM算法
- 进程间通信(IPC):管道(Pipe)
- 浅析jQuery基础框架
- 删除了googlecode中的directui-erp项止
- 实战解析:如何整合iBatis和Spring
- 解决页面中不能复制问题.txt
- [上海]LinkCoder - 软件开发时代的变迁:从Application 到App
- 软件工程 测试
- Stay Hungry, Stay Foolish
- 记录关于c4droid的一些简单资料