本文记录的是进程间通信之无名管道
pipe ( 建立无名管道 )
头文件:
#include
定义函数:
int pipe(int pipefd[2]);
参数分析:
pipefd[0] --> 接收管道描述符
pipefd[1] --> 写管道描述符
返回值:
若成功则返回 0,
否则返回-1, 错误原因存于 errno 中.
#include
#include
int main(int argc, char const *argv[])
{
int pipe_fd[2]; //两个文件描述符
//int pipe(int pipefd[2]);
int ret = pipe( pipe_fd );
if ( ret < 0 ) {
printf("create pipe failure\n");
perror("create pipe error");
return -1;
}
printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
return 0;
}
把"hello linux!" 写进无名管道,然后通过read函数读取出来。
#include
#include
int main(int argc, char const *argv[])
{
char buffer[20];
int pipe_fd[2]; //两个文件描述符
//int pipe(int pipefd[2]);
int ret_pipe = pipe( pipe_fd );
if ( ret_pipe < 0 ) {
printf("create pipe failure\n");
perror("create pipe error");
return -1;
}
printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
//把hello linux!写入无名管道
int ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
if( ret_write < 0 ) {
printf("写入失败!\n");
perror("write error");
return -1;
}
read(pipe_fd[0], buffer, sizeof(buffer) );
printf("%s\n", buffer);
close(pipe_fd[0]);
close(pipe_fd[1]);
return 0;
}
代码如下(示例):
#include
#include
#include
int main(int argc, char const *argv[])
{
char buffer[20];
int pipe_fd[2]; //两个文件描述符
//int pipe(int pipefd[2]);
int ret_pipe = pipe( pipe_fd );
if ( ret_pipe < 0 ) {
printf("create pipe failure\n");
perror("create pipe error");
return -1;
}
printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
//把hello linux!写入无名管道
int ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
if( ret_write < 0 ) {
printf("写入失败!\n");
perror("write error");
return -1;
}
//The first read
read(pipe_fd[0], buffer, sizeof(buffer) );
printf("%s\n", buffer);
//The second read void *memset(void *s, int c, size_t n);
memset(buffer, 0, 20); //前20个字节清零
read(pipe_fd[0], buffer, sizeof(buffer) );
printf("%s\n", buffer);
close(pipe_fd[0]);
close(pipe_fd[1]);
return 0;
}
从执行结果来看,第二次读取无名管道的内容的时候,并没有成功,终端一直在执行着。
/************************************************************************
*
* 文件名:pipe_03.c
*
* 功能:测试无名管道能写入多少个字节
*
* 创建人:LiZhenhao
*
* 时间:2021年10月20日19:02:56
*
* 版本号:3.0
*
* 结果:无名管道最多能写入65507个字节
*
************************************************************************/
#include
#include
#include
int main(int argc, char const *argv[])
{
char buffer[20];
int pipe_fd[2]; //两个文件描述符
int i = 0;
//创建无名管道
int ret_pipe = pipe( pipe_fd );
if ( ret_pipe < 0 ) {
printf("create pipe failure\n");
perror("create pipe error");
return -1;
}
printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
//把hello linux!写入无名管道
int ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
printf("ret_write = %d\n", ret_write);
while( ret_write != -1 ) {
ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
i++;
printf("total = %d\n", i);
}
//关闭文件描述符
close(pipe_fd[0]);
close(pipe_fd[1]);
return 0;
}
从上面界面的结果来看,执行次数是5039次,然后写入的字节一次总共写了13个字节,通过计算13*5039 = 65,507个字节
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
int status;
//创建无名管道
int pipefd[2];
if( 0 == pipe(pipefd) ) {
printf("创建无名管道成功!\n");
}
//创建子进程
pid_t pid = fork();
if(pid > 0) {
printf("我是父进程,ID:%d\n", getpid());
ssize_t ret = write (pipefd[1], "hello linux!", strlen("hello linux!"));
printf("成功写入%ld个字节进无名管道\n", ret);
wait(&status);
if( WIFEXITED(status) ) {
printf("子进程正常退出,退出值为:%d\n", WEXITSTATUS(status));
}
sleep(1);
} else if(0 == pid){
printf("我是子进程,ID:%d\n", getpid());
char re_buf[20] = {0};
ssize_t ret = read(pipefd[0], re_buf, sizeof(re_buf));
printf("成功进从无名管道读取%ld个字节, 内容是:%s\n", ret, re_buf);
//exit(1);
}
return 0;
}
从上面结果分析:父子进程同时进行,父进程写入数据到无名管道,子进程成功从无名管道读取数据
/* 实习父子进程通信 */
#include
#include
#include
#include
#include
#include
//char* msg_buf = NULL;
int main(int argc, char const *argv[])
{
int status;
//msg_buf = (char*)malloc(sizeof(char) * 1024);
char msg_buf[1024];
//创建无名管道
int pipefd[2];
if( pipe(pipefd) < 0) {
printf("创建无名管道失败!\n");
perror("create pipe error");
return -1;
}
//创建子进程
pid_t pid = fork();
if(pid > 0) { //父进程
printf("我是父进程,ID:%d\n", getpid());
usleep(1000);
while(1) {
printf("父进程请输入:\n");
memset(msg_buf, 0, 1024);
fgets(msg_buf, 1024, stdin);
ssize_t ret = write (pipefd[1], msg_buf, strlen(msg_buf)+1);
printf("父进程成功写入%ld个字节进无名管道\n", ret);
usleep(1000);
ret = read(pipefd[0], msg_buf, sizeof(msg_buf));
printf("父进程成功进从无名管道读取%ld个字节, 内容是:%s\n", ret, msg_buf);
}
wait(&status);
close(pipefd[0]);
close(pipefd[1]);
} else if(0 == pid){ //子进程
printf("我是子进程,ID:%d\n", getpid());
ssize_t ret;
while(1) {
ret = read(pipefd[0], msg_buf, 1024);
printf("子进程成功进从无名管道读取%ld个字节, 内容是:%s\n", ret, msg_buf);
printf("子进程请输入:\n");
memset(msg_buf, 0, 1024);
fgets(msg_buf, 1024, stdin);
ssize_t ret = write (pipefd[1], msg_buf, strlen(msg_buf) + 1);
printf("子进程成功写入%ld个字节进无名管道\n", ret);
usleep(1000);
}
close(pipefd[0]);
close(pipefd[1]);
//exit(1);
}
return 0;
}
父进程和子进程在写完内容都无名管道以后,要usleep等待一下,不然会出现父进程写,父进程读这种情况。同理,子进程也一样。
1、无名管道的通信方式是要有血缘关系的进程才能使用,父子进程对同一管道的通信
2、要在创建父子进程fork()之前,先创建无名管道,不然的话,父进程写完数据到无名管道,子进程会发生读阻塞。