- 无名管道:
- 首先它是
内核空间的实现机制; - 然后
只能用于亲缘进程间通信; - 它在内核所占的
大小是64KB; - 它采用
半双工的通信方式; - 请勿使用
lseek函数; - 读写特点:
- 若读端
存在写管道,那么有多少数据,就写多少数据,直到无名管道写满为止,此时会出现写阻塞,当无名管道出现新的4KB空间,写操作就会解除阻塞; - 若读端
不存在写管道,会出现管道破裂的情况; - 若写端
存在读管道,那么有多少数据,就读多少数据,没有数据的时候,会出现阻塞等待; - 若写端
不存在读管道,有多少数据,就读多少,没有数据的时候,就会立即返回,即非阻塞的状态; - 创建无名管道(pipe函数):
#include
int pipe(int pipefd[2]);
- 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
- 实例要求:
- 创建一个进程,要求使用无名管道,实现
父进程写,子进程读的操作; - 示例代码:
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
int pipe_fd[2] = {0};
if(-1 == pipe(pipe_fd))
{
perror("pipe error");
exit(-1);
}
pid_t pid = 0;
if(-1 == (pid = fork()))
{
perror("fork error");
exit(-1);
}
else if(0 < pid)
{
close(pipe_fd[0]);
char buf[128] = {0};
while(true)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
write(pipe_fd[1],buf,sizeof(buf));
if(!strncmp(buf,"quit",4))
{
exit(-1);
}
}
sleep(2);
wait(NULL);
}
else if(0 == pid)
{
close(pipe_fd[1]);
char buf[128] = {0};
while(true)
{
memset(buf,0,sizeof(buf));
read(pipe_fd[0],buf,sizeof(buf));
if(!strncmp(buf,"quit",4))
{
close(pipe_fd[0]);
exit(-1);
}
printf("父进程发来的消息[%s]\n",buf);
}
}
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
linux@ubuntu:~$ gcc pipe.c
linux@ubuntu:~$ ./a.out
hello beijing
父进程发来的消息[hello beijing]
i love china
父进程发来的消息[i love china]
quit
linux@ubuntu:~$
- 总结:
- 为了
防止误操作,在父进程和子进程里,分别关闭读端和写端;