a.设计两个独立进程:发送进程和接收进程,通过共享内存进行进程通信,以"end"作为结束消息。采用先执行发送进程,后执行接收进程的方式实现两个进程的同步关系。
时间: 2024-03-22 08:38:53 浏览: 20
下面是一个简单的实现示例:
发送进程代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024
int main() {
key_t key = ftok(".", 's'); // 生成共享内存的 key
int shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666); // 创建共享内存
if (shmid == -1) {
perror("shmget failed"); // 创建共享内存失败
exit(EXIT_FAILURE);
}
char *shmaddr = shmat(shmid, NULL, 0); // 将共享内存映射到当前进程的地址空间中
if (shmaddr == (char *) -1) {
perror("shmat failed"); // 映射共享内存失败
exit(EXIT_FAILURE);
}
while (1) {
char buffer[256];
printf("Enter message: ");
fgets(buffer, 256, stdin); // 从标准输入读取消息
if (strncmp(buffer, "end", 3) == 0) { // 如果消息为 "end",则跳出循环
break;
}
strncpy(shmaddr, buffer, SHM_SIZE); // 将消息写入共享内存中
printf("Sent message: %s\n", buffer);
while (*shmaddr != '*') { // 等待接收进程读取共享内存中的消息
sleep(1);
}
}
strncpy(shmaddr, "end", SHM_SIZE); // 将 "end" 消息写入共享内存中
printf("Sent message: end\n");
shmdt(shmaddr); // 解除共享内存映射
shmctl(shmid, IPC_RMID, NULL); // 删除共享内存
return 0;
}
```
接收进程代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024
int main() {
key_t key = ftok(".", 's'); // 使用与发送进程相同的 key
int shmid = shmget(key, SHM_SIZE, 0666); // 获取共享内存
if (shmid == -1) {
perror("shmget failed"); // 获取共享内存失败
exit(EXIT_FAILURE);
}
char *shmaddr = shmat(shmid, NULL, 0); // 将共享内存映射到当前进程的地址空间中
if (shmaddr == (char *) -1) {
perror("shmat failed"); // 映射共享内存失败
exit(EXIT_FAILURE);
}
while (1) {
while (*shmaddr == '*') { // 等待发送进程写入共享内存中的消息
sleep(1);
}
if (strncmp(shmaddr, "end", 3) == 0) { // 如果消息为 "end",则跳出循环
break;
}
printf("Received message: %s", shmaddr); // 输出共享内存中的消息
strncpy(shmaddr, "*", SHM_SIZE); // 将 "*" 写入共享内存中,表示已读取消息
}
printf("Received message: end\n");
shmdt(shmaddr); // 解除共享内存映射
return 0;
}
```
这两个进程可以通过共享内存进行通信。发送进程将消息写入共享内存中,并等待接收进程读取共享内存中的消息。接收进程从共享内存中读取消息,并将 "*" 写入共享内存中,表示已读取消息。在发送进程发送 "end" 消息后,接收进程读取到 "end" 消息后退出循环,实现两个进程的同步关系。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)