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

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

请问如何在该进程中,根据命令查询这块内存已经申请删除了。通过shmctl似乎不行
...全文
326 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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 )

23,217

社区成员

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

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