使用系统调用pipe( )建立一个管道,然后使用系统调用fork( )创建2个子进程p1和p2。这2个子进程分别向管道中写入字符串:“Child process p1 is sending message!”和“Child process p2 is sending message!”,而父进程则从管道中读出来自两个子进程的信息,并显示在屏幕上。流程图
时间: 2024-03-04 16:47:59 浏览: 24
以下是此问题的流程图:
```flow
st=>start: Start
op1=>operation: 创建管道 pipe()
cond1=>condition: pipe() 是否成功?
op2=>operation: 创建子进程 p1
op3=>operation: 创建子进程 p2
cond2=>condition: 子进程 p1?
cond3=>condition: 子进程 p2?
op4=>operation: p1 向管道中写入信息
op5=>operation: p2 向管道中写入信息
op6=>operation: 父进程从管道中读取信息
op7=>operation: 打印信息
e=>end: End
st->op1->cond1
cond1(yes)->op2->cond2
cond1(no)->e
cond2(yes)->op4->op3->cond3
cond2(no)->cond3
cond3(yes)->op5->op6->op7->cond1
cond3(no)->op6->op7->cond1
```
流程图中,首先开始,然后创建一个管道,判断管道是否创建成功。如果创建成功,就通过fork()系统调用创建两个子进程p1和p2,然后判断当前进程是否为子进程p1或p2。如果是p1或p2,则向管道中写入信息,否则父进程从管道中读取信息并打印。最后结束程序。
相关问题
使用系统调用pipe( )建立一个管道,然后使用系统调用fork( )创建2个子进程p1和p2。这2个子进程分别向管道中写入字符串:“Child process p1 is sending message!”和“Child process p2 is sending message!”,而父进程则从管道中读出来自两个子进程的信息,并显示在屏幕上。写出程序说明和程序流程图
程序说明:
该程序使用系统调用pipe()函数建立了一个管道,然后使用系统调用fork()函数创建了2个子进程p1和p2。这2个子进程分别向管道中写入字符串:“Child process p1 is sending message!”和“Child process p2 is sending message!”,而父进程则从管道中读出来自两个子进程的信息,并显示在屏幕上。
程序流程图:
```
START
|
v
调用pipe()函数,建立一个管道
|
v
调用fork()函数,创建2个子进程p1和p2
|
|---p1: 向管道中写入字符串:“Child process p1 is sending message!”
|
|---p2: 向管道中写入字符串:“Child process p2 is sending message!”
|
v
父进程从管道中读出来自两个子进程的信息,并显示在屏幕上
|
END
```
程序代码:
编写程序实现进程的管道通信。用系统调用pipe( )建立一管道,二个子进程P1和P2分别向管道各写一句话:(可以输出其他消息,消息自定)
```c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd[2];
pid_t pid;
if (pipe(fd) < 0) { // 建立管道
fprintf(stderr, "pipe error\n");
return 1;
}
pid = fork(); // 创建子进程1
if (pid < 0) {
fprintf(stderr, "fork error\n");
return 1;
} else if (pid == 0) { // 子进程1
close(fd[0]); // 关闭读端
char *msg = "Hello, I am P1.";
write(fd[1], msg, strlen(msg) + 1); // 写入消息
close(fd[1]); // 关闭写端
return 0;
} else { // 父进程
pid = fork(); // 创建子进程2
if (pid < 0) {
fprintf(stderr, "fork error\n");
return 1;
} else if (pid == 0) { // 子进程2
close(fd[1]); // 关闭写端
char buf[50];
read(fd[0], buf, sizeof(buf)); // 读取消息
printf("P2 received: %s\n", buf);
close(fd[0]); // 关闭读端
return 0;
} else { // 父进程
close(fd[0]); // 关闭读端
close(fd[1]); // 关闭写端
wait(NULL); // 等待子进程1结束
wait(NULL); // 等待子进程2结束
return 0;
}
}
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![mp4](https://img-home.csdnimg.cn/images/20210720083504.png)