linux管道阻塞代码,linux中的管道

管道是一种最基本的IPC机制,由pipe函数创建:#include 

int pipe(int filedes[2]);

调用pipe函数就是在内核区开辟一块缓冲区(称为管道)。filedes[0]指向管道的读端,filedes[1]指向管道的写端。管道实际上就是一个打开的文件。pipe函数成功返回0,失败返回-1.

如何用管道实现两个进程间的通信?

1.父进程调用pipe函数开辟管道,得到两个文件描述符指向管道的两端。

2cb8bb702696c536c69f31344eff6def.png

2.父进程调用fork()创建子进程,那么子进程也有两个文件描述符指向该管道。

e1150afee94d50c2b7d01de7727f5dd7.png

3.父进程关闭管道读端,子进程关闭管道写端。父进程可以往管道写,子进程可以从管道

读,管道是环形队列实现的,数据从写端流,从读端流出,这样就实现了进程间通信。

484748186803ac048d4508d7811cbce4.png

代码实现:#include 

#include 

#include 

#include 

int main()

{

int _pipe[2];

int ret = pipe(_pipe);

if (ret == -1){

printf("create pipe error! errno code is : %d\n", errno);

return 1;

}

pid_t id = fork();

if (id 

printf("fork error!");

return 2;

}

else if (id == 0){ //child

close(_pipe[0]);

int i = 0;

char *_mesg_c = NULL;

while (i<100){

_mesg_c = "i am child!";

write(_pipe[1], _mesg_c, strlen(_mesg_c) + 1);

sleep(1);

i++;

}

}

else{ //father

close(_pipe[1]);

char _mesg[100];

int j = 0;

while (j<100){

memset(_mesg, '\0', sizeof(_mesg));

read(_pipe[0], _mesg, sizeof(_mesg));

printf("%s\n", _mesg);

j++;

}

}

return 0;

}

使用管道需要注意的4中情况:

1. 如果所有指向管道写端的文件描述符都关闭了(管道写端的引用计数等于0),仍然有

进程从管道的读端读数据,那么管道中剩余的数据都被读取后,再次read会返回0,就像

读到件末尾样。

测试代码:#include 

#include 

#include 

#include 

#include 

int main()

{

int _pipe[2];

int ret = pipe(_pipe);

if (ret == -1){

printf("create pipe error! errno code is : %d\n", errno);

return 1;

}

pid_t id = fork();

if (id 

printf("fork error!");

return 2;

}

else if (id == 0){ //child

close(_pipe[0]);

int i = 0;

char *_mesg_c = NULL;

while (i<10){

_mesg_c = "i am child!";

write(_pipe[1], _mesg_c, strlen(_mesg_c) + 1);

sleep(1);

i++;

}

close(_pipe[1]);

}

else{ //father

close(_pipe[1]);

char _mesg[100];

int j = 0;

while (j<100){

memset(_mesg, '\0', sizeof(_mesg));

int ret = read(_pipe[0], _mesg, sizeof(_mesg));

printf("%s : code is : %d\n", _mesg, ret);

j++;

}

if (waitpid(id, NULL, 0)

{

return 3;

}

}

return 0;

}

2. 如果有指向管道写端的文件描述符没关闭(管道写端的引用计数大于0),持有管道写

端的进程也没有向管道中写数据,这时有进程从管道读端读数据,那么管道中剩余的数

据都被读取后,再次read会阻塞,直到管道中有数据可读了才读取数据并返回。

测试代码:#include 

#include 

#include 

#include 

#include 

int main()

{

int _pipe[2];

int ret = pipe(_pipe);

if (ret == -1){

printf("create pipe error! errno code is : %d\n", errno);

return 1;

}

pid_t id = fork();

if (id 

printf("fork error!");

return 2;

}

else if (id == 0){ //child

close(_pipe[0]);

int i = 0;

char *_mesg_c = NULL;

while (i<20){

if (i 

_mesg_c = "i am child!";

write(_pipe[1], _mesg_c, strlen(_mesg_c) + 1);

}

sleep(1);

i++;

}

close(_pipe[1]);

}

else{ //father

close(_pipe[1]);

char _mesg[100];

int j = 0;

while (j<20){

memset(_mesg, '\0', sizeof(_mesg));

int ret = read(_pipe[0], _mesg, sizeof(_mesg));

printf("%s : code is : %d\n", _mesg, ret);

j++;

}

if (waitpid(id, NULL, 0)

{

return 3;

}

}

return 0;

}

3. 如果所有指向管道读端的文件描述符都关闭了(管道读端的引用计数等于0),这时有进

程向管道的写端write,那么该进程会收到信号SIGPIPE,通常会导致进程异常终。

测试代码:#include 

#include 

#include 

#include 

#include 

int main()

{

int _pipe[2];

int ret = pipe(_pipe);

if (ret == -1){

printf("create pipe error! errno code is : %d\n", errno);

return 1;

}

pid_t id = fork();

if (id 

printf("fork error!");

return 2;

}

else if (id == 0){ //child

close(_pipe[0]);

int i = 0;

char *_mesg_c = NULL;

while (i<20){

if (i 

_mesg_c = "i am child!";

write(_pipe[1], _mesg_c, strlen(_mesg_c) + 1);

}

sleep(1);

i++;

}

}

else{ //father

close(_pipe[1]);

char _mesg[100];

int j = 0;

while (j<3){

memset(_mesg, '\0', sizeof(_mesg));

int ret = read(_pipe[0], _mesg, sizeof(_mesg));

printf("%s : code is : %d\n", _mesg, ret);

j++;

}

close(_pipe[0]);

sleep(10);

if (waitpid(id, NULL, 0)

{

return 3;

}

}

return 0;

}

4. 如果有指向管道读端的文件描述符没关闭(管道读端的引用计数大于0),持有管道读

端的进程也没有从管道中读数据,这时有进程向管道写端写数据,那么在管道被写满时

再次write会阻塞,直到管道中有空位置了才写入数据并返回。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值