使用C语言设计三个程序,程序A通过消息队列将不同的内容传输给程序B和程序C,程序B和程序C将收到的内容进行打印。
时间: 2024-03-20 07:41:52 浏览: 20
好的,以下是使用C语言设计的示例程序,使用消息队列进行进程间通信,具体实现如下:
程序A:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// 消息队列键值
#define MQ_KEY 1234
// 消息结构体
struct message {
long type;
char text[100];
};
int main() {
// 创建消息队列
int mq_id = msgget(MQ_KEY, IPC_CREAT | 0666);
if (mq_id == -1) {
perror("Failed to create message queue");
exit(EXIT_FAILURE);
}
// 创建进程B
pid_t pid_b = fork();
if (pid_b == -1) {
perror("Failed to fork process B");
exit(EXIT_FAILURE);
} else if (pid_b == 0) {
execl("./process_b", "process_b", NULL);
perror("Failed to exec process B");
exit(EXIT_FAILURE);
}
// 创建进程C
pid_t pid_c = fork();
if (pid_c == -1) {
perror("Failed to fork process C");
exit(EXIT_FAILURE);
} else if (pid_c == 0) {
execl("./process_c", "process_c", NULL);
perror("Failed to exec process C");
exit(EXIT_FAILURE);
}
// 向消息队列发送消息
srand(time(NULL));
for (int i = 0; i < 10; i++) {
struct message msg;
msg.type = i % 2 + 1; // 根据奇偶性选择发送给进程B或进程C
sprintf(msg.text, "Message from A: %d", rand() % 100);
if (msgsnd(mq_id, &msg, sizeof(msg.text), 0) == -1) {
perror("Failed to send message");
exit(EXIT_FAILURE);
}
printf("%s\n", msg.text);
sleep(1);
}
// 发送结束信号
struct message msg;
msg.type = 0;
if (msgsnd(mq_id, &msg, sizeof(msg.text), 0) == -1) {
perror("Failed to send end message");
exit(EXIT_FAILURE);
}
// 等待进程B、C结束
waitpid(pid_b, NULL, 0);
waitpid(pid_c, NULL, 0);
// 删除消息队列
if (msgctl(mq_id, IPC_RMID, NULL) == -1) {
perror("Failed to remove message queue");
exit(EXIT_FAILURE);
}
return 0;
}
```
程序B:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// 消息队列键值
#define MQ_KEY 1234
// 消息结构体
struct message {
long type;
char text[100];
};
int main() {
// 获取消息队列ID
int mq_id = msgget(MQ_KEY, 0666);
if (mq_id == -1) {
perror("Failed to get message queue");
exit(EXIT_FAILURE);
}
// 接收消息并打印
struct message msg;
while (msgrcv(mq_id, &msg, sizeof(msg.text), 1, 0) != -1) {
printf("Message received by B: %s\n", msg.text);
}
return 0;
}
```
程序C:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// 消息队列键值
#define MQ_KEY 1234
// 消息结构体
struct message {
long type;
char text[100];
};
int main() {
// 获取消息队列ID
int mq_id = msgget(MQ_KEY, 0666);
if (mq_id == -1) {
perror("Failed to get message queue");
exit(EXIT_FAILURE);
}
// 接收消息并打印
struct message msg;
while (msgrcv(mq_id, &msg, sizeof(msg.text), 2, 0) != -1) {
printf("Message received by C: %s\n", msg.text);
}
return 0;
}
```
程序A创建了一个消息队列,并创建了进程B和进程C,然后向消息队列发送10个随机数,每个随机数根据奇偶性选择发送给进程B或进程C,最后发送结束信号,等待进程B和进程C结束。进程B和进程C从消息队列中获取消息,并将其打印出来。
当您编译并运行程序A时,它将在控制台上输出10个随机数,进程B和进程C将同时收到这些消息,并在各自的控制台上打印它们。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)