【发布时间】:2017-01-30 18:29:36
【问题描述】:
#include<sys/shm.h>
#include<sys/stat.h>
#include<stdio.h>
int main(void)
{
int segment_id;
char *shared_memory;
const int size=4069;
segment_id=shmget(IPC_PRIVATE,size,S_IRUSR|S_IWUSR);
printf("segment ID=%d\n",segment_id);
shared_memory=(char *)shmat(segment_id,NULL,0);
sprintf(shared_memory,"Hi There!");
while(1){
}
return 0;
}
如果在下面程序的输入中输入相同的segment_id,它会起作用吗?
#include<sys/shm.h>
#include<sys/stat.h>
#include<stdio.h>
int main(void)
{
int segment_id;
char *shared_memory;
const int size=4069;
printf("please input segment id\n");
scanf("%d",&segment_id);
shared_memory=(char *)shmat(segment_id,NULL,0);
printf("%s\n",shared_memory );
return 0;
}
它在这里工作,但请解释它是如何工作的,这意味着相同的 segment_id 在下面的程序中也是如何工作的?
【问题讨论】:
标签: c linux ubuntu memory-management shared-memory