czq1024
czq1024
采纳率50%
2017-12-01 02:25 阅读 1.1k
已采纳

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

10

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

看一下代码
#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条回答 默认 最新

  • 已采纳
    qq_41242687 qq_41242687 2017-12-01 13:05

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

    点赞 1 评论 复制链接分享
  • oyljerry oyljerry 2017-12-01 02:38

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

    点赞 1 评论 复制链接分享
  • qq_33372544 qq_33372544 2017-12-01 03:40

    返回的是一个物理地址

    点赞 评论 复制链接分享
  • qq_41172958 qq_41172958 2017-12-01 04:54

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

    点赞 评论 复制链接分享

相关推荐