【问题标题】:What happens if I do not call pthread_mutex_destroy when using PTHREAD_PROCESS_SHARED如果我在使用 PTHREAD_PROCESS_SHARED 时不调用 pthread_mutex_destroy 会发生什么
【发布时间】:2020-08-02 22:43:14
【问题描述】:

在 Linux 上,可以通过使用 PTHREAD_PROCESS_SHARED 属性在进程之间共享互斥体,然后将其保存在可由许多进程使用的映射文件中。

这是https://linux.die.net/man/3/pthread_mutexattr_init 中完成上述工作的示例:

For example, the following code implements a simple counting semaphore in a mapped file that may be used by many processes.

/* sem.h */
struct semaphore {
    pthread_mutex_t lock;
    pthread_cond_t nonzero;
    unsigned count;
};
typedef struct semaphore semaphore_t;

semaphore_t *semaphore_create(char *semaphore_name);
semaphore_t *semaphore_open(char *semaphore_name);
void semaphore_post(semaphore_t *semap);
void semaphore_wait(semaphore_t *semap);
void semaphore_close(semaphore_t *semap);

/* sem.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
#include "sem.h"

semaphore_t *
semaphore_create(char *semaphore_name)
{
int fd;
    semaphore_t *semap;
    pthread_mutexattr_t psharedm;
    pthread_condattr_t psharedc;

    fd = open(semaphore_name, O_RDWR | O_CREAT | O_EXCL, 0666);
    if (fd < 0)
        return (NULL);
    (void) ftruncate(fd, sizeof(semaphore_t));
    (void) pthread_mutexattr_init(&psharedm);
    (void) pthread_mutexattr_setpshared(&psharedm,
        PTHREAD_PROCESS_SHARED);
    (void) pthread_condattr_init(&psharedc);
    (void) pthread_condattr_setpshared(&psharedc,
        PTHREAD_PROCESS_SHARED);
    semap = (semaphore_t *) mmap(NULL, sizeof(semaphore_t),
            PROT_READ | PROT_WRITE, MAP_SHARED,
            fd, 0);
    close (fd);
    (void) pthread_mutex_init(&semap->lock, &psharedm);
    (void) pthread_cond_init(&semap->nonzero, &psharedc);
    semap->count = 0;
    return (semap);
}

semaphore_t *
semaphore_open(char *semaphore_name)
{
    int fd;
    semaphore_t *semap;

    fd = open(semaphore_name, O_RDWR, 0666);
    if (fd < 0)
        return (NULL);
    semap = (semaphore_t *) mmap(NULL, sizeof(semaphore_t),
            PROT_READ | PROT_WRITE, MAP_SHARED,
            fd, 0);
    close (fd);
    return (semap);
}

void
semaphore_post(semaphore_t *semap)
{
    pthread_mutex_lock(&semap->lock);
    if (semap->count == 0)
        pthread_cond_signal(&semapx->nonzero);
    semap->count++;
    pthread_mutex_unlock(&semap->lock);
}

void
semaphore_wait(semaphore_t *semap)
{
    pthread_mutex_lock(&semap->lock);
    while (semap->count == 0)
        pthread_cond_wait(&semap->nonzero, &semap->lock);
    semap->count--;
    pthread_mutex_unlock(&semap->lock);
}

void
semaphore_close(semaphore_t *semap)
{
    munmap((void *) semap, sizeof(semaphore_t));
}
The following code is for three separate processes that create, post, and wait on a semaphore in the file /tmp/semaphore. Once the file is created, the post and wait programs increment and decrement the counting semaphore (waiting and waking as required) even though they did not initialize the semaphore.

/* create.c */
#include "pthread.h"
#include "sem.h"

int
main()
{
    semaphore_t *semap;

    semap = semaphore_create("/tmp/semaphore");
    if (semap == NULL)
        exit(1);
    semaphore_close(semap);
    return (0);
}

/* post */
#include "pthread.h"
#include "sem.h"

int
main()
{
    semaphore_t *semap;

    semap = semaphore_open("/tmp/semaphore");
    if (semap == NULL)
        exit(1);
    semaphore_post(semap);
    semaphore_close(semap);
    return (0);
}

/* wait */
#include "pthread.h"
#include "sem.h"

int
main()
{
    semaphore_t *semap;

    semap = semaphore_open("/tmp/semaphore");
    if (semap == NULL)
        exit(1);
    semaphore_wait(semap);
    semaphore_close(semap);
    return (0);
}

但是在共享互斥体上调用 pthread_mutex_destroy() 很棘手,因为它可能导致其他进程出错,并且上面的示例也没有调用 pthread_mutex_destroy()。所以我想不要破坏它。

我的问题是:如果我初始化 PTHREAD_PROCESS_SHARED 互斥体,将其保存到映射文件并在许多进程上永久使用它而不调用 pthread_mutex_destroy() 或重新初始化它,是否安全?

【问题讨论】:

  • 当映射文件的最后一个进程关闭它时,一切都会被破坏。互斥体只是一堆用于以某种方式工作的内存。我想说,只要您不尝试重新映射同一个文件,并继续使用其足迹仍然存在的同一个互斥锁,而不重新启动互斥锁,一切都会好起来的。

标签: c++ c linux pthreads mutex


【解决方案1】:

我的问题是:如果我初始化 PTHREAD_PROCESS_SHARED 互斥体,将其保存到映射文件并在许多进程上永久使用它而不调用 pthread_mutex_destroy() 或重新初始化它,是否安全?

允许进程共享互斥体比初始化它的进程寿命更长。如果您将这样的互斥体映射到一个持久的常规文件,那么它的状态将无限期地持续下去,即使没有进程映射它。只要保持其状态的完整性——包括但不限于,没有进程通过pthread_mutex_destroy() 破坏它——新进程就可以映射并使用它。也就是说,你描述的语义是明确的。

但是它安全吗?不是特别。

第一个问题是您需要知道何时创建它,并且在创建时需要避免竞争条件。如果您依赖经常使用互斥锁的进程在需要时对其进行初始化,那么您必须确保在文件尚不存在时创建并初始化它。

另一个问题是,使用这样的长期共享互斥体会产生大量的故障风险。例如,如果程序在保持互斥锁锁定时崩溃,那么它将保持锁定状态,直到您采取某种手动纠正措施。或者,如果直接操作映射文件,则互斥锁状态很容易被破坏,从而在所有使用它的程序中产生未定义的行为——即使在重新启动时也是如此。

如果你真的需要一个长期存在的同步对象,那么我建议考虑a POSIX named semaphore。它是为此目的而设计的,并考虑了上述考虑因素和其他因素。然而,它有些不同,因为此类信号量驻留在内核中并具有内核持久性,因此它们不会在重新启动后持久存在(这通常是一件好事),并且它们不易受到普通文件的影响操纵。

或者,您可以考虑a System V semaphore。这是一个较旧的信号量实现,也具有内核持久性。它的界面比 POSIX 信号量要笨重得多,但它具有一些 POSIX 信号量没有的有用功能,例如当持有一个锁定的进程终止(甚至异常)时提供自动解锁。

【讨论】:

猜你喜欢
  • 2011-04-02
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
相关资源
最近更新 更多