博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
进程通信之无名管道
阅读量:4298 次
发布时间:2019-05-27

本文共 1976 字,大约阅读时间需要 6 分钟。

1、无名管道有一定的局限性
       –它是属于半双工的通信方式

       –只有具有“亲缘关系”的的进程才能使用这种通信方式,也就是父进程和子进程之间。

2、man 2 pipe

3、int pipe(int pipefd[2])

     –参数pipefd[0]:用于读管道
     –参数pipefd[1]:用于写管道
     –返回值:执行成功返回0,失败返回-1

例:

#include 
#include
#include
#include
//进程读函数void read_data(int *);//进程写函数 void write_data(int *);int main(int argc,char *argv[]){ int pipes[2],rc; pid_t pid; rc = pipe(pipes); //创建管道 if(rc == -1){ perror("\npipes\n"); exit(1); } pid = fork(); //创建进程 switch(pid){ case -1: perror("\nfork\n"); exit(1); case 0: read_data(pipes); //相同的pipes default: write_data(pipes); //相同的pipes } return 0;}//进程读函数void read_data(int pipes[]){ int c,rc; //由于此函数只负责读,因此将写描述关闭(资源宝贵) close(pipes[1]); //阻塞,等待从管道读取数据 //int 转为 unsiged char 输出到终端 while( (rc = read(pipes[0],&c,1)) > 0 ){ putchar(c); } exit(0);}//进程写函数void write_data(int pipes[]){ int c,rc; //关闭读描述字 close(pipes[0]); while( (c=getchar()) > 0 ){ rc = write( pipes[1], &c, 1); //写入管道 if( rc == -1 ){ perror("Parent: write"); close(pipes[1]); exit(1); } } close( pipes[1] ); exit(0);}
2、管道标准库函数

#include<stdio.h>

FIFE *popen(const char *command, const char *mode)
int pclose(FIFE *stream);
command:参数command是一个在shell中可运行的命令字符串的指针,参数mode是一个字符指针,这个参数只有两种值可以使用,r或者w,分别表示popen()函数的返回值是一个读打开文件指针,还是写打开文件指针。当函数失败时返回值为NULL,并设置出错变量errno。
popen函数先执行创建一个管道,然后调用fork()函数创建子进程紧接着执行一个exec()函数调用,调用/bin/sh-c来执行参数command中的命令字符串,然后函数返回一个标准的I/O文件指针。

例:

#include
#include
#include
#include
#include
#include
#define BUFSE PIPE_BUFint main(void){ FIFE *fp; char *cmd = "cat file1" ; char buf[BUFSZ]; if(fp = popen(cmd,"r") == NULL) { perroe("failed to popen"); exit(1); } while((fgets(buf,BUFSZ,fp)) != NULL); printf("%s",buf); pclose(fp); exit(1);}
$ recat.c -o react 
$./recat
used the popen and pclose function to create a pipe !!!

转载地址:http://xynws.baihongyu.com/

你可能感兴趣的文章
东航期货行情接口和交易接口(20190509)
查看>>
ubnutu系统完美克隆至新硬盘,系统备份迁移至新硬盘
查看>>
ubnutu系统完美克隆至新硬盘,系统备份迁移至新硬盘
查看>>
东航期货模拟交易brockerid(期货公司的客户号)
查看>>
史上最全量化资源整理
查看>>
vnpy2.0安装后报错ModuleNotFoundError: No module named 'vnpy.api.ctp.vnctpmd'
查看>>
VNPY2.0火币期货交易接口配置使用
查看>>
win10和ubuntu18双系统时间同步(20190604亲测可行)
查看>>
重启小狼毫输入法,rime输入法重启
查看>>
命令行或终端ImportError:No module named(pycharm运行没问题)
查看>>
量化策略回测01双均线
查看>>
量化策略回测ATRRSI
查看>>
量化干货:量化交易系统设计的六大细节
查看>>
量化策略回测tdma
查看>>
量化策略回测TRIXKDJ
查看>>
量化策略回测唐安奇通道
查看>>
CTA策略如何过滤部分震荡行情?
查看>>
量化策略回测DualThrust
查看>>
量化策略回测BoolC
查看>>
量化策略回测DCCV2
查看>>