按每一行解释如下代码:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/sem.h> #include <time.h> #define MSG_SIZE 100 struct sembuf sem_wait = {0, -1, SEM_UNDO}; struct sembuf sem_signal = {0, 1, SEM_UNDO}; int pfd[2]; int semid; void send_msg(int id) { srand(time(NULL) + id); int len = rand() % MSG_SIZE + 1; char msg[len]; for (int i = 0; i < len; i++) { msg[i] = 'A' + rand() % 26; } msg[len - 1] = '\0'; printf("Child %d sends message: %s\n", id, msg); semop(semid, &sem_wait, 1); write(pfd[1], msg, strlen(msg) + 1); semop(semid, &sem_signal, 1); } int main() { if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666); if (semid == -1) { perror("semget"); exit(EXIT_FAILURE); } if (semctl(semid, 0, SETVAL, 1) == -1) { perror("semctl"); exit(EXIT_FAILURE); } for (int i = 0; i < 3; i++) { pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0) { send_msg(i); exit(EXIT_SUCCESS); } } for (int i = 0; i < 3; i++) { wait(NULL); } char msg[MSG_SIZE]; int total_bytes = 0; while (total_bytes < MSG_SIZE * 3) { semop(semid, &sem_wait, 1); int n_bytes = read(pfd[0], msg + total_bytes, MSG_SIZE * 3 - total_bytes); if (n_bytes == -1) { perror("read"); exit(EXIT_FAILURE); } total_bytes += n_bytes; semop(semid, &sem_signal, 1); } printf("Parent receives message: %s\n", msg); close(pfd[0]); close(pfd[1]); if (semctl(semid, 0, IPC_RMID) == -1) { perror("semctl"); exit(EXIT_FAILURE); } return 0; }
时间: 2023-12-03 07:43:22 浏览: 43
这是一个使用管道和信号量实现进程间通信的程序,主要分为以下几个部分:
1. 引入需要用到的头文件。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <time.h>
```
2. 定义宏 MSG_SIZE,表示消息的最大长度。
```
#define MSG_SIZE 100
```
3. 定义信号量操作结构体 sembuf,包含三个字段:信号量编号、信号量操作类型和操作标志。
```
struct sembuf sem_wait = {0, -1, SEM_UNDO};
struct sembuf sem_signal = {0, 1, SEM_UNDO};
```
4. 定义管道文件描述符和信号量标识符。
```
int pfd[2];
int semid;
```
5. 定义函数 send_msg,用于向管道中写入随机生成的消息。
```
void send_msg(int id) {
srand(time(NULL) + id);
int len = rand() % MSG_SIZE + 1;
char msg[len];
for (int i = 0; i < len; i++) {
msg[i] = 'A' + rand() % 26;
}
msg[len - 1] = '\0';
printf("Child %d sends message: %s\n", id, msg);
semop(semid, &sem_wait, 1);
write(pfd[1], msg, strlen(msg) + 1);
semop(semid, &sem_signal, 1);
}
```
6. 定义主函数,首先创建管道和信号量,然后创建三个子进程,分别调用 send_msg 函数向管道中写入消息。等待子进程结束后,父进程从管道中读取三个消息,拼接后输出。
```
int main() {
// 创建管道
if (pipe(pfd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
// 创建信号量
semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
if (semid == -1) {
perror("semget");
exit(EXIT_FAILURE);
}
if (semctl(semid, 0, SETVAL, 1) == -1) {
perror("semctl");
exit(EXIT_FAILURE);
}
// 创建子进程并向管道中写入消息
for (int i = 0; i < 3; i++) {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
send_msg(i);
exit(EXIT_SUCCESS);
}
}
// 等待子进程结束
for (int i = 0; i < 3; i++) {
wait(NULL);
}
// 从管道中读取消息并拼接
char msg[MSG_SIZE];
int total_bytes = 0;
while (total_bytes < MSG_SIZE * 3) {
semop(semid, &sem_wait, 1);
int n_bytes = read(pfd[0], msg + total_bytes, MSG_SIZE * 3 - total_bytes);
if (n_bytes == -1) {
perror("read");
exit(EXIT_FAILURE);
}
total_bytes += n_bytes;
semop(semid, &sem_signal, 1);
}
// 输出拼接后的消息
printf("Parent receives message: %s\n", msg);
// 关闭管道和删除信号量
close(pfd[0]);
close(pfd[1]);
if (semctl(semid, 0, IPC_RMID) == -1) {
perror("semctl");
exit(EXIT_FAILURE);
}
return 0;
}
```
相关推荐
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.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)
![](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)