pthread mutexattr 进程共享内存泄漏

pthread mutexattr process-shared memory leak

通过设置互斥锁的 process-shared 属性(使用 pthread_mutexattr_setpshared), it allows a mutex to exist beyond the lifetime of the process that created it, according to the manpage for pthread_mutexattr_init:

In particular, these processes may exist beyond the lifetime of the initializing process.

因此,如果我在多个进程可以访问的 mmap(2)d 文件中创建互斥锁,然后我 unlink(2) 该文件,是否会导致内核持久内存泄漏?如果是这样,从实现的角度来看,僵尸互斥体的数据究竟位于何处?

Cinolt,您认为 linux.die.net 发布 linux 手册页的原因是什么?您有 https://linux.die.net/man/3/pthread_mutexattr_init link,但很难找到 linux.die.net 在哪里找到了文本;未列出作者和日期;页面上有注释:

Prolog

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.

Michael Kerrisk 的 man7.org 作为 linux 手册页项目的主页:https://www.kernel.org/doc/man-pages/ 这是 pthread_mutexattr_init 的人 http://man7.org/linux/man-pages/man3/pthread_mutexattr_init.3p.html。它很短,没有关于泄漏的信息。

在POSIX中,一些实现可能会使用一些内核结构来保存 mutexattr 属性;对于这样的实现没有内存泄漏,POSIX 要求程序员销毁所有创建的属性。

http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutexattr_init.html - 单一 UNIX® 规范,版本 2 - 源自 POSIX 线程扩展 (1003.1c-1995)[=22] 上没有泄漏(存在于进程生命周期之外)警告=]

但它在 http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutexattr_destroy.html 中(The Open Group Base Specifications Issue 6 - IEEE Std 1003.1, 2004 Edition);例如 die.net 页面上的示例:

Synchronization variables that are initialized with the PTHREAD_PROCESS_SHARED process-shared attribute may be operated on by any thread in any process that has access to it. In particular, these processes may exist beyond the lifetime of the initializing process. For example, the following code implements a simple counting semaphore in a mapped file that may be used by many processes.

在 GNU glibc 的实际实现中,通常出现在 Linux 变体中(NPTL - https://en.wikipedia.org/wiki/Native_POSIX_Thread_Library),没有额外的内核结构分配:

http://code.metager.de/source/xref/gnu/glibc/nptl/pthread_mutexattr_init.c

24 __pthread_mutexattr_init (pthread_mutexattr_t *attr)
26   if (sizeof (struct pthread_mutexattr) != sizeof (pthread_mutexattr_t))
27     memset (attr, '[=10=]', sizeof (*attr));
28
32   ((struct pthread_mutexattr *) attr)->mutexkind = PTHREAD_MUTEX_NORMAL;

销毁只是 NOP:http://code.metager.de/source/xref/gnu/glibc/nptl/pthread_mutexattr_destroy.c

22 int
23 __pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
24 {
25   return 0;
26 }