linux 进程编程:无名管道pipe
2015-05-24 22:36
495 查看
今天写一下利用无名管道pipe进行进程间通信。
该通信方法利用两个文件描述符进行通信,其中pipes[0] 用于读, pipes[1]用于写。
相关函数
#include <unistd.h>
pid_t fork(void);
int pipe(int file_descriptor[2]);
测试代码
[main:28] read data:hello pipe!
该通信方法利用两个文件描述符进行通信,其中pipes[0] 用于读, pipes[1]用于写。
相关函数
#include <unistd.h>
pid_t fork(void);
int pipe(int file_descriptor[2]);
测试代码
#include <stdio.h> #include <unistd.h> int main(int argc, char **argv) { int ret = -1; unsigned char test_buf[16] = {0}; pid_t pid; int pipes[2]; ret = pipe(pipes); if(0 != ret) { printf("[%s:%d]pipe fail\n", __func__, __LINE__); } else { pid = fork(); if(pid < 0) { printf("[%s:%d] fork fail\n", __func__, __LINE__); } else if(0 == pid) { ret = read(pipes[0], test_buf, sizeof(test_buf)); if(ret > 0) { printf("[%s:%d] read data:%s\n", __func__, __LINE__, test_buf); } } else { ret = write(pipes[1], "hello pipe!", 11); if(ret <= 0) { printf("[%s:%d] write data fail\n", __func__, __LINE__); } } } return 0; }运行结果
[main:28] read data:hello pipe!
相关文章推荐
- Linux 高级编程 - 无名管道 Pipe
- linux下c编程之无名管道pipe()函数
- 【Linux系统编程】进程间通信--无名管道(pipe)
- linux应用编程--pipe无名管道
- 进程返回linux系统编程之管道(二):管道读写规则和Pipe Capacity、PIPE_BUF
- Linux进程间通讯一--无名管道PIPE
- Linux编程 进程间通信------无名管道(PIPE)
- linux编程---进程间通信---PIPE---无名管道
- Linux 进程间通讯之创建无名管道和读写无名管道
- linux系统编程之管道(一):匿名管道(pipe)
- linux 进程编程:有名管道fifo
- Linux下的无名管道pipe的设计
- Linux 进程间通讯之创建无名管道和读写无名管道
- linux进程间管道通信pipe与fifo
- linux系统编程之管道(一):匿名管道(pipe)
- linux进程通信--无名管道
- 实验 9-1 1. 实现以下功能 a) 调用 pipe()创建无名管道 b) 调用 fork 创建一个子进程 c) 在子进程中向管道内写入 128k 数据,打印出进程号及成功写入的字节数 d) 在父
- LINUX编程——进程间管道通信
- Linux编程—进程间通信—无名管道(1)
- Linux的进程编程-之二-进程间通信(管道)