UNIX 如果查询一个共享内存已经被IPCRM了

pengxn 2010-06-28 04:35:44
先启动了一个进程,进程中连接了一个共享内存,并保存了SHM_id,
然后手工删除这块共享内存,使用IPCRM命令

请问如何在该进程中,根据命令查询这块内存已经申请删除了。通过shmctl似乎不行
...全文
318 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2010-06-29
  • 打赏
  • 举报
回复
进程内可以用根据ipc_perm的mode成员SHM_DEST位进行判断

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#include <stdio.h>

int main(void)
{
key_t key;
int id;
size_t size;
void *addr;
int ch;
struct shmid_ds buf;
unsigned int mode;

key = ftok("shm.key", 1);
size = 1024;
id = shmget(key, size, SHM_R | SHM_W | IPC_CREAT);
addr = shmat(id, NULL, 0);

while ((ch = getchar()) != EOF) {
shmctl(id, IPC_STAT, &buf);
mode = buf.shm_perm.mode;
(void)printf("%o\n", (unsigned short)mode);
#ifdef SHM_DEST
if (SHM_DEST & mode)
(void)printf("%s\n", "destroy segment when # attached = 0");
#endif
}

shmdt(addr);
shmctl(id, IPC_RMID, NULL);

return 0;
}


用ipcs -m命令进行检查,如果在MODE列有D标志,表示已被其他进程删除。

IPC status from /dev/mem as of Tue Jun 29 20:37:31 BEIST 2010
T ID KEY MODE OWNER GROUP
Shared Memory:
m 0 0x580022ed --rw-rw-rw- root system
m 1048577 0x0d000553 --rw-rw---- root system
m 1048578 0xffffffff --rw-rw---- root system
m 1048579 0x78000004 --rw-rw-rw- root system
m 4 0x00001515 --rw-rw-rw- hsh1985 member
m 5 0xffffffff --rw-rw-r-- friedric member
m 6 0xffffffff --rw-rw-r-- friedric member
m 7 0xffffffff --rw-rw-r-- friedric member
m 8 0xffffffff --rw-rw-r-- friedric member
m 7340042 0xff2b0a63 --rw-rw-r-- friedric member
m 11 0xff2b0a60 --rw-rw-r-- friedric member
m 12 0xff2b0a5f --rw-rw-r-- friedric member
m 13 0xff2b0a61 --rw-rw-r-- friedric member
m 15728655 0xff2b0491 --rw-rw-r-- friedric member
m 23068688 0xffffffff D-rw------- mymtom member

MODE
(all) the facility access modes and flags. The mode consists of 11 characters that are interpreted as follows:

The first two characters can be the following:
R
If a process is waiting on a msgrcv system call.
S
If a process is waiting on a msgsnd system call.
D
If the associated shared memory segment has been removed. It disappears when the last process attached
to the segment detaches it.
C
If the associated shared memory segment is to be cleared when the first attach is run.
-
If the corresponding special flag is not set.
wqkjj 2010-06-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pengxn 的回复:]
引用 2 楼 wqkjj 的回复:
汗,写成消息队列的了,再写一个
if( shmget(shmkey,size,shmflag) == -1 &amp;&amp; errno == EEXIST )


NO NO NO
[/Quote]

哦,EEXIST不能识别这种情况。
那就自己写一个函数也很简单:
popen("ipcs -m".....);
然后读输出结果,判断你的shmid或者shmkey是否存在。
pengxn 2010-06-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wqkjj 的回复:]
汗,写成消息队列的了,再写一个
if( shmget(shmkey,size,shmflag) == -1 && errno == EEXIST )
[/Quote]

NO NO NO
wqkjj 2010-06-28
  • 打赏
  • 举报
回复
汗,写成消息队列的了,再写一个
if( shmget(shmkey,size,shmflag) == -1 && errno == EEXIST )
wqkjj 2010-06-28
  • 打赏
  • 举报
回复
if( msgget( Queue_Key, perms ) == -1 ) && errno == ENOENT )
Re: 《Linux 系统管理命令》 ---------------------------------------内容提要: 01/20)命令lsof          :查看进程打开的文件02/20)命令uptime     :显示系统的运行时间及负载03/20)命令free          :查看系统内存信息04/20)命令iftop         :动态显示网络接口流量信息05/20)命令vmstat      :虚拟内存统计06/20)命令mpstat      :CPU信息统计07/20)命令iostat        :I/O信息统计08/20)命令 iotop        :动态显示磁盘 I/O 统计信息09/20)命令 sar            :收集系统信息10/20)命令 chkconfig :管理开机服务11/20)命令 ntsysv       :管理开机服务12/20)命令 setup        :系统管理工具13/20)命令 ethtool      :查询网卡参数14/20)命令 mii-tool     :管理网络接口的状态15/20)命令 dmidecode:查询系统硬件信息16/20)命令 lspci           :显示所有PCI设备17/20)命令 ipcs            :显示进程间通信设施的状态18/20)命令ipcrm          :清除ipc相关信息19/20)命令rpm             :RPM包管理器20/20)命令yum             :自动化RPM包管理工具  本人在教学和实战过程中发现,即便是有一定运维经验的人,可能已经能够搭建一定复杂度的Linux架构,但是在来来回回的具体操作中,还是体现出CLI(命令界面)功底不够扎实,甚至操作的非常‘拙’、处处露‘怯’。 对一个士兵来说,枪就是他的武器,对于一个程序员来说,各种library(工具库)就是他的武器;而对于Linux运维人员来说,无疑命令行工具CLI(命令界面)就是他们的武器;高手和小白之间的差距往往就体现在对于这些“武器”的掌握和熟练程度上。有时候一个参数就能够解决的事情,小白们可能要写一个复杂的Shell脚本才能搞定,这就是对CLI(命令界面)没有理解参悟透彻导致。 研磨每一个命令就是擦拭手中的作战武器,平时不保养不理解,等到作战的时候,一定不能够将手中的武器发挥到最好,所以我们要平心、静气和专注,甘坐冷板凳一段时间,才能练就一身非凡的内功! 本教程从实战出发,结合当下流行或最新的Linux(v6/7/8 版本)同时演示,将命令行结合到解决企业实战问题中来,体现出教学注重实战的务实精神,希望从事或未来从事运维的同学,能够认真仔细的学完Linux核心命令的整套课程。 本课程系列将逐步推出,看看我教学的进度和您学习的步伐,孰占鳌头! 注:关于教学环境搭建,可以参考本人其它课程系列,本教学中就不再赘述! 《参透 VMware 桌面级虚拟化》 《在虚拟机中安装模版机(包括应用软件等)》 《SecureCRT 连接 GNS3/Linux 的安全精密工具》 

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧