编写一个程序,使用系统调用shmget(), shmat(), shmdt(), shmctl()实现进程通过共享内存的通信:一个子进程SERVER将共享内存首字节置为-1,当首字节值不为-1时读取这个值并再次将其置为-1;另一个子进程CLIENT在首字节为-1时将首字节置为9-0中的一个数字。(SHMKEY取值75,共享区的长度取1024)
时间: 2024-02-21 15:00:40 浏览: 26
好的,下面是代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#define SHMKEY 75
#define SHMSIZE 1024
int main() {
int shmid;
char *shmaddr;
// 创建共享内存
if ((shmid = shmget(SHMKEY, SHMSIZE, IPC_CREAT | 0666)) == -1) {
perror("shmget");
exit(1);
}
// 映射共享内存到进程空间
if ((shmaddr = shmat(shmid, NULL, 0)) == (char *)-1) {
perror("shmat");
exit(1);
}
// 创建子进程1: SERVER
pid_t pid1 = fork();
if (pid1 == -1) {
perror("fork");
exit(1);
} else if (pid1 == 0) {
// 子进程1: SERVER
printf("SERVER: start\n");
*shmaddr = -1;
printf("SERVER: set value to %d\n", *shmaddr);
while (1) {
if (*shmaddr != -1) {
printf("SERVER: read value %d\n", *shmaddr);
*shmaddr = -1;
printf("SERVER: set value to %d\n", *shmaddr);
}
sleep(1);
}
} else {
// 创建子进程2: CLIENT
pid_t pid2 = fork();
if (pid2 == -1) {
perror("fork");
exit(1);
} else if (pid2 == 0) {
// 子进程2: CLIENT
printf("CLIENT: start\n");
while (*shmaddr != -1) {
sleep(1);
}
int num = rand() % 10;
*shmaddr = num;
printf("CLIENT: set value to %d\n", *shmaddr);
exit(0);
} else {
// 父进程等待两个子进程结束
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
// 删除共享内存
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl");
exit(1);
}
printf("Parent exit.\n");
}
}
// 解除共享内存映射
if (shmdt(shmaddr) == -1) {
perror("shmdt");
exit(1);
}
return 0;
}
```
这段代码中,我们首先使用 `shmget()` 创建了一个共享内存区域,然后使用 `shmat()` 将其映射到当前进程的地址空间中。接着,我们创建了两个子进程,一个是 SERVER 进程,它将共享内存首字节设置为 -1,并循环检查首字节是否被修改,如果被修改,则读取它的值,并将首字节再次设置为 -1;另一个是 CLIENT 进程,它在首字节为 -1 时将其设置为 0 到 9 中的一个数字。
在父进程中,我们使用 `waitpid()` 等待子进程结束,并在最后使用 `shmctl()` 删除共享内存区域。最后,我们使用 `shmdt()` 解除共享内存的映射。
注意,为了保证 SERVER 进程能够正常工作,我们需要让它一直运行,所以在这里我们使用了一个死循环。如果你想要结束这个程序,可以在终端中使用 `killall -9 程序名` 命令来杀死所有与该程序相关的进程。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)