Linux编程之pipe()函数详解与管道通信机制
管道是用于连接两个进程的标准输入与标准输出的机制,以此实现多进程间的通信。创建管道时需提供两个文件描述符,分别用于执行写操作和读操作。对管道的读写遵循常规IO系统函数规范,即通过write()写入数据,通过read()读取数据。
#include
int pipe(int filedes[2]);
该函数成功时返回0,失败则返回-1。参数数组包含用于管道操作的兩個文件描述符:fd[0]对应读端,fd[1]对应写端。
调用pipe()必须在fork()之前进行,否则子进程无法继承文件描述符。若两个进程没有共同的祖先进程,则不能使用匿名管道,但可改用命名管道。



当向管道写入数据时,若数据量小于128K,写入非原子操作;若超过128K字节,缓冲区数据将连续写入直至完成。若无进程读取数据,写入操作将持续阻塞,如下所示:



在上述程序示例中,子进程一次性写入128K数据。当父进程完全读取完毕后,子进程的write()函数才解除阻塞并
返回写入信息。
命名管道FIFO
匿名管道的主要缺陷在于缺乏名称,仅适用于拥有共同祖先进程的进程间通信。FIFO代表先进先出,属于单向数据流(半双工),与
匿名管道的区别在于:每个FIFO都关联一个路径名,使得无亲缘关系的进程也能进行访问。
#include
#include
int mkfifo(const char *pathname, mode_t mode);
其中pathname指定路径名,mode为sys/stat.h中定义的创建文件权限。
有亲缘关系进程间的fifo的例子
/*
* 有亲缘关系的进程间的fifo的使用
* fifo 使用的简单例子
*/
#include "../all.h"
#define FIFO_PATH "/tmp/hover_fifo"
void
do_sig(int signo)
{
if (signo == SIGCHLD)
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
int
main(void)
{
int ret;
int fdr, fdw;
pid_t pid;
char words[10] = "123456789";
char buf[10] = {'\0'};
// 创建它,若存在则不算是错误,
// 若想修改其属性需要先打开得到fd,然后用fcntl来获取属性,然后设置属性.
if (((ret = mkfifo(FIFO_PATH, FILE_MODE)) == -1)
&& (errno != EEXIST))
perr_exit("mkfifo()");
fprintf(stderr, "fifo : %s created successfully!\n", FIFO_PATH);
signal(SIGCHLD, do_sig);
pid = fork();
if (pid == 0) { // child
if ((fdr = open(FIFO_PATH, O_WRONLY)) < 0) // 打开fifo用来写
perr_exit("open()");
sleep(2);
// 写入数据
if (write(fdr, words, sizeof(words)) != sizeof(words))
perr_exit("write");
fprintf(stderr, "child write : %s\n", words);
close(fdw);
} else if (pid > 0) { // parent
if ((fdr = open(FIFO_PATH, O_RDONLY)) < 0) // 打开fifo用来读
perr_exit("open()");
fprintf(stderr, "I father read, waiting for child ...\n");
if (read(fdr, buf, 9) != 9) //读数据
perr_exit("read");
fprintf(stderr, "father get buf : %s\n", buf);
close(fdr);
}
// 到这里fifo管道并没有被删除,必须手动调用函数unlink或remove删除.
return 0;
}
从上述示例可以看出,使用FIFO时需关注以下几点:
* FIFO管道需先调用mkfifo创建,再通过open打开获取文件描述符后方可使用。
* 打开FIFO时需注意其为半双工特性,通常不可使用O_RDWR标志打开,而应仅使用只读或只写方式打开。
FIFO可用于非亲缘关系进程间通信,其主要应用场景在服务器与客户端之间。由于它是半双工的,若要实现客户端与服务器的双向通信,
每个方向都必须建立两个管道,一个用于读取,另一个用于写入。
下面是一个服务器处理多个客户端连接的FIFO示例:
server 端的例子:
/*
* FIFO server
*/
#include "all.h"
int
main(void)
{
int fdw, fdw2;
int fdr;
char clt_path[PATH_LEN] = {'\0'};
char buf[MAX_LINE] = {'\0'};
char *p;
int n;
if (mkfifo(FIFO_SVR, FILE_MODE) == -1 && errno != EEXIST)
perr_exit("mkfifo()");
if ((fdr = open(FIFO_SVR, O_RDONLY)) < 0)
perr_exit("open()");
/*
* 根据fifo的创建规则, 若从一个空管道或fifo读,
* 而在读之前管道或fifo有打开来写的操作, 那么读操作将会阻塞
* 直到管道或fifo不打开来读, 或管道或fifo中有数据为止.
*
* 这里,我们的fifo本来是打开用来读的,但是为了,read不返回0,
* 让每次client端读完都阻塞在fifo上,我们又打开一次来读.
* 见unpv2 charper 4.7
*/
if ((fdw2 = open(FIFO_SVR, O_WRONLY)) < 0)
fprintf(stderr, "open()");
while (1) {
/* read client fifo path from FIFO_SVR */
/* 这里由于FIFO_SVR有打开来写的操作,所以当管道没有数据时,
* read会阻塞,而不是返回0.
*/
if (read(fdr, clt_path, PATH_LEN) < 0) {
fprintf(stderr, "read fifo client path error : %s\n", strerror(errno));
break;
}
if ((p = strstr(clt_path, "\r\n")) == NULL) {
fprintf(stderr, "clt_path error: %s\n", clt_path);
break;
}
*p = '\0';
DBG("clt_path", clt_path);
if (access(clt_path, W_OK) == -1) { // client fifo ok, but no permission
perror("access()");
continue;
}
/* open client fifo for write */
if ((fdw = open(clt_path, O_WRONLY)) < 0) {
perror("open()");
continue;
}
if ((n = read(fdr, buf, WORDS_LEN)) > 0) { /* read server words is ok */
printf("server read words : %s\n", buf);
buf[n] = '\0';
write(fdw, buf, strlen(buf));
}
}
close(fdw);
unlink(FIFO_SVR);
exit(0);
}
客户端的例子:
/*
* Fifo client
*
*/
#include "all.h"
int
main(void)
{
int fdr, fdw;
pid_t pid;
char clt_path[PATH_LEN] = {'\0'};
char buf[MAX_LINE] = {'\0'};
char buf_path[MAX_LINE] = {'\0'};
snprintf(clt_path, PATH_LEN, FIFO_CLT_FMT, (long)getpid());
DBG("clt_path1 = ", clt_path);
snprintf(buf_path, PATH_LEN, "%s\r\n", clt_path);
if (mkfifo(clt_path, FILE_MODE) == -1 && errno != EEXIST)
perr_exit("mkfifo()");
/* client open clt_path for read
* open server for write
*/
if ((fdw = open(FIFO_SVR, O_WRONLY)) < 0)
perr_exit("open()");
/* write my fifo path to server */
if (write(fdw, buf_path, PATH_LEN) != PATH_LEN)
perr_exit("write()");
if (write(fdw, WORDS, WORDS_LEN) < 0) /* write words to fifo server */
perr_exit("error");
if ((fdr = open(clt_path, O_RDONLY)) < 0)
perr_exit("open()");
if (read(fdr, buf, WORDS_LEN) > 0) { /* read reply from fifo server */
buf[WORDS_LEN] = '\0';
printf("server said : %s\n", buf);
}
close(fdr);
unlink(clt_path);
exit(0);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。