13 共享内存3
来源:互联网 发布:ubuntu启动程序命令 编辑:程序博客网 时间:2024/05/22 06:06
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.calex@alex-virtual-machine:/extra/process/thirteen$ ./a.outcreat key sucess key=6211A055creat share memory sucess shmid=2195468------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 950272 alex 600 524288 2 dest0x00000000 1867777 alex 600 524288 2 dest0x00000000 425986 alex 600 524288 2 dest0x00000000 458755 alex 600 524288 2 dest0x00000000 2064388 alex 600 524288 2 dest0x00000000 1572869 alex 600 33554432 2 dest0x00000000 2129926 alex 777 128 00x00000000 1081351 alex 600 16777216 20x00000000 1179656 alex 600 524288 2 dest0x00000000 2097161 alex 600 1048576 2 dest0x00000000 1409034 alex 600 524288 2 dest0x6211a066 2162699 alex 777 128 00x6211a055 2195468 alex 777 128 0helloshare memory data:hellosecond read share memory data:helloSegmentation 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.calex@alex-virtual-machine:/extra/process/thirteen$ lsa.c a.out myipcsrm myipcsrm.c shmctl.c shmdt.calex@alex-virtual-machine:/extra/process/thirteen$ ./a.outcreat key sucess key=6211A055creat share memory sucess shmid=2195468------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 950272 alex 600 524288 2 dest0x00000000 1867777 alex 600 524288 2 dest0x00000000 425986 alex 600 524288 2 dest0x00000000 458755 alex 600 524288 2 dest0x00000000 2064388 alex 600 524288 2 dest0x00000000 1572869 alex 600 33554432 2 dest0x00000000 2129926 alex 777 128 00x00000000 1081351 alex 600 16777216 20x00000000 1179656 alex 600 524288 2 dest0x00000000 2097161 alex 600 1048576 2 dest0x00000000 1409034 alex 600 524288 2 dest0x6211a066 2162699 alex 777 128 00x6211a055 2195468 alex 777 128 0helloshare memory data:hellosecond read share memory data:hello------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 950272 alex 600 524288 2 dest0x00000000 1867777 alex 600 524288 2 dest0x00000000 425986 alex 600 524288 2 dest0x00000000 458755 alex 600 524288 2 dest0x00000000 2064388 alex 600 524288 2 dest0x00000000 1572869 alex 600 33554432 2 dest0x00000000 2129926 alex 777 128 00x00000000 1081351 alex 600 16777216 20x00000000 1179656 alex 600 524288 2 dest0x00000000 2097161 alex 600 1048576 2 dest0x00000000 1409034 alex 600 524288 2 dest0x6211a066 2162699 alex 777 128 0alex@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.calex@alex-virtual-machine:/extra/process/thirteen$ ./myipcsrmplease input paramalex@alex-virtual-machine:/extra/process/thirteen$ ipcs -m------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 950272 alex 600 524288 2 dest0x00000000 1867777 alex 600 524288 2 dest0x00000000 425986 alex 600 524288 2 dest0x00000000 458755 alex 600 524288 2 dest0x00000000 2064388 alex 600 524288 2 dest0x00000000 1572869 alex 600 33554432 2 dest0x00000000 2129926 alex 777 128 00x00000000 1081351 alex 600 16777216 20x00000000 1179656 alex 600 524288 2 dest0x00000000 2097161 alex 600 1048576 2 dest0x00000000 1409034 alex 600 524288 2 dest0x6211a066 2162699 alex 777 128 0alex@alex-virtual-machine:/extra/process/thirteen$ ./myipcsrm 2162699please input paramalex@alex-virtual-machine:/extra/process/thirteen$ ./myipcsrm -m 2162699delete share memoryshmid=2162699------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00000000 950272 alex 600 524288 2 dest0x00000000 1867777 alex 600 524288 2 dest0x00000000 425986 alex 600 524288 2 dest0x00000000 458755 alex 600 524288 2 dest0x00000000 2064388 alex 600 524288 2 dest0x00000000 1572869 alex 600 33554432 2 dest0x00000000 2129926 alex 777 128 00x00000000 1081351 alex 600 16777216 20x00000000 1179656 alex 600 524288 2 dest0x00000000 2097161 alex 600 1048576 2 dest0x00000000 1409034 alex 600 524288 2 destalex@alex-virtual-machine:/extra/process/thirteen$
0 0
- 13 共享内存3
- 【共享内存】共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- 共享内存
- Git仓库应用
- 数据挖掘整理(一)
- 分块查找算法 (php)
- RxJava学习经验谈——map与flatmap
- 【mysql】如何搭建集群主从复制
- 13 共享内存3
- Java:日期时间字符串和毫秒相互转换
- Nginx源码阅读(ngx_array_t)
- [Boolan] C++第二周(创建一个带指针成员变量的类)[注意事项]
- 直立平衡车的姿态测量卡尔曼滤波算法原理与应用(附代码及调试截图)
- Android简易实战教程--第五十二话《满屏拖动的控件2》
- 编程珠玑: 15章 字符串 15.2寻找字符串中的最长重复子串 -------解题总结
- C ++ new的几种用法
- 堆栈窗体QStackedWidget类