共享内存映射的进程空间shmat函数返回是什么

是共享内存的物理地址,还是进程的虚拟地址,如果是父子进程分别映射呢?返回地址一样吗?有点晕

看一下代码
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 2048

int main()
{
pid_t pid;
int shmid;
char *shm_addr;
char flag[] = "WROTE";
char buffer[BUFFER_SIZE];

if((shmid = shmget(IPC_PRIVATE,BUFFER_SIZE,0666)) < 0)
{
perror("shmget");
exit(-1);
}
else
{
printf("Create shared-memory:%d\n",shmid);
}

system("ipcs -m");
pid = fork();
if(pid < 0)
{
perror("fork error");
exit(-1);
}
else if(pid == 0)
{
shm_addr = shmat(shmid,0,0);
if(shm_addr == (void *)-1)
{
perror("Child:shmat");
exit(-1);
}
else
{
printf("Child:Attach shared-memory:%p \n",shm_addr);
}

system("ipcs -m");

while(strncmp(shm_addr,flag,strlen(flag)))
{
printf("Child:wait for enable data...\n");
sleep(5);
}

strcpy(buffer,shm_addr + strlen(flag));
printf("Child:shared-memory:%s\n",buffer);

if((shmdt(shm_addr)) < 0)
{
perror("shmdt");
exit(-1);
}
else
{
printf("Child: Deattach shared-memory\n");
}
system("ipcs -m");

if(shmctl(shmid,IPC_RMID,NULL) == -1)
{
perror("Child:shmctl(IPC_RMID)");
exit(-1);
}
else
{
printf("Delete shmared-memory\n");
}

system("ipcs -m");
}
else
{
if((shm_addr = shmat(shmid,0,0)) == (void *)-1)
{
perror("parent:shmat");
exit(-1);
}
else
{
printf("Parent:Attach shmared-memory:%p\n",shm_addr);
}

sleep(1);
printf("\nInput some string:\n");
fgets(buffer,BUFFER_SIZE,stdin);
strncpy(shm_addr + strlen(flag),buffer,strlen(buffer));
strncpy(shm_addr,flag,strlen(flag));

if((shmdt(shm_addr)) < 0)
{
perror("Parent:shmdt");
exit(-1);
}
else
{
printf("Parent : Deattach shared-memory\n");
}

system("ipcs -m");
waitpid(pid,NULL,0);
printf("Finsihed\n");
}

return 0;
}

运行结果
Create shared-memory:196613

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

0x00000000 0 fs 700 76800 2 dest

0x00000000 32769 fs 700 1843200 2 dest

0x00000000 65538 fs 700 20196 2 dest

0x00000000 98307 fs 700 17028 2 dest

0x00000000 131076 fs 700 19800 2 dest

0x00000000 196613 fs 666 2048 0

Parent:Attach shmared-memory:0xb773d000
Child:Attach shared-memory:0xb773d000

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

0x00000000 0 fs 700 76800 2 dest

0x00000000 32769 fs 700 1843200 2 dest

0x00000000 65538 fs 700 20196 2 dest

0x00000000 98307 fs 700 17028 2 dest

0x00000000 131076 fs 700 19800 2 dest

0x00000000 196613 fs 666 2048 2

Child:wait for enable data...

Input some string:
xiaoqiang
Parent : Deattach shared-memory

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

0x00000000 0 fs 700 76800 2 dest

0x00000000 32769 fs 700 1843200 2 dest

0x00000000 65538 fs 700 20196 2 dest

0x00000000 98307 fs 700 17028 2 dest

0x00000000 131076 fs 700 19800 2 dest

0x00000000 196613 fs 666 2048 1

Child:shared-memory:xiaoqiang

Child: Deattach shared-memory

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

0x00000000 0 fs 700 76800 2 dest

0x00000000 32769 fs 700 1843200 2 dest

0x00000000 65538 fs 700 20196 2 dest

0x00000000 98307 fs 700 17028 2 dest

0x00000000 131076 fs 700 19800 2 dest

0x00000000 196613 fs 666 2048 0

Delete shmared-memory

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status

0x00000000 0 fs 700 76800 2 dest

0x00000000 32769 fs 700 1843200 2 dest

0x00000000 65538 fs 700 20196 2 dest

0x00000000 98307 fs 700 17028 2 dest

0x00000000 131076 fs 700 19800 2 dest

Finsihed

4个回答

啥叫物理地址?都是虚拟的啊。反正就是返回一个地址,你就用这个地址就行了,就是你进程空间的地址。
如果是两个不同进程,那么地址是自己的,大家看见自己的不同虚拟地址,实际上访问的是同一块物理内存。

连接共享内存标识符为shmid的共享内存,连接成功后把共享内存区对象映射到调用进程的地址空间,随后可像本地空间一样访问

czq1024
czq1024 映射函数shmat返回的是共享内存空间的物理地址,还是进程空间的虚拟地址
接近 3 年之前 回复

返回的是一个物理地址

是共享内存空间的物理地址

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问