1. 删除进程中的地址映射shmdt
原型: int shmdt(const void *shmaddr)
参数: shmaddr共享内存映射后的地址
返回: 成功0,出错-1
2. 删除共享内存对象shmctl
原型: int shmctl(int shmid, int cmd, struct shmid_ds * buf)
参数: shmid,要操作的共享内存标识符
cmd: IPC_STAT,获取对象属性值 //实现了命令ipcs -m
IPC_SET,设置对象属性
IPC_RMID,删除对象 //实现了命令ipcrm -m
返回: 成功0,出错-1
3. 实例一
通过ftok生成key,并创建共享内存;将共享内存映射到用户空间;写内存;读内存;之后删除;删除后在继续写共享内存,系统会报错
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
int shmid;
int key;
char *p;
key=ftok("./a.c",'b');
if(key <0 )
{
printf("creat key fialure\n");
return -2;
}
printf("creat key sucess key=%X\n",key);
shmid=shmget(key,128,IPC_CREAT | 0777);
if(shmid <0)
{
printf("creat share memory failure\n");
return -1;
}
printf("creat share memory sucess shmid=%d\n",shmid);
system("ipcs -m");
p=(char *)shmat(shmid,NULL,0);
if(p == NULL)
{
printf("shmat function failure\n");
return -3;
}
//write share memory
fgets(p,128,stdin);
//start read share memory
printf("share memory data:%s",p);
printf("second read share memory data:%s",p);
shmdt(p);
memcpy(p,"abcd",4);
return 0;
}
执行结果:
alex@alex-virtual-machine:/extra/process/thirteen$ gcc shmdt.c
alex@alex-virtual-machine:/extra/process/thirteen$ ./a.out
creat key sucess key=6211A055
creat share memory sucess shmid=2195468
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 950272 alex 600 524288 2 dest
0x00000000 1867777 alex 600 524288 2 dest
0x00000000 425986 alex 600 524288 2 dest
0x00000000 458755 alex 600 524288 2 dest
0x00000000 2064388 alex 600 524288 2 dest
0x00000000 1572869 alex 600 33554432 2 dest
0x00000000 2129926 alex 777 128 0
0x00000000 1081351 alex 600 16777216 2
0x00000000 1179656 alex 600 524288 2 dest
0x00000000 2097161 alex 600 1048576 2 dest
0x00000000 1409034 alex 600 524288 2 dest
0x6211a066 2162699 alex 777 128 0
0x6211a055 2195468 alex 777 128 0
hello
share memory data:hello
second read share memory data:hello
Segmentation fault (core dumped)
alex@alex-virtual-machine:/extra/process/thirteen$
4. 实例二,操作完共享内存后,将对象删除
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
int shmid;
int key;
char *p;
key=ftok("./a.c",'b');
if(key <0 )
{
printf("creat key fialure\n");
return -2;
}
printf("creat key sucess key=%X\n",key);
shmid=shmget(key,128,IPC_CREAT | 0777);
if(shmid <0)
{
printf("creat share memory failure\n");
return -1;
}
printf("creat share memory sucess shmid=%d\n",shmid);
system("ipcs -m");
p=(char *)shmat(shmid,NULL,0);
if(p == NULL)
{
printf("shmat function failure\n");
return -3;
}
//write share memory
fgets(p,128,stdin);
//start read share memory
printf("share memory data:%s",p);
printf("second read share memory data:%s",p);
shmdt(p);
// memcpy(p,"abcd",4);
shmctl(shmid,IPC_RMID,NULL);
system("ipcs -m ");
return 0;
}
执行结果:
alex@alex-virtual-machine:/extra/process/thirteen$ gcc shmctl.c
alex@alex-virtual-machine:/extra/process/thirteen$ ls
a.c a.out myipcsrm myipcsrm.c shmctl.c shmdt.c
alex@alex-virtual-machine:/extra/process/thirteen$ ./a.out
creat key sucess key=6211A055
creat share memory sucess shmid=2195468
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 950272 alex 600 524288 2 dest
0x00000000 1867777 alex 600 524288 2 dest
0x00000000 425986 alex 600 524288 2 dest
0x00000000 458755 alex 600 524288 2 dest
0x00000000 2064388 alex 600 524288 2 dest
0x00000000 1572869 alex 600 33554432 2 dest
0x00000000 2129926 alex 777 128 0
0x00000000 1081351 alex 600 16777216 2
0x00000000 1179656 alex 600 524288 2 dest
0x00000000 2097161 alex 600 1048576 2 dest
0x00000000 1409034 alex 600 524288 2 dest
0x6211a066 2162699 alex 777 128 0
0x6211a055 2195468 alex 777 128 0
hello
share memory data:hello
second read share memory data:hello
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 950272 alex 600 524288 2 dest
0x00000000 1867777 alex 600 524288 2 dest
0x00000000 425986 alex 600 524288 2 dest
0x00000000 458755 alex 600 524288 2 dest
0x00000000 2064388 alex 600 524288 2 dest
0x00000000 1572869 alex 600 33554432 2 dest
0x00000000 2129926 alex 777 128 0
0x00000000 1081351 alex 600 16777216 2
0x00000000 1179656 alex 600 524288 2 dest
0x00000000 2097161 alex 600 1048576 2 dest
0x00000000 1409034 alex 600 524288 2 dest
0x6211a066 2162699 alex 777 128 0
alex@alex-virtual-machine:/extra/process/thirteen$
5. 实例三,myipcsrm函数
#include "sys/types.h"
#include "sys/shm.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main(int argc,char *argv[])
{
int shmid;
if(argc < 3)
{
printf("please input param\n");
return -1;
}
if(strcmp(argv[1],"-m") == 0)
printf("delete share memory");
else
return -2;
shmid=atoi(argv[2]);
printf("shmid=%d\n",shmid);
shmctl(shmid,IPC_RMID,NULL);
system("ipcs -m");
return 0;
}
执行结果:
alex@alex-virtual-machine:/extra/process/thirteen$ gcc -o myipcsrm myipcsrm.c
alex@alex-virtual-machine:/extra/process/thirteen$ ./myipcsrm
please input param
alex@alex-virtual-machine:/extra/process/thirteen$ ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 950272 alex 600 524288 2 dest
0x00000000 1867777 alex 600 524288 2 dest
0x00000000 425986 alex 600 524288 2 dest
0x00000000 458755 alex 600 524288 2 dest
0x00000000 2064388 alex 600 524288 2 dest
0x00000000 1572869 alex 600 33554432 2 dest
0x00000000 2129926 alex 777 128 0
0x00000000 1081351 alex 600 16777216 2
0x00000000 1179656 alex 600 524288 2 dest
0x00000000 2097161 alex 600 1048576 2 dest
0x00000000 1409034 alex 600 524288 2 dest
0x6211a066 2162699 alex 777 128 0
alex@alex-virtual-machine:/extra/process/thirteen$ ./myipcsrm 2162699
please input param
alex@alex-virtual-machine:/extra/process/thirteen$ ./myipcsrm -m 2162699
delete share memoryshmid=2162699
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 950272 alex 600 524288 2 dest
0x00000000 1867777 alex 600 524288 2 dest
0x00000000 425986 alex 600 524288 2 dest
0x00000000 458755 alex 600 524288 2 dest
0x00000000 2064388 alex 600 524288 2 dest
0x00000000 1572869 alex 600 33554432 2 dest
0x00000000 2129926 alex 777 128 0
0x00000000 1081351 alex 600 16777216 2
0x00000000 1179656 alex 600 524288 2 dest
0x00000000 2097161 alex 600 1048576 2 dest
0x00000000 1409034 alex 600 524288 2 dest
alex@alex-virtual-machine:/extra/process/thirteen$