无名管道的特点:
1.适用场景:只能用于具有亲缘关系的两个进程之间;
2.通信模式:半双工模式,具有固定的读端(0)和写端(1);
3.读写方式:用文件io,不支持lseek;
4.读阻塞:当管道中无数据时;
写阻塞:当管道中写满数据时;
5.管道大小:无名管道大小64k;
6.管道破裂:关闭读端,仍然想管道中写入数据时;
对管道进行写的进程会收到内核发来SIGPIPE的信号,可以捕获;
来,我们用无名管道先写一个回显程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define N 32
int main(int argc, const char *argv[])
{
int pipefd[2];
int pid;
if(pipe(pipefd) < 0)
{
perror("pipe error");
exit(1);
}
if( (pid = fork()) < 0)
{
perror("fork error");
exit(1);
}
else if(pid == 0)
{
char buf[N];
int ret;
close(pipefd[0]);
puts("im chd start");
while(fgets(buf, N, stdin) != NULL)
{
write(pipefd[1], buf, N);
if(strncmp(buf, "quit", 4) == 0)
{
break;
}
}
puts("im chd end");
exit(2);
}
else if(pid > 0)
{
int chdstatus, out_pid, ret;
char buf[N];
close(pipefd[1]);
puts("im farther start");
//父进程在这里会阻塞,应为管道的特点中有一条“当管道中无数据时,会读阻塞”,
//程序启动时,如果父进程现将占到系统资源执行到这里仍然会阻塞,因为管道中没
//有数据。所以父进程要想执行,必须依赖于子进程向管道中写入数据;
//但是如果是读取一个文件,文件为空,此时read等于0,则不会执行while循环;
//在这里主要提一下管道无数据,会读阻塞;
while(read(pipefd[0], buf, N) > 0)
{
puts("im if");
if(strncmp(buf, "quit", 4) == 0)
{
exit(1);;
}
fputs(buf, stdout);
}
puts("im farther end");
out_pid = waitpid(pid, &chdstatus, 0);
printf("outpid = %d\n", out_pid);
if(WIFEXITED(chdstatus))
{
printf("exit_num = %d\n", WEXITSTATUS(chdstatus));
}
exit(0);
}
return 0;
}
下面我们用无名管道来实现一个文件服务器的功能