小知识:linux编程之pipe()函数详解

管道是一种把两个进程之间的标准输入和标准输出连接起来的机制,从而提供一种让多个进程间通信的方法,当进程创建管道时,每次都需要提供两个文件描述符来操作管道。其中一个对管道进行写操作,另一个对管道进行读操作。对管道的读写与一般的io系统函数一致,使用write()函数写入数据,使用read()读出数据。%小知识:linux编程之pipe()函数详解-猿站网-插图

?
1
2
3
#include<unistd.h>
int pipe(int filedes[2]);

返回值:成功,返回0,否则返回-1。参数数组包含pipe使用的两个文件的描述符。fd[0]:读管道,fd[1]:写管道。

必须在fork()中调用pipe(),否则子进程不会继承文件描述符。两个进程不共享祖先进程,就不能使用pipe。但是可以使用命名管道。

%小知识:linux编程之pipe()函数详解-1猿站网-插图

%小知识:linux编程之pipe()函数详解-2猿站网-插图%小知识:linux编程之pipe()函数详解-3猿站网-插图

当管道进行写入操作的时候,如果写入的数据小于128k则是非原子的,如果大于128k字节,缓冲区的数据将被连续地写入管道,直到全部数据写完为止,如果没有进程读取数据,则将一直阻塞,如下:%小知识:linux编程之pipe()函数详解-4猿站网-插图

%小知识:linux编程之pipe()函数详解-5猿站网-插图

%小知识:linux编程之pipe()函数详解-6猿站网-插图

%小知识:linux编程之pipe()函数详解-7猿站网-插图

在上例程序中,子进程一次性写入128k数据,当父进程将全部数据读取完毕的时候,子进程的write()函数才结束阻塞并且

返回写入信息。

命名管道fifo

管道最大的劣势就是没有名字,只能用于有一个共同祖先进程的各个进程之间。fifo代表先进先出,单它是一个单向数据流,也就是半双工,和

管道不同的是:每个fifo都有一个路径与之关联,从而允许无亲缘关系的进程访问。       

?
1
2
3
4
5
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);

这里pathname是路径名,mode是sys/stat.h里面定义的创建文件的权限.

有亲缘关系进程间的fifo的例子

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* 有亲缘关系的进程间的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 端的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* 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);
}

客户端的例子: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* 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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/kunhu/p/3608109.html

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Linux svn的搭建与使用(图文详解)

2023-4-18 4:21:15

建站知识

小知识:使用nginx实现分布式限流的方法

2023-4-18 4:28:12

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索