原理
管道是一个特殊的共享文件,需要通信的进程双方,通过连接管道的读写端口,对该共享文件进行读写来进行数据交换。创建管道采用函数Pipe系统调用。
- pipe(int fd[])创建管道的函数
- fd[0]为管道的读出端,fd[1]为管道的写入端
- 使用普通的函数调用read()和write()访问管道
普通的管道是单向的,只允许单向通信,如果需要双向通信,就需要采用两个管道。
普通管道只能由创建进程所访问。通常情况下,父进程创建一个管道,并使用它与其子进程进行通信,子进程继承父进程的打开文件,因此也就继承了父进程创建的管道。
任务一
使用Pipe创建管道,创建一个子进程,子进程向父进程发送消息“I am your son!”,父进程接收到子进程的消息后,显示在屏幕上,并向子进程发送“I am your father!”。子进程接收到父进程的消息并显示在屏幕上。
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
#include <signal.h>
int main () {
int fd_father[2];
int fd_child[2];
pipe(fd_father);
pipe(fd_child);
int pid1, pid2;
char str_father[100];
char str_child[100];
while ((pid1 = fork()) == -1);
if (pid1 == 0) {
lockf(fd_child[1], 1, 0);
write(fd_child[1], "I am your son!", 15);
lockf(fd_child[1], 0, 0);
lockf(fd_father[0], 1, 0);
read(fd_father[0], str_father, 50);
printf("Receives messages from the father process:%s\n", str_father);
lockf(fd_father[0], 0, 0);
exit(0);
} else {
lockf(fd_father[1], 1, 0);
write(fd_father[1], "I am your father!", 18);
lockf(fd_father[1], 0, 0);
lockf(fd_child[0], 1, 0);
read(fd_child[0], str_child, 50);
printf("Receives messages from the child process:%s\n", str_child);
lockf(fd_child[0], 0, 0);
wait(0);
}
}
由于管道是单向传输的,但这里的要求是父进程与子进程的双向通信传输,所以这里我采用了建立两个管道,一个由父进程传输给子进程,一个由子进程传输给父进程,从而实现双向通信传输。
任务二
使用系统调用 pipe ( ) 建立一条管道线;两个子进程 P1 和 P2 分别向管道各写一句话
Child 1 is sending a message!
Child 2 is sending a message!
而父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
#include <signal.h>
int main () {
int fd[2];
pipe(fd);
int pid1,pid2;
char str[100];
while ((pid1 = fork()) == -1);
if (pid1 == 0) {
lockf(fd[1], 1, 0);
write(fd[1], "Child 1 is sending a message!", 30);
lockf(fd[1], 0, 0);
exit(0);
} else {
while ((pid2 = fork()) == -1);
if(pid2 == 0) {
lockf(fd[1], 1, 0);
write(fd[1], "Child 2 is sending a message!", 30);
lockf(fd[1], 0, 0);
exit(0);
} else {
lockf(fd[0], 1, 0);
read(fd[0], str, 50);
printf("Receives messages from the child 1 process:%s\n", str);
lockf(fd[0], 0, 0);
}
lockf(fd[0], 1, 0);
read(fd[0], str, 50);
printf("Receives messages from the child 2 process:%s\n", str);
lockf(fd[0], 0, 0);
wait(0);
}
}
任务三
能否创建一条管道,实现两个子进程之间的通信,如果可以,请实现并给出代码。如果不可以,请说明理由
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
#include <signal.h>
int main () {
int fd[2];
pipe(fd);
int pid1, pid2;
char str[100];
while ((pid1 = fork()) == -1);
if (pid1 == 0) {
lockf(fd[1], 1, 0);
write(fd[1], "I am child 1 process", 30);
lockf(fd[1], 0, 0);
exit(0);
} else {
waitpid(pid1, NULL, 0);
while ((pid2 = fork()) == -1);
if (pid2 == 0) {
lockf(fd[0], 1, 0);
read(fd[0], str, 50);
printf("The child 2 process receives messages from the child 1 process:%s\n", str);
lockf(fd[0], 0, 0);
exit(0);
} else {
;
}
wait(0);
}
}
管道是一端到另一端之间的连接,相当于是一个进程与另一个进程之间的连接,是可以实现两个子进程之间的通信的。为了实现这一点,我将父进程的读出端和写入端分别给了两个不同的子进程,这样两个子进程就可以利用管道进行通信。