91-互斥量的鲁棒属性

上一文有一个遗留练习,很重要,不知道同学们有没有做。先说结论吧,如果其中一个进程在未释放互斥量的情况下挂掉了,将会导致另一个线程永远无法获得锁,然后就死锁了。

为了能够让进程在异常终止时,释放掉互斥锁,需要指定 ROBUST 属性。所谓的 ROBUST,指是的健壮的意思。

1. 相关函数

可以通过下面两个函数设置和获取互斥量的鲁棒属性:

int pthread_mutexattr_getrobust(const pthread_mutexattr_t *attr, int *restrict robust);

int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr, int robust);

2. 互斥量状态一致性

在指定 robust 属性的情况下,如果其中某个进程在未释放锁的情况下退出了,另一个进程仍然可以获得锁,但是此时 pthread_mutex_lock 将返回 EOWNERDEAD,通知获得锁的线程,有一个其它进程的线程挂掉了,互斥量现在变成了 inconsistent 的状态。这时候,需要对互斥量做 consistent 处理,否则,一旦再次解锁后,互斥量将永久不可用。

翻译成代码就是这样的:

if (EOWNERDEAD == pthread_mutex_lock(&lock)) {
  pthread_mutex_consistent(&lock);
}

consistent 函数原型如下:

int pthread_mutex_consistent(pthread_mutex_t *mutex);

它表示将 robust mutex 标记为 consistent 状态。

3. 程序清单

我们仍使用上一篇文章中的 init、destroy 程序,下面给出 rbstbuyticket.c 的代码。

3.1 代码

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>

#define PERR(msg) do { perror(msg); exit(-1); } while(0)
#define PPERR(err, msg) do { err = errno; perror(msg); exit(-1); } while(0)

struct ticket {
  int remain;
  pthread_mutex_t lock;
};

int main(int argc, char* argv[]) {
  if (argc < 2) {
    printf("Usage: %s <name>\n", argv[0]);
    exit(-1);
  }

  char *name = argv[1];
  int err, shared, flag = 1;
  key_t key = 0x8888;
  int id = shmget(key, 0, 0); 
  if (id < 0) PERR("shmget");

  struct ticket *t = (struct ticket*)shmat(id, NULL, 0); 
  
  if ((int)t == -1) PERR("shmat");

  while(flag) {
    if (EOWNERDEAD == (err = pthread_mutex_lock(&t->lock))) {
      puts("EOWNERDEAD");
      pthread_mutex_consistent(&t->lock);
    }   
    else if (ENOTRECOVERABLE == err) {
      puts("ENOTRECOVERABLE");
    }   
    int remain = t->remain;
    if (remain > 0) {
      sleep(1);
      printf("%s buy a ticket\n", name);
      --remain;
      sleep(3);
      t->remain = remain;
    }   
    else flag = 0;
    pthread_mutex_unlock(&t->lock);
    sleep(2);
  }

  return 0;
}

3.2 Makefile 文件

添加了两行,现在如下:

main:init destroy buyticket rbstbuyticket 
init:init.c
  gcc init.c -o init -lpthread
destroy:destroy.c
  gcc destroy.c -o destroy -lpthread
buyticket:buyticket.c
  gcc buyticket.c -o buyticket -lpthread
rbstbuyticket:rbstbuyticket.c
  gcc rbstbuyticket.c -o rbstbuyticket -lpthread

3.3 编译和运行

  • 编译
$ make
  • 未指定 robust 下的运行结果

这里写图片描述

图1 未指定 robust 导致死锁
  • 指定 robust 的运行结果(init 带参数)

这里写图片描述

图2 指定 robust 后正常

细心的同学发现,allen 抢了两张票,而 luffy 抢了 4 张票!!! 不过这个问题似乎是没办法解决……如果你有解决方案,请在评论区留言。

4. 总结

  • 理解 robust 属性
  • 注意对 mutex 做一致性处理
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值