从壳shm_unlink? [英] shm_unlink from the shell?

查看:60
本文介绍了从壳shm_unlink?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序使用正在处理的 shm_open 创建共享内存对象.它尝试使用 shm_unlink 释放对象,但是有时编程错误将导致其崩溃,然后再进行调用.在那种情况下,我需要手动"取消链接共享内存对象,并且我希望能够在普通外壳中完成它,而无需编写任何C代码-即使用普通Linux实用程序.

I have a program that creates a shared memory object with shm_open that I'm working on. It tries to release the object with shm_unlink, but sometimes a programming error will cause it to crash before it can make that call. In that case, I need to unlink the shared memory object "by hand," and I'd like to be able to do it in a normal shell, without writing any C code -- i.e. with normal Linux utilities.

可以做到吗?这个问题似乎是说在/dev/shm/path_passed_to_shm_open上使用 unlink(1),但手册页不清楚.

Can it be done? This question seems to say that using unlink(1) on /dev/shm/path_passed_to_shm_open, but the manpages aren't clear.

推荐答案

如果没有其他进程映射对象,则取消链接/dev/shm 中的文件将删除共享内存对象.

Unlinking the file in /dev/shm will delete the shared memory object if no other process has the object mapped.

与在内核中实现的SYSV共享内存不同,POSIX共享内存对象只是伪装的文件".

Unlike SYSV shared memory, which is implemented in the Kernel, POSIX shared memory objects are simply "files in disguise".

调用shm_open和mmap时,您可以在流程流程图中看到以下内容(使用 pmap -X ):

When you call shm_open and mmap, you can see the following in the process process map (using pmap -X):

 Address Perm   Offset Device   Inode Mapping
b7737000 r--s 00000000  00:0d 2267945 test_object

设备的主设备号和次设备号对应于安装在/dev/shm 上的tmpfs(某些系统将其安装在/run上,然后将/dev/shm 链接到/run/shm ).

The device major and minor number correspond to the tmpfs mounted at /dev/shm (some systems mount this at /run, and then symlink /dev/shm to /run/shm).

该文件夹的清单将显示相同的inode编号:

A listing of the folder will show the same inode number:

$ ls -li /dev/shm/
2267945 -rw------- 1 mikel mikel 1 Apr 14 13:36 test_object

与其他inode一样,删除所有引用后,该空间将被释放.如果我们关闭唯一引用此程序的程序,则会看到:

Like any other inode, the space will be freed when all references are removed. If we close the only program referencing this we see:

$ cat /proc/meminfo | grep Shmem
Shmem:               700 kB

一旦我们删除了最后一个引用(在/dev/shm 中创建),该空间就会被释放:

Once we remove the last reference (created in /dev/shm), the space will be freed:

$ rm /dev/shm/test_object
$ cat /proc/meminfo | grep Shmem
Shmem:               696 kB

如果您感到好奇,可以在glibc源代码中查看相应的文件. shm_directory.c shm_directory.h 将文件名生成为/dev/your_shm_name .实现 shm_open shm_unlink 只需打开并取消链接此文件.因此,应该很容易看到 rm/dev/shm/your_shm_name 执行相同的操作,

If you're curious, you can look at the corresponding files in the glibc sources. shm_directory.c and shm_directory.h generate the filename as /dev/your_shm_name. The implementations shm_open and shm_unlink simply open and unlink this file. So it should be easy to see that rm /dev/shm/your_shm_name performs the same operation,

这篇关于从壳shm_unlink?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆