Linux——IPC 管道、FIFO

来源:互联网 发布:linux 系统内核版本 编辑:程序博客网 时间:2024/05/17 01:57

 

最好的参考资料:

1.师从互联网。

2.UNP v2 Posix IPC的相关章节4。

3.Linux man 命令。

第一条:管道

 

概述:无名,只能用于亲缘进程间数据传递!!

#include <unistd.h>

int pipe(int pipefd[2]);

#define _GNU_SOURCE

#include <unistd.h>

int pipe2(int pipefd[2], int flags);

函数调用正确返回后,fd[0]可读,fd[1]可写。他们分别位于管道的两端!

 

 #include <stdio.h>

FILE *popen(const char *command, const char *type);

int pclose(FILE *stream);

重点说下popen的type参数:

当tpye为"r":执行command参数的命令后,产生的结果放到FILE * 类型中,通过popen返回。之后我们就可以“读”这个FILE*里的数据了^_^!!这才是“r”的真谛!!例如:command为“date”,那么我们读这个FILE *结构的数据应为:2011年 01月 01日 星期六 18:54:47 CST。

当tpyewei“w”:popen返回的FILE * 指针将作为command的标准输入。比如:command为cat >file时 ,这个FILE *指针将用于向file真写入数据!!

第二条:FIFO

概述:有名,任意知道他名字的进程间传递信息。

 

#include <sys/types.h>

#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);

mkfifo 隐含指定O_CREAT|O_EXCL,你们懂的!!

具体使用方法是,分别指定O_RDONLY、O_WRONLY的open调用打开pathname参数,得到的文件描述符分别用来读、和写操作。如此简单实用。

 

 

 

原创粉丝点击