管道是一种最基本的IPC机制,由pipe函数创建:#include
int pipe(int filedes[2]);
调用pipe函数就是在内核区开辟一块缓冲区(称为管道)。filedes[0]指向管道的读端,filedes[1]指向管道的写端。管道实际上就是一个打开的文件。pipe函数成功返回0,失败返回-1.
如何用管道实现两个进程间的通信?
1.父进程调用pipe函数开辟管道,得到两个文件描述符指向管道的两端。
2.父进程调用fork()创建子进程,那么子进程也有两个文件描述符指向该管道。
3.父进程关闭管道读端,子进程关闭管道写端。父进程可以往管道写,子进程可以从管道
读,管道是环形队列实现的,数据从写端流,从读端流出,这样就实现了进程间通信。
代码实现:#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会阻塞,直到管道中有空位置了才写入数据并返回。