代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。