管道的一些特点如下:
它只能用于有亲缘关系的进程之间的通信(父子进程或兄弟进程)
它是一个半双工的通信模式,有固定读端、写端
管道可以看成一种特殊的文件,读写可以使用普通的read、write等函数
1.管道创建
prototype : int pipi(int fd[2])
input : fd[2],管道的两个文件描述符,成功创建管道之后可以直接操作
output: 0 —— success
-1 —— failure
例子:
int main(void)
{
int pipe_fd[2];
if (pipe(pipe_fd) < 0)
{
printf("pipe create error/n");
return -1;
}
else
{
printf("pipe create success/n");
}
close(pipe_fd[0]);
close(pipe_fd[1]);
}
2.管道读写
创建管道之后,有两个文件描述符,分别是fd[0]和fd[1],其中fd[0]是读取端,fd[1]是写入端。以下例子中,子进程作为读取端(需要关闭其写入端),父进程作为写入端(关闭其读取端)。贴出代码如下:
int main(void)
{
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char* p_wbuf;
int r_num;
memset(buf_r, 0, sizeof(buf_r));
if (pipe(pipe_fd) < 0) //创建管道
{
printf("pipe create error/n");
return -1;
}
if ((pid = fork()) == 0) //进入子进程
{
printf("/n");
close(pipe_fd[1]); //关闭写端口
sleep(2); //睡眠2s,等待父进程写
if ((r_num = read(pipe_fd[0], buf_r, 100)) > 0)
{ //读取管道内容
printf("%d numbers read from the pipe is %s/n", r_num, buf_r);
}
close(pipe_fd[0]); //关闭读端口
exit(0);
}
else if (pid > 0) //进入父进程
{
close(pipe_fd[0]); //关闭读端口
if (write(pipe_fd[1], "jarvis", 6) != -1)
{ //分两次向管道写入
printf("parent write_1 jarvis!/n");
}
if (write(pipe_fd[1], "hello", 5) != -1)
{
printf("parent write_2 hello!/n");
}
close(pipe_fd[1]);
sleep(3);
waitpid(pid, NULL, 0); //等待子进程结束
exit(0);
}
}
编译并运行,终端打印信息如下:
OK,实现管道通信!