抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

进程间通讯的概念

进程是一个独立的资源分配单元,不同进程(这里所说的进程通常指的是用户进程)之间的资源是独立的,没有关联,不能在一个进程中直接访问另一个进程的资源。

但是,进程不是孤立的,不同的进程需要进行信息的交互和状态的传递等,因此需要进程间通信(IPC: Inter Processes Communication )。

GUI:用户图像接口

IDE:集成开发环境

API:应用程序接口

进程间通信的目的:

  • 数据传输:一个进程需要将它的数据发送给另一个进程。
  • 通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种事件(如进程终止时要通知父进程)。
  • 资源共享:多个进程之间共享同样的资源。为了做到这一点,需要内核提供互斥和同步机制。
  • (同步(就是排队看病 - 安全)、异步(一个医生一起看多个病人 - 不安全))
  • 进程控制:有些进程希望完全控制另一个进程的执行(如Debug 进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变。

Linux进程间通信的方式

image-20220417194955298

匿名管道

管道也叫无名(匿名)管道,它是是UNIX系统IPC(进程间通信)的最古老形式,所有的UNIX系统都支持这种通信机制。

统计一个目录中文件的数目命令: ls | wc -l,为了执行该命令, shell创建了两个进程来分别执行 ls 和 wc。

image-20220418085601242

管道特点 匿名管道 - 有名管道 共同特点

管道其实是一个在内核'内存中'通过管道传递的数据是顺序的,从管道中读取出来的字节的顺序和它们被写入管道的顺序是完全一样的。的缓冲器,这个缓冲器的存储能力是有限的,不同的操作系统大小不一定相同。

管道拥有文件的特质:读操作、写操作,匿名管道没有文件实体(有关系的进程 父进程 - 子进程 - 孙子进程),有名管道有文件实体(没有关系的进程),但不存储数据。可以按照操作文件的方式对管道进行操作。

一个管道是一个字节流(一个字节 一个字节 在管道里面),使用管道时不存在消息或者消息边界的概念,从管道读取数据的进程可以读取任意大小的数据块,而不管写入进程写入管道的数据块的大小是多少。

通过管道传递的数据是顺序的,从管道中读取出来的字节的顺序和它们被写入管道的顺序是完全一样的。

在管道中的数据的传递方向是单向的,一端用于写入,一端用于读取,管道是半双工的。

单工(遥控器发射信号给电视机 但是电视机不能给遥控器发射信号 这就是单工)

双工(打电话 1给2打电话 1 说 二可以听到、2说 1可以听到 同时)

半双工(同一时间只能一个方向就好比那放学 都往出走你就进不去(当然啊 除非你不要命的硬挤进去!!!) 当出来的都走完了进的在往里进)

从管道读数据是一次性操作,数据一旦被读走,它就从管道中被抛弃,释放空间以便写更多的数据.在管道中无法使用lseek ()来随机的访问数据。

匿名管道只能在具有公共祖先的进程(父进程与子进程,或者两个兄弟进程,具有亲缘关系)之间使用

image-20220418131840375

为什么可以使用管道进行进程通信

image-20220418133041024

管道(红色长方体) 左侧读 右侧写

父 - 子 进程

image-20220418133402645

管道的数据结构

image-20220418183322833

匿名管道的使用

  • 创建匿名管道
1
2
#include <unistd.h>
int pipe(int pipefd[2]);
  • 查看管道缓冲大小命令
1
ulimit -a
  • 查看管道缓冲大小函数
1
2
3
#include <unistd.h>
long fpathconf(int fd, int name);

代码

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
/*************************************************************************
> File Name: pipe.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月18日 星期一 19时08分03秒
************************************************************************/

/*
* #include <unistd.h>
* int pipe(int pipefd[2]);
* 功能: 创建一个匿名管道, 用来进程间通信
* 参数: int pipefd[2] 这个数组是一个传出参数。
* - pipefd[0] : 对应管道的读端
* - pipefd[1] : 对应管道的写端
* 返回值:
* 成功返回 0
* 失败返回 -1
* 注意: 匿名管道只能用于具有关系的进程之间通信 (父子进程, 兄弟进程, 孙子进程)
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

// 子进程发送数据给父进程, 父进程读取到数据输出
int main() {

// 再 fork 之前创建管道
int pipefd[2];
int ret = pipe(pipefd);
if(ret == -1) {
perror("pipe");
exit(0);
}

// 创建子进程
pid_t pid = fork();

if(pid > 0) {
// 父进程
// 从管道的读取端读取数据
char buf[1024] = {0};
int len = read(pipefd[0], buf, sizeof(buf));
printf("parent revc : %s, pid : %d\n", buf, getpid());

} else if(pid == 0){
// 子进程
char *str = "hello,i am child";
write(pipefd[1], str, strlen(str));
}

return 0;
}

先猜想: 如果子进程写入前 sleep(10) 后 中端输出什么:

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
/*************************************************************************
> File Name: pipe.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月18日 星期一 19时08分03秒
************************************************************************/

/*
* #include <unistd.h>
* int pipe(int pipefd[2]);
* 功能: 创建一个匿名管道, 用来进程间通信
* 参数: int pipefd[2] 这个数组是一个传出参数。
* - pipefd[0] : 对应管道的读端
* - pipefd[1] : 对应管道的写端
* 返回值:
* 成功返回 0
* 失败返回 -1
* 注意: 匿名管道只能用于具有关系的进程之间通信 (父子进程, 兄弟进程, 孙子进程)
* 管道默认是阻塞的, 如果管道中没有数据, read阻塞, 如果管道满了, write阻塞
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

// 子进程发送数据给父进程, 父进程读取到数据输出
int main() {

// 再 fork 之前创建管道
int pipefd[2];
int ret = pipe(pipefd);
if(ret == -1) {
perror("pipe");
exit(0);
}

// 创建子进程
pid_t pid = fork();

if(pid > 0) {
// 父进程
// 从管道的读取端读取数据
char buf[1024] = {0};
int len = read(pipefd[0], buf, sizeof(buf));
printf("parent revc : %s, pid : %d\n", buf, getpid());

} else if(pid == 0){
// 子进程
sleep(10);
char *str = "hello,i am child";
write(pipefd[1], str, strlen(str));
}

return 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
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
76
77
78
79
80
/*************************************************************************
> File Name: pipe.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月18日 星期一 19时08分03秒
************************************************************************/

/*
* #include <unistd.h>
* int pipe(int pipefd[2]);
* 功能: 创建一个匿名管道, 用来进程间通信
* 参数: int pipefd[2] 这个数组是一个传出参数。
* - pipefd[0] : 对应管道的读端
* - pipefd[1] : 对应管道的写端
* 返回值:
* 成功返回 0
* 失败返回 -1
* 注意: 匿名管道只能用于具有关系的进程之间通信 (父子进程, 兄弟进程, 孙子进程)
* 管道默认是阻塞的, 如果管道中没有数据, read阻塞, 如果管道满了, write阻塞
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

// 子进程发送数据给父进程, 父进程读取到数据输出
int main() {

// 再 fork 之前创建管道
int pipefd[2];
int ret = pipe(pipefd);
if(ret == -1) {
perror("pipe");
exit(0);
}

// 创建子进程
pid_t pid = fork();

if(pid > 0) {
// 父进程
// 从管道的读取端读取数据
printf("i am parent process, pid : %d\n", getpid());
char buf[1024] = {0};
// 关闭写端
close(pipefd[1]);
while(1) {
int len = read(pipefd[0], buf, sizeof(buf));
printf("parent revc : %s, pid : %d\n", buf, getpid());

// char *str = "i am parent";
// write(pipefd[1], str, strlen(str));
// sleep(1);

}

} else if(pid == 0){
// 子进程
printf("i am child process, pid : %d\n", getpid());

char buf[1024];
// 关闭读端
close(pipefd[0]);
while(1) {
char *str = "hello,i am child";
write(pipefd[1], str, strlen(str));
// sleep(1);

int len = read(pipefd[0], buf, sizeof(buf));
printf("pa child recv : %s, pid : %d\n", buf, getpid());
bsize(buf, 1024);
}

}

return 0;
}

// 发完数据还没有数字没清空

ps: 如果注释掉 sleep() 就会发现问题!就是自己写的会自己读回去 当然在项目中不会出现这种情况 项目中只会一端读一端写

image-20220419202543257

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
76
77
78
79
80
/*************************************************************************
> File Name: pipe.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月18日 星期一 19时08分03秒
************************************************************************/

/*
* #include <unistd.h>
* int pipe(int pipefd[2]);
* 功能: 创建一个匿名管道, 用来进程间通信
* 参数: int pipefd[2] 这个数组是一个传出参数。
* - pipefd[0] : 对应管道的读端
* - pipefd[1] : 对应管道的写端
* 返回值:
* 成功返回 0
* 失败返回 -1
* 注意: 匿名管道只能用于具有关系的进程之间通信 (父子进程, 兄弟进程, 孙子进程)
* 管道默认是阻塞的, 如果管道中没有数据, read阻塞, 如果管道满了, write阻塞
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

// 子进程发送数据给父进程, 父进程读取到数据输出
int main() {

// 再 fork 之前创建管道
int pipefd[2];
int ret = pipe(pipefd);
if(ret == -1) {
perror("pipe");
exit(0);
}

// 创建子进程
pid_t pid = fork();

if(pid > 0) {
// 父进程
// 从管道的读取端读取数据
printf("i am parent process, pid : %d\n", getpid());
char buf[1024] = {0};
while(1) {
int len = read(pipefd[0], buf, sizeof(buf));
printf("parent revc : %s, pid : %d\n", buf, getpid());

char *str = "i am parent";
write(pipefd[1], str, strlen(str));
sleep(1);

}

} else if(pid == 0){
// 子进程
printf("i am child process, pid : %d\n", getpid());

char buf[1024];

// 关闭读端
close(pipefd[0]);

while(1) {
char *str = "hello,i am child";
write(pipefd[1], str, strlen(str));
sleep(1);

// int len = read(pipefd[0], buf, sizeof(buf));
// printf("pa child recv : %s, pid : %d\n", buf, getpid());
// bsize(buf, 1024);
}

}

return 0;
}

查看管道大小 ulimit -a

image-20220419190512476

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*************************************************************************
> File Name: fpathconfig.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月19日 星期二 19时05分50秒
************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
int pipefd[2];

int ret = pipe(pipefd);

// 获取管道的大小 可以查看man手册来查看宏值
long size = fpathconf(pipefd[0], _PC_PIPE_BUF);

printf("pipe size : %ld", size);

return 0;
}

匿名管道 - 案例

ls | wc -l - ps aux | grep

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
/*************************************************************************
> File Name: parent-child-ipc.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月20日 星期三 09时21分59秒
************************************************************************/

/*
* 实现 ps aux | grep xxx 父子进程通信
* 子进程: ps aux, 子进程结束后, 将数据发送给父进程
* 父进程: 获取到数据, 过滤
*
* pipe()
* execlp() -> ps aux 会输出到终端
* 子进程将标准输出 stdout_fileno 重定向管道的写端 dup2
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {

// 创建一个管道
int fd[2];
int ret = pipe(fd);

if(ret == -1) {
perror("pipe");
exit(0);
}

// 创建子进程
pid_t pid = fork();

if(pid > 0) {
// 父进程
// 关闭写端
close(fd[1]);
// 从管道中读取数据
char buf[1024];
int len = -1;
while((len = read(fd[0], buf, sizeof(buf) - 1)) > 0) {
// 因为每次最多只能读取 1024 所以需要循环
// 过滤数据
printf("%s", buf);
memset(buf, 0, sizeof(buf));
}
wait(NULL);
} else if(pid == 0) {
// 子进程
// 关闭读端
close(fd[0]);
// 文件描述符的重定向 stdout_fileno -> fd[1];
// 执行 ps aux
dup2(fd[1], STDOUT_FILENO);
execlp("ps", "ps", "aux", NULL);
perror("execlp");
exit(0);
} else {
perror("fork");
exit(0);
}

return 0;
}

管道的读写特点

使用管道时候,需要注意以下几种特殊的情况(假设都是I/O操作)

  1. 所有的指向管道写端文件描述符都关闭了 (管道写端引用计数为0),进程从管道的读端读数据,那么管道中剩余的数据被读取以后,再次read会返回0,就像读到文件末尾一样
  2. 如果有指向管道的写端的文件描述符没有关闭(管道的写端引用计数大于0),而持有管道写端的也没有往管道中写数据,这个时候有进程从管道中读取数据,那么管道中剩余的数据被读取后,再次read会阻塞,值到管道中有数据可以读了才读取数据并返回。
  3. 如果所有指向管道的读端的文件描述符都关闭了(管道的读端引用计数为0)这个时候有进程向管道中写数据,那么该进程会收到一个信号SIGPIPE(管道破裂),通常会导致进程异常终止
  4. 如果有指向管道读端的文件描述符没有关闭(管道的读端引用计数大于0)而持有管道读端的进程也没有从管道中读数据,这时有进程向管道中写数据,那么在管道被写满的时候在次调用write时候就会被阻塞直到管道中有空位置猜能再次写入数据并返回

总结:

​ 读管道:

​ 管道中有数据,red返回实际读到的字节数

​ 管道中无数据:

​ 写端被全包关闭,read返回0(相当于读到文件末尾)

​ 写端没有完全关闭,read阻塞等待

​ 写管道:

​ 管道读端全部都关闭,进程异常终止(进程SIGPIPE信号)

​ 管道读端没有完全关闭:

​ 管道已满:writ阻塞

​ 管道没满:wirt将数据写入,并返回实际写入的字节数。

管道 读非阻塞状态

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
/*************************************************************************
> File Name: pipe.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月18日 星期一 19时08分03秒
************************************************************************/

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

/*
* 设置管道非阻塞
* int flags = fcntl(fd[0], F_GETFL); // 获取原来的flags
* flags |= O_NONBLOCK; // 修改flags
* fcntl(fd[0], F_SETFL, flags); // 设置新的flgas
*
*/

// man 2 fcntl // 快捷键 shift + k

int main() {
// 再 fork 之前创建管道
int pipefd[2];
int ret = pipe(pipefd);
if(ret == -1) {
perror("pipe");
exit(0);
}

// 创建子进程
pid_t pid = fork();

if(pid > 0) {
// 父进程
// 关闭写
close(pipefd[1]);
// 从管道的读取端读取数据
printf("i am parent process, pid : %d\n", getpid());
char buf[1024] = {0};

int flags = fcntl(pipefd[0], F_GETFL);
flags |= O_NONBLOCK;
fcntl(pipefd[0], F_SETFL, flags);

while(1) {
int len = read(pipefd[0], buf, sizeof(buf));
printf("parent revc : %s, pid : %d\n", buf, getpid());
printf("len : %d\n", len);
memset(buf, 0 ,sizeof(buf));
sleep(2);
}

} else if(pid == 0){
// 关闭管道读
close(pipefd[0]);
// 子进程
printf("i am child process, pid : %d\n", getpid());

char buf[1024];

while(1) {
char *str = "hello,i am child";
write(pipefd[1], str, strlen(str));
sleep(5);

}

}

return 0;
}

有名管道

匿名管道,由于没有名字,只能用于亲缘关系的进程间通信。为了克服这个缺点,提出了有名管道(FIFO),也叫命名管道、FIFO文件。

有名管道(FIFO)不同于匿名管道之处在于它提供了一个路径名与之关联,以FIFO的文件形式存在于文件系统中,并且其打开方式与打开一个普通文件是一样的,这样即使与FIFO 的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过 FIFO相互通信,因此,通过FIFO不相关的进程也能交换数据。

一旦打开了 FIFO,就能在它上面使用与操作匿名管道和其他文件的系统调用一样的I/O系统调用了(如read ( ) 、 write ()和close())。与管道一样, FIFO也有一个写入端和读取端,并且从管道中读取数据的顺序与写入的顺序是一样的。FIFO的名称也由此而来:先入先出。

有名管道(FIFO)和匿名管道 (pipe)有一些特点是相同的,不一样的地方在于:

有名管道(FIFO)和匿名管道(pipe)有一些特点是相同的,不一样的地方在于:

  1. FIFO在文件系统中作为一个特殊文件存在,但 FIFO中的内容却存放在内存中。
  2. 当使用FIFO的进程退出后,FIFo文件将继续保存在文件系统中以便以后使用。
  3. FIFO 有名字,不相关的进程可以通过打开有名管道进行通信。

有名管道的使用

通过命令创建有名管道

mkfifo 名字

文件类型 p 就是一个管道文件

image-20220424191124817

通过函数创建有名管道

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

一旦使用mkfifo创建了一个FIFO就可以使用open打开它,常见的文件I/O函数都可以用于fifo。如:close、read、write、unlink等

FIFO严格遵循先进先出(First in First out), 对管道及FIFO的读总是从开始处返回数据,对它们的写则把数据添加到末尾。他们不支持诸如lseek()等文件定位操作

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
/*************************************************************************
> File Name: mkfifo.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月24日 星期日 19时24分40秒
************************************************************************/

/*
* 创建fifo文件
* 1.通过命令: mkfifo 名字
* 2.通过函数: int mkfifo(const char *pathname, mode_t mode);
*
* #include <sys/types.h>
* #include <sys/stat.h>
* int mkfifo(const char *pathname, mode_t mode);
* 参数:
* - pathname: 管道名称的路径
* - mode: 文件的权限 和 opne 的 mode 是一样的
* 是一个八进制的数
* 返回值:
* 成功返回: 0
* 失败返回: -1
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

int main() {

// 判断文件是否存在
int ret = access("fifo1", F_OK);
if(ret == -1) {
printf("管道不存在, 创建管道\n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}


}

return 0;
}

一端读、一端写

write

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
/*************************************************************************
> File Name: write.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月24日 星期日 20时20分58秒
************************************************************************/

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

// 向管道中写数据
int main() {
int ret = access("test", F_OK);

if(ret == -1) {
perror("管道不存在, 创建管道\n");
// 创建管道文件
ret = mkfifo("test", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 打开管道
int fd = open("test", O_WRONLY);
if(fd == -1) {
perror("open");
exit(0);
}

for(int i = 0; i < 100; i++) {
char buf[1024];
sprintf(buf, "hellow, %d\n", i);
printf("write data : %s\n", buf);
write(fd, buf, strlen(buf));
sleep(1);
}
close(fd);
return 0;
}

read

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
/*************************************************************************
> File Name: read.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月24日 星期日 20时25分41秒
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

// 从管道中读数据
int main() {

// 打开管道文件
int fd = open("test", O_RDONLY);
if(fd == -1) {
perror("open");
exit(0);
}

// 读数据
while(1) {
char buf[1024] = {0};
int len = read(fd, buf, sizeof(buf));
//
if(len == 0) {
printf("写端断开链接了...\n");
break;
}
printf("recv buf : %s\n", buf);
}
close(fd);

return 0;
}

image-20220425200818402

有名管道的注意事项

  1. 一个为只读而打开一个管道的进程会阻塞,直到另外一个进程为只写打开管道
  2. 一个为只写而打开一个管道的进程会阻塞,直到另外一个进程为只读打开管道

读管道:

​ 管道中有数据,read返回实际读到的字节数

​ 管道中无数据:

​ 管道写端被全部关闭,read返回0,(相当于读到文件末尾)

​ 写端没有全部被关闭,read阻塞等待

写管道:

​ 管道读端被全部关闭,进行异常终止(收到一个SIGPIPE信号)

​ 管道读端没有全部关闭:

​ 管道已经满了,write会阻塞

​ 管道没有满,write将数据写入,并返回实际写入的字节数。

有名管道实现简单版聊天功能 - 一问一答

image-20220426115047403

chatA

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*************************************************************************
> File Name: chatA.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月26日 星期二 11时54分44秒
************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

int main() {
// 判断有名管道1是否存在
int ret = access("fifo1", F_OK);
if(ret == -1) {
// 文件不存
printf("管道文件不存在, 创建对应的有名管道\n");
// 文件路径
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("fifo1");
exit(-1);
}
}

// 判断有名管道2是否存在
ret = access("fifo2", F_OK);
if(ret == -1) {
// 文件不存
printf("管道文件不存在, 创建对应的有名管道\n");
// 文件路径
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("fifo2");
exit(-1);
}
}

// 以只写方式打开管道fifo1 --- 1
int fdw = open("fifo1", O_WRONLY);
if(fdw == -1) {
perror("open - fifo1");
exit(-1);
}

printf("打开fifo1成功!等待写入... ...\n");

// 以只读方式打开管道fifo2 --- 2
int fdr = open("fifo2", O_RDONLY);
if(fdr == -1) {
perror("open - fifo2");
exit(-1);
}

printf("打开fifo2成功!等待读取... ...\n");

char buf[128];

// 循环写读数据
while(1) {

memset(buf, 0, sizeof(buf));
// 获取标准输入(stdin)的数据
fgets(buf, sizeof(buf), stdin);
// 写数据
ret = write(fdw, buf, sizeof(buf));
if(ret == -1) {
perror("writ");
exit(-1);
}

// 读管道数据
memset(buf, 0, sizeof(buf));
ret = read(fdr, buf, sizeof(buf));
if(ret <= 0) {
perror("read");
break;
}
printf(" buf : %s\n", buf);

}

// 关闭文件描述符
close(fdr);
close(fdw);
return 0;
}

chatB

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*************************************************************************
> File Name: chatA.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月26日 星期二 11时54分44秒
************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

int main() {
// 判断有名管道1是否存在
int ret = access("fifo1", F_OK);
if(ret == -1) {
// 文件不存
printf("管道文件不存在, 创建对应的有名管道\n");
// 文件路径
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("fifo1");
exit(-1);
}
}

// 判断有名管道2是否存在
ret = access("fifo2", F_OK);
if(ret == -1) {
// 文件不存
printf("管道文件不存在, 创建对应的有名管道\n");
// 文件路径
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("fifo2");
exit(-1);
}
}

// 以只读方式打开管道fifo1 --- 1
int fdr = open("fifo1", O_RDONLY);
if(fdr == -1) {
perror("open - fifo1");
exit(-1);
}

printf("打开fifo1成功!等待读取... ...\n");

// 以只写方式打开管道fifo2 --- 2
int fdw = open("fifo2", O_WRONLY);
if(fdw == -1) {
perror("open - fifo2");
exit(-1);
}

printf("打开fifo2成功!等待写入... ...\n");

char buf[128];

// 循环读写数据
while(1) {
// 读管道数据
memset(buf, 0, sizeof(buf));
ret = read(fdr, buf, sizeof(buf));
if(ret <= 0) {
perror("read");
break;
}
printf(" buf : %s\n", buf);

memset(buf, 0, sizeof(buf));
// 获取标准输入(stdin)的数据
fgets(buf, sizeof(buf), stdin);
// 写数据
ret = write(fdw, buf, strlen(buf));
if(ret == -1) {
perror("writ");
exit(-1);
}
}

// 关闭文件描述符
close(fdr);
close(fdw);
return 0;
}

image-20220427203503435

问!如何实现一方多次发送消息!怎么实现?

可以fork创建子进程一起跑 - 有时间我一定代码实现测试一下

代码如下

A - B 无限通信 嗷嗷叫!

chatA

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*************************************************************************
> File Name: chatA.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月30日 星期六 19时58分44秒
************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main() {

// 判断管道是否存在 - FIFO
int ret = access("fifo1", F_OK);
if(ret == -1) {
printf("FIFO1 文件不存在, 正在创建 ... ... \n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo1");
exit(-1);
}
}

ret = access("fifo2", F_OK);
if(ret == -1) {
printf("FIFO2 文件不存在, 正在创建 ... ... \n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo2");
exit(-1);
}
}

// 以只写方式打开 FIFO1
int fdw = open("fifo1", O_WRONLY);
if(fdw == -1) {
perror("open-FIFO1");
exit(-1);
}
printf("打开 FIFO1 成功! 等待写入....\n");

int fdr = open("fifo2", O_RDONLY);
if(fdr == -1) {
perror("open-FIFO2");
exit(-1);
}
printf("打开 FIFO2 成功! 等待读取....\n");

// 创建进程
pid_t pid = fork();
// char *buf = (char *)malloc(sizeof(char) * 5);
char buf[1024];

if(pid > 0) {

// 父进程
while(1) {
memset(buf, 0, sizeof(buf));
// 获取标准输入(stdin)的数据
fgets(buf, sizeof(buf), stdin);
ret = write(fdw, buf, sizeof(buf));
if(ret == -1) {
perror("writ");
exit(-1);
}
}

} else if(pid == 0) {

// 创建子进程
while(1) {
memset(buf, 0, sizeof(buf));
ret = read(fdr, buf, sizeof(buf));
if(ret <= 0) {
perror("read");
exit(-1);
break;
}
printf("buf : %s\n", buf);
}
}

// 关闭文件描述符
close(fdw);
close(fdr);
// free(buf);
return 0;
}

chatB

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*************************************************************************
> File Name: chatA.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月30日 星期六 19时58分44秒
************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main() {

// 判断管道是否存在 - FIFO
int ret = access("fifo1", F_OK);
if(ret == -1) {
printf("FIFO1 文件不存在, 正在创建 ... ... \n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo1");
exit(-1);
}
}

ret = access("fifo2", F_OK);
if(ret == -1) {
printf("FIFO2 文件不存在, 正在创建 ... ... \n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo2");
exit(-1);
}
}

// 以只读方式打开 FIFO1
int fdr = open("fifo1", O_RDONLY);
if(fdr == -1) {
perror("open-FIFO1");
exit(-1);
}
printf("打开 FIFO1 成功! 等待读取....\n");

int fdw = open("fifo2", O_WRONLY);
if(fdw == -1) {
perror("open-FIFO2");
exit(-1);
}
printf("打开 FIFO2 成功! 等待写入....\n");

// 创建进程
pid_t pid = fork();
// char *buf = (char *)malloc(sizeof(char) * 1024);
char buf[1024];

if(pid > 0) {
// 父进程
while(1) {
memset(buf, 0, sizeof(buf));
ret = read(fdr, buf, sizeof(buf));
if(ret <= 0) {
perror("read");
// exit(-1);
break;
}
printf("buf : %s\n", buf);
}

} else if(pid == 0) {
// 创建子进程

while(1) {
memset(buf, 0, sizeof(buf));
// 获取标准输入(stdin)的数据
fgets(buf, sizeof(buf), stdin);
ret = write(fdw, buf, sizeof(buf));
if(ret == -1) {
perror("writ");
exit(-1);
}
}
}

// 关闭文件描述符
close(fdr);
close(fdw);
return 0;
}

评论