编写一个程序 sharedmem.c,在其中建立一个子进程,让父子进程通过共享内存的方 法实现通信。其中,父进程创建一个共享内存段,然后由子进程将该共享内存附加到自己 的地址空间中,并写入该共享内存下列信息:Hi,this is child process sending message!。在等待子进程对共享内存的操作完成后,父进程将该共享内存附加到自己的地 址空间中,并读出该共享内存中的信息,与该共享内存段的基本信息(大小,建立该共享 内存的进程 ID,最后操作该共享内存段的进程 ID)一并显示出来
时间: 2024-03-02 22:53:22 浏览: 9
下面是 sharedmem.c 的代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024 // 共享内存大小
int main() {
char* shm_ptr; // 指向共享内存的指针
int shm_id; // 共享内存的 ID
pid_t pid; // 进程 ID
// 创建共享内存
shm_id = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);
if (shm_id < 0) {
perror("shmget error");
exit(EXIT_FAILURE);
}
// 创建子进程
pid = fork();
if (pid < 0) {
perror("fork error");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// 子进程
// 将共享内存附加到自己的地址空间
shm_ptr = shmat(shm_id, NULL, 0);
if (shm_ptr == (char*) -1) {
perror("shmat error");
exit(EXIT_FAILURE);
}
// 写入信息到共享内存
sprintf(shm_ptr, "Hi, this is child process sending message!");
// 解除附加
shmdt(shm_ptr);
exit(EXIT_SUCCESS);
} else {
// 父进程
// 等待子进程对共享内存的操作完成
wait(NULL);
// 将共享内存附加到自己的地址空间
shm_ptr = shmat(shm_id, NULL, 0);
if (shm_ptr == (char*) -1) {
perror("shmat error");
exit(EXIT_FAILURE);
}
// 读取共享内存中的信息
printf("Message from child process: %s\n", shm_ptr);
// 获取共享内存的基本信息
struct shmid_ds shm_info;
if (shmctl(shm_id, IPC_STAT, &shm_info) == -1) {
perror("shmctl error");
exit(EXIT_FAILURE);
}
printf("Shared memory size: %ld\n", shm_info.shm_segsz);
printf("Process ID that created the shared memory: %d\n", shm_info.shm_cpid);
printf("Process ID that last operated on the shared memory: %d\n", shm_info.shm_lpid);
// 解除附加并删除共享内存
shmdt(shm_ptr);
shmctl(shm_id, IPC_RMID, NULL);
exit(EXIT_SUCCESS);
}
}
```
在该程序中,首先创建了一个共享内存段,大小为 1024 字节。然后创建了一个子进程,子进程将该共享内存附加到自己的地址空间中,并向共享内存中写入消息。父进程等待子进程对共享内存的操作完成后,将该共享内存附加到自己的地址空间中,并读取共享内存中的信息,并输出共享内存的基本信息。最后,父进程解除共享内存的附加并删除该共享内存。
注意,在实际使用中,要确保共享内存的键值是唯一的,可以使用 ftok 函数生成键值,例如:
```
key_t shm_key = ftok(".", 's');
shm_id = shmget(shm_key, SHM_SIZE, IPC_CREAT | 0666);
```
这里使用当前目录和字符 's' 作为参数生成键值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)