Cinolt Yuklair Cinolt Yuklair - 1 year ago 107
Linux Question

pthread mutexattr process-shared memory leak

By setting the

attribute of a mutex (with 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.

So, if I create a mutex inside an
d file that multiple processes can access, then I
the file, will that cause a kernel-persistent memory leak? If so, where exactly does the data for the zombie mutex reside from an implementation viewpoint?

Answer Source

Cinolt, why do you think that published linux man pages? You have link, but it is hard to find where did find the text; author and date are not listed; and there is note on page:


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.

There is by Michael Kerrisk as home page of linux man-pages project: and here is man of pthread_mutexattr_init It is very short and have no information about leak.

In POSIX some implementation may use some in-kernel structures to save mutexattr properties; for such implementations to not have memory leak, POSIX requires programmer to destroy all created attrs.

There is no leak (existing beyond process lifetime) warnings on - Single UNIX ® Specification, Version 2 - Derived from the POSIX Threads Extension (1003.1c-1995)

But it is here in (The Open Group Base Specifications Issue 6 - IEEE Std 1003.1, 2004 Edition); with example just like one on page:

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.

In actual implementation in the GNU glibc, usually present in Linux variations (NPTL - there is no additional in-kernel structure allocation:

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

And destroy is just NOP:

22 int
23 __pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
24 {
25   return 0;
26 }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download