13 共享内存3

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$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值