Linux上共享内存的寿命是多少 [英] What is the lifetime of shared memory on Linux

查看:104
本文介绍了Linux上共享内存的寿命是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ftok/shmget/shmat/shmdt函数在Linux的共享段上进行创建,写入和读取操作.

I am creating, writing to and reading from a shared segment on Linux using the ftok/shmget/shmat/shmdt functions.

如果我在一个程序中写入该段然后退出,随后又从另一个程序中读取该段,那么我很惊讶地发现数据仍然存在.

If I write to the segment in one program which then exits, and subsequently read the segment from another program later, I am surprised to find the data still there.

我希望当共享段的最后一个进程执行shmdt时,该段将被释放.

I would have expected that when the last process sharing a segment does a shmdt, the segment would be free'd.

我可以依靠这种行为吗?还是类似于在free()之后继续使用指针?

Can I rely on this behavior? Or is it analogous to continuing to use a pointer after free()'ing it?

推荐答案

共享内存区域将一直保留,直到通过 shmctl(shmid,IPC_RMID,...) [或系统重新启动]将其删除.这将在每个进程完成 shmdt [或终止]后生效.

The shared memory area remains until it is deleted via shmctl(shmid,IPC_RMID,...) [or system reboot]. This will take effect after every process has done shmdt [or terminated].

语义与文件非常相似. shmat 类似于 open . shmdt 类似于 close ,[上方] shmctl 类似于 unlink .

The semantics are very similar to a file. shmat is like open. shmdt is like close and the [above] shmctl is like unlink.

如果您关闭文件,您希望它一直存在直到被专门删除,不是吗?因此,共享内存段的运行方式相似

If you close a file, you'd like it to persist until specifically deleted, wouldn't you? So, shared memory segments operate similarly

这篇关于Linux上共享内存的寿命是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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