PROCESS_SHARED/ROBUST pthread_mutex_t dealocks instead of returning EOWNERDEAD

huangapple go评论79阅读模式
英文:

PROCESS_SHARED/ROBUST pthread_mutex_t dealocks instead of returning EOWNERDEAD

问题

在我的情况下,我需要在共享内存中使用一些锁,所以我使用了pthread_mutex_t对象。

每个pthread_mutex_t将如下初始化,以便可以在进程之间共享,并且在持有锁的情况下退出进程后可以还原。

pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
pthread_mutex_init(&m_lock, &attr);

我尝试通过以下步骤触发pthread_mutex_lock返回EOWNERDEAD情况:

  1. 启动一个进程并在共享内存中创建一个pthread_mutex_t,如上所述。
  2. 明确在shm中使用pthread_mutex_lock锁定并退出进程。
  3. 启动另一个进程,获取shm中的锁,并尝试使用pthread_mutex_lock再次锁定。

问题是,我在步骤3中观察到了死锁,即pthread_mutex_lock永远不会返回。

但根据pthread mutex文档中的说明,在这种情况下,pthread_mutex_lock应该返回EOWNERDEAD,表示持有锁的进程在持有锁的情况下退出。

测试环境如下:

  1. 5.15.0-1033-aws Ubuntu 20.04 LTS
  2. g++ --version
    g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
英文:

In my case, I need some lock in shm so I used pthread_mutex_t object.

Each pthread_mutex_t will be initialized as below so it can be shared between processes and is able to retore if the process exits while holding it.

pthread_mutexattr_t attr;                                                                                                                                                                            
pthread_mutexattr_init(&attr);                                                                                                                                                                       
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);                                                                                                                                         
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);                                                                                                                                            
pthread_mutex_init(&m_lock, &attr);  

I tried to trigger the pthread_mutex_lock returns EOWNERDEAD case through steps below:

  1. Start one process and create one pthread_mutex_t in shm as described above;
  2. pthread_mutex_lock the lock in shm explicitly and exit the process;
  3. Start another process, get the lock in shm and try to lock it again with pthread_mutex_lock;

The issue is that I observed in step 3 deadlock, i.e. the pthread_mutex_lock never returns.

But according to pthread mutex document, in this case the pthread_mutex_lock should return EOWNERDEAD, indicating the holding process exitted while holding the process.

Test env is :

  1. 5.15.0-1033-aws Ubuntu 20.04 LTS
  2. g++ --version
    g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

答案1

得分: 0

经过调查,我发现在共享内存中的鲁棒互斥锁仅在持有锁的进程/线程以异常方式退出时才标记为不一致。

当进程通过exit(0)或主函数成功返回时正常退出时,shm中的锁处于一致的锁定状态,因此会导致下一个锁操作永远阻塞。

英文:

After investigation, I found the robust mutex in shm is only marked as inconsistent when holding process/thread exits abnormally.

When the process exits normally through exit(0) or main function returns successfully, the lock in shm is in consistent locked status, hence causes the next lock operation blocks forever.

huangapple
  • 本文由 发表于 2023年5月29日 11:20:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定