如果其中一个进程意外死亡,进程间内存会发生什么? [英] What happens to interprocess memory if one of the processes dies unexpectedly?

查看:80
本文介绍了如果其中一个进程意外死亡,进程间内存会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您对动机感兴趣,那么请在接下来的几句话中详细说明,如果不只是跳至问题解答.

if you are interested in motivation Ill elaborate it in next few sentences, if not just skip to the Q.

我当时正在考虑制作快速记录器,但是该程序在程序崩溃时不会受到影响(也就是最后几个日志消息丢失).所以我的想法是写入共享内存(ringbuffer),并从中读取另一个低优先级进程并进行转储. 但是要使它正常工作,我需要知道如果一个进程退出(正常退出,SEGFAULT),共享内存会发生什么……

I was thinking about making fast logger but the one that is not affected when program crashes(aka few last log msgs arent lost). So my idea is to write to the shared memory(ringbuffer) and have another low prio process read from it and do the dumping. But for that to work I need to know what happens to shared memory if one process exits(normal exit, SEGFAULT)...

所以我的问题是: 当其中一个进程死亡时,共享内存会如何处理(在Linux中,但您也可以使用A for Win)?是UB吗?

So my question is: What happens to shared memory (in Linux, but you can A for Win also) when one of the procs die? Is it UB?

推荐答案

其中一个进程死亡时,共享内存会如何处理(在Linux中,但您也可以使用A for Win)?

What happens to shared memory (in Linux, but you can A for Win also) when one of the procs die?

什么都没有.当进程终止时,共享内存将保留原样.它映射为/dev/shm/目录下的文件.当系统重新启动或所有进程取消对共享内存文件的映射并调用shm_unlink()时,将其删除.

Nothing. When a process dies, the shared memory is left as it is. It is mapped as a file under /dev/shm/ directory. It is removed either when the system reboots, or when all processes unmmap the shared memory file and the shm_unlink() is called.

是UB吗?

Is it UB?

不,它的定义很明确.请参见手册页中的shm_overview(7):

No, it is well defined. See the man page for shm_overview(7) :

POSIX共享内存对象具有内核持久性:一个共享内存对象将一直存在,直到系统关闭或所有进程取消映射该对象并且已使用shm_unlink(3)将其删除

POSIX shared memory objects have kernel persistence: a shared memory object will exist until the system is shut down, or until all processes have unmapped the object and it has been deleted with shm_unlink(3)

这篇关于如果其中一个进程意外死亡,进程间内存会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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