1 Star 0 Fork 0

maxiaowei / Linux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ipc_shm.c 872 Bytes
一键复制 编辑 原始数据 按行查看 历史
maxiaowei 提交于 2020-12-28 16:02 . 修改目录层级
#include "apue.h"
#include <sys/shm.h>
#include <sys/wait.h>
int main()
{
int shmid;
pid_t pid;
char *ptr;
// 创建共享存储
shmid = shmget(IPC_PRIVATE, 128, IPC_CREAT | S_IWUSR | S_IRUSR);
if (shmid < 0) {
printf("shmget failed.\n");
} else {
printf("shmid=%d\n", shmid);
}
if ((pid = fork()) < 0) {
printf("fork failed.\n");
} else if (pid > 0) {
// parent
// 获取存储区地址
if ((ptr = shmat(shmid, 0, 0)) == (void *)-1) {
printf("parent: shmat failed.\n");
}
// 写入
char *str = "abcdefg";
memcpy(ptr, str, sizeof(str) + 1);
wait(NULL);
return 0;
} else {
// child
sleep(1);
// 获取存储器地址
if ((ptr = shmat(shmid, 0, 0)) == (void *)-1) {
printf("child : shmat failed.\n");
}
printf("child: %s\n",ptr);
return 0;
}
return 0;
}
C
1
https://gitee.com/maxiaowei/Linux.git
git@gitee.com:maxiaowei/Linux.git
maxiaowei
Linux
Linux
master

搜索帮助