概述
管道是什么:
1. 管道只能用于具备亲缘关系的进程之间通讯。
2.管道是一种单工或者说半双工的通讯方式,传递信息的方向是固定的,只能由一端传递到另外一端。
头文件及函数原型:
#include
int pipe(int fd[2]);
当用pipe 建立管道后,两个文件描述符fd[0],fd[1]就能够使用了,其中fd[0]用于读取,fd[1]用于写入。调用管道pipe返回值0表示成功,返回值-1表示失败。
pipe函数建立管道后,接着fork出子进程,子进程继承父进程管道。
代码举例来看:函数
#include
#include
#include
#include
#include
#define MAX_DATA_LEN 256
#define DELAY_TIME 1
int main() {
pid_t pid;
char buf[MAX_DATA_LEN];
const char *data="Pipe Test";
int real_read,real_write;
int pipe_fd[2];
memset((void*)buf,0,sizeof(buf));
if(pipe(pipe_fd)<0){
perror("Pipe create error!n");
exit(1);
}
if ((pid=fork())<0) {
perror("Fork error!n");
exit(1);
} else if (pid==0) {
close(pipe_fd[1]);
sleep(DELAY_TIME*3);
if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
printf("Child receive %d bytes from pipe: '%s'.n",real_read,buf);
}
close(pipe_fd[0]);
exit(0);
} else {
close(pipe_fd[0]);
sleep(DELAY_TIME);
if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
printf("Parent write %d bytes into pipe: '%s'.n",real_write,data);
}
close(pipe_fd[1]);
waitpid(pid,NULL,0);
exit(0);
}
} 复制代码
运行输出:
Parent write 9 bytes into pipe: 'Pipe Test'.
Child receive 9 bytes from pipe: 'Pipe Test'.spa
上述代码只能从父进程向子进程传递,如何从子进程向父进程传递呢?咱们看到父进程关闭了管道读取端“close(pipe_fd[0]);”,子进程关闭了管道写入端“close(pipe_fd[1]);”,若是取消关闭这两个端,是否可以实现子进程向父进程传递呢。
父进程先发送,子进程接收,而后子进程再发送,父进程再接收,实现全双工互相通讯,指望运行结果以下:
Parent write 9 bytes into pipe: 'Pipe Test'.
Child receive 9 bytes from pipe: 'Pipe Test'.
Child write 9 bytes from pipe: 'Pipe Test'.
Parent receive 9 bytes from pipe: 'Pipe Test'.
修改代码以下:
code
#include
#include
#include
#include
#include
#define MAX_DATA_LEN 256
#define DELAY_TIME 1
int main() {
pid_t pid;
char buf[MAX_DATA_LEN];
const char *data="Pipe Test";
int real_read,real_write;
int pipe_fd[2];
memset((void*)buf,0,sizeof(buf));
if(pipe(pipe_fd)<0){
perror("Pipe create error!n");
exit(1);
}
if ((pid=fork())<0) {
perror("Fork error!n");
exit(1);
} else if (pid==0) {
//close(pipe_fd[1]);
sleep(DELAY_TIME*3);
if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
printf("Child receive %d bytes from pipe: '%s'.n",real_read,buf);
}
if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
printf("Child write %d bytes into pipe: '%s'.n",real_write,data);
}
close(pipe_fd[0]);
exit(0);
} else {
//close(pipe_fd[0]);
sleep(DELAY_TIME);
if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
printf("Parent write %d bytes into pipe: '%s'.n",real_write,data);
}
if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
printf("Parent receive %d bytes from pipe: '%s'.n",real_read,buf);
}
close(pipe_fd[1]);
waitpid(pid,NULL,0);
exit(0);
}
} 复制代码
可是实际运行以下:
Parent write 9 bytes into pipe: 'Pipe Test'.
Parent receive 9 bytes from pipe: 'Pipe Test'.
能够看到,父进程发送的数据被父进程本身接收了,子进程读不到数据被阻塞了。显然这种方法不行。继承
由于管道是单工的,只能固定从一个方向传递到另外一个方向。
要实现互相通讯,一个管道是不行的,能够建立两个管道,一个管道是父写子读,另外一个是子写父读。
代码以下:进程
#include
#include
#include
#include
#include
#define MAX_DATA_LEN 256
#define DELAY_TIME 1
int main() {
pid_t pid;
char buf[MAX_DATA_LEN];
const char *data="Pipe Test";
int real_read,real_write;
int pipe_fd[2],pipe_fd2[2];
memset((void*)buf,0,sizeof(buf));
if(pipe(pipe_fd)<0){
perror("Pipe create error!n");
exit(1);
}
if(pipe(pipe_fd2)<0){
perror("Pipe create error!n");
exit(1);
}
if ((pid=fork())<0) {
perror("Fork error!n");
exit(1);
} else if (pid==0) {
close(pipe_fd[1]);
close(pipe_fd2[0]);
sleep(DELAY_TIME*3);
if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
printf("Child receive %d bytes from pipe: '%s'.n",real_read,buf);
}
if ((real_write=write(pipe_fd2[1],data,strlen(data)))>0) {
printf("Child write %d bytes into pipe: '%s'.n",real_write,data);
}
close(pipe_fd[0]);
close(pipe_fd2[1]);
exit(0);
} else {
close(pipe_fd[0]);
close(pipe_fd2[1]);
sleep(DELAY_TIME);
if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
printf("Parent write %d bytes into pipe: '%s'.n",real_write,data);
}
if ((real_read=read(pipe_fd2[0],buf,MAX_DATA_LEN))>0) {
printf("Parent receive %d bytes from pipe: '%s'.n",real_read,buf);
}
close(pipe_fd[1]);
close(pipe_fd2[0]);
waitpid(pid,NULL,0);
exit(0);
}
} 复制代码
运行结果:
Parent write 9 bytes into pipe: 'Pipe Test'.
Child receive 9 bytes from pipe: 'Pipe Test'.
Child write 9 bytes into pipe: 'Pipe Test'.
Parent receive 9 bytes from pipe: 'Pipe Test'.
能够看到,建立了两个管道 “int pipe_fd[2],pipe_fd2[2];”,
pipe_fd 是父写子读,pipe_fd2是子写父读,经过两个管道,实现了进程的全双工互相通讯。ip
最后
以上就是还单身红酒为你收集整理的pipe 半双工_linux进程间通讯之管道(无名管道pipe)实现全双工双向通讯的全部内容,希望文章能够帮你解决pipe 半双工_linux进程间通讯之管道(无名管道pipe)实现全双工双向通讯所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复