linux编程之pipe()函数详解
管道是⼀种把两个进程之间的标准输⼊和标准输出连接起来的机制,从⽽提供⼀种让多个进程间通信的⽅法,当进程创建管道时,每次都需要提供两个⽂件描述符来操作管道。其中⼀个对管道进⾏写操作,另⼀个对管道进⾏读操作。对管道的读写与⼀般的IO系统函数⼀致,使⽤write()函数写⼊数据,使⽤read()读出数据。
#include<unistd.h>
int pipe(int filedes[2]);
返回值:成功,返回0,否则返回-1。参数数组包含pipe使⽤的两个⽂件的描述符。fd[0]:读管道,fd[1]:写管道。
必须在fork()中调⽤pipe(),否则⼦进程不会继承⽂件描述符。两个进程不共享祖先进程,就不能使⽤pipe。但是可以使⽤命名管道。
当管道进⾏写⼊操作的时候,如果写⼊的数据⼩于128K则是⾮原⼦的,如果⼤于128K字节,缓冲区的数据将被连续地写⼊管道,直到全部数据写完为⽌,如果没有进程读取数据,则将⼀直阻塞,如下:
在上例程序中,⼦进程⼀次性写⼊128K数据,当⽗进程将全部数据读取完毕的时候,⼦进程的write()函数才结束阻塞并且
返回写⼊信息。
命名管道FIFO
库函数printf详解管道最⼤的劣势就是没有名字,只能⽤于有⼀个共同祖先进程的各个进程之间。FIFO代表先进先出,单它是⼀个单向数据流,也就是半双⼯,和
管道不同的是:每个FIFO都有⼀个路径与之关联,从⽽允许⽆亲缘关系的进程访问。
#include <sys/types.h>
#include <sys/stat.h>
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打开得到fd来使⽤.
*在打开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()");