C中带有信号灯的死锁

时间:2019-05-14 23:01:39

标签: c multithreading deadlock semaphore dining-philosopher

我有一个关于餐厅哲学家计划的僵局的任务。我被要求制作一个名为“ sections1.c”的文件,该文件存在死锁问题,完成死锁后,我将复制代码并解决文件“ sections2.c”中的死锁情况。我的老师让我们通过MDAT进行编程。正如他在MDAT指南中向我们提供的那样,MDAT应该像信号量和pthread函数一样运行。

void mdat_mutex_init(const char *name, pthread_mutex_t *lock,
pthread_mutexattr_t *attr);

void mdat_mutex_lock(pthread_mutex_t *lp);

void mdat_mutex_unlock(pthread_mutex_t *lp);

void mdat_sem_init(const char *name, sem_t *sem, int pshared, int value);

void mdat_sem_wait(sem_t *sp);

void mdat_sem_post(sem_t *sp);

MDAT应该由调度程序负责,并通过播种当前运行时间来随机调度胎面。

在不允许编辑的主文件中,函数sectionInitGlobals运行一次,sectionRunPhilosopher随每个pthread_create运行一次。

一个问题可能是我以前从未使用过信号灯,并且使用不正确。

//  sections1.c: mutual exclusion model sections

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include "sections.h"
#include "mdat.h"

// TODO: Declare shared variables here
int numPhils;
sem_t * sem_arr;

void sectionInitGlobals(int numPhilosophers)
{
  // TODO: Complete this function
  int i;

  char char_arr[50];

  sem_t arr[numPhilosophers];

  numPhils = numPhilosophers;

  for(i = 0; i < numPhilosophers; i++)
  {
    sprintf(char_arr,"%d", i);
    mdat_sem_init(char_arr, &arr[i], 0, 0);
  }

  sem_arr = arr;
}

void sectionRunPhilosopher(int philosopherID, int numRounds)
{
  int lChopstick = philosopherID;
  int rChopstick;

  int left;
  int right;

  int i;
  int hasEaten;
  int hasLeft;
  int hasRight;

  if(philosopherID == 0)
    rChopstick = numPhils - 1;
  else
    rChopstick = philosopherID - 1;

  for(i = 0; i < numRounds; i++)
  {
    hasEaten = 0;
    hasLeft = 0;
    hasRight = 0;

    while(hasEaten == 0)
    {
      sem_getvalue(&sem_arr[lChopstick], &left);
      if(left >= 0 || hasLeft == 1)
      {
        hasLeft = 1;
      }
      else
      {
        mdat_sem_wait(&sem_arr[lChopstick]);
      }

      sem_getvalue(&sem_arr[rChopstick], &right);
      if(right >= 0 && hasLeft != 0)
      {
        hasRight = 1;
      }
      else
      {
        mdat_sem_wait(&sem_arr[rChopstick]);
      }

      if(hasLeft != 0 && hasRight != 0)
      {
        eat();
        hasEaten = 1;
        mdat_sem_post(&sem_arr[lChopstick]);
        mdat_sem_post(&sem_arr[rChopstick]);
      }
    }

    think();
  }
}

在测试我的代码时,我可以在任意数量的线程和回合中运行它,但是,似乎可以保证使用更多线程可以确保死锁。当我运行具有100个线程的代码时,每次都会到达一个死锁,但是拥有更多线程时,它难道没有更少的机会陷入死锁吗?

结果:

具有16个线程和10个回合的死锁有时取决于调度程序。

具有6个线程和5个回合,死锁的发生率为0%。

具有100个线程和5个回合,死锁会在100%的时间内发生。

编辑:

当没有死锁发生并且程序认为死锁发生时跟踪文件的结尾:

无死锁:

THREAD: 3
SECTION: DoneRounds
MESSAGE: Thread 3 has completed
*******************************************************************************
|ID   |PROPERTY     |LOC  |SECTION      |STATUS       |WAITING ON             |
|0    |0            |19   |             |completed    |                       |
|1    |1            |19   |             |completed    |                       |
|2    |2            |19   |             |completed    |                       |
|3    |3            |19   |             |completed    |                       |
|4    |4            |19   |             |completed    |                       |
|5    |5            |19   |             |completed    |                       |
|6    |6            |19   |             |completed    |                       |
|7    |7            |19   |             |completed    |                       |
|8    |8            |19   |             |completed    |                       |
|9    |9            |19   |             |completed    |                       |
|10   |10           |19   |             |completed    |                       |
|11   |11           |19   |             |completed    |                       |
|12   |12           |19   |             |completed    |                       |
|13   |13           |19   |             |completed    |                       |
|14   |14           |19   |             |completed    |                       |
|15   |15           |19   |             |completed    |                       |
-------------------------------------------------------------------------------
|LOCK NAME                |STATUS       |WAITING THREADS                      |
|lock_a                   |unlocked     |                                     |
|lock_b                   |unlocked     |                                     |
-------------------------------------------------------------------------------
|SEMAPHORE NAME           |VALUE        |WAITING THREADS                      |
|0                        |20           |                                     |
|1                        |20           |                                     |
|2                        |20           |                                     |
|3                        |20           |                                     |
|4                        |20           |                                     |
|5                        |20           |                                     |
|6                        |20           |                                     |
|7                        |20           |                                     |
|8                        |20           |                                     |
|9                        |20           |                                     |
|10                       |20           |                                     |
|11                       |20           |                                     |
|12                       |20           |                                     |
|13                       |20           |                                     |
|14                       |20           |                                     |
|15                       |20           |                                     |
*******************************************************************************
***** Program Trace End *****

死锁:

THREAD: 13
SECTION: DoneRounds
MESSAGE: Thread 13 has completed
*******************************************************************************
|ID   |PROPERTY     |LOC  |SECTION      |STATUS       |WAITING ON             |
|0    |0            |19   |             |completed    |                       |
|1    |1            |32   |             |waiting-sem  |1                      |
|2    |2            |32   |             |waiting-sem  |2                      |
|3    |3            |38   |             |waiting-sem  |2                      |
|4    |4            |19   |             |completed    |                       |
|5    |5            |19   |             |completed    |                       |
|6    |6            |19   |             |completed    |                       |
|7    |7            |19   |             |completed    |                       |
|8    |8            |19   |             |completed    |                       |
|9    |9            |32   |             |waiting-sem  |9                      |
|10   |10           |38   |             |waiting-sem  |9                      |
|11   |11           |19   |             |completed    |                       |
|12   |12           |19   |             |completed    |                       |
|13   |13           |19   |             |completed    |                       |
|14   |14           |19   |             |completed    |                       |
|15   |15           |19   |             |completed    |                       |
-------------------------------------------------------------------------------
|LOCK NAME                |STATUS       |WAITING THREADS                      |
|lock_a                   |unlocked     |                                     |
|lock_b                   |unlocked     |                                     |
-------------------------------------------------------------------------------
|SEMAPHORE NAME           |VALUE        |WAITING THREADS                      |
|0                        |10           |                                     |
|1                        |-1           |1                                    |
|2                        |-2           |2 3                                  |
|3                        |10           |                                     |
|4                        |20           |                                     |
|5                        |20           |                                     |
|6                        |20           |                                     |
|7                        |20           |                                     |
|8                        |10           |                                     |
|9                        |-2           |9 10                                 |
|10                       |10           |                                     |
|11                       |20           |                                     |
|12                       |20           |                                     |
|13                       |20           |                                     |
|14                       |20           |                                     |
|15                       |20           |                                     |
*******************************************************************************
ERROR! VIOLATION: No ready threads to schedule - possible DEADLOCK
***** Program Trace End *****

回答后编辑

感谢梅维斯! 最终代码: section1.c-想要死锁

//  sections1.c: mutual exclusion model sections

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include "sections.h"
#include "mdat.h"

// TODO: Declare shared variables here
int numPhils;
sem_t * sem_arr;

void sectionInitGlobals(int numPhilosophers)
{
  // TODO: Complete this function
  int i;

  char char_arr[50];

  sem_t arr[numPhilosophers];

  numPhils = numPhilosophers;

  for(i = 0; i < numPhilosophers; i++)
  {
    sprintf(char_arr,"%d", i);
    mdat_sem_init(char_arr, &arr[i], 0, 1);
  }

  sem_arr = arr;
}

void sectionRunPhilosopher(int philosopherID, int numRounds)
{
  int lChop = philosopherID;
  int rChop;

  int i;

  if(philosopherID == 0)
    rChop = numPhils - 1;
  else
    rChop = philosopherID - 1;

  for(i = 0; i < numRounds; i++)
  {
    mdat_sem_wait(&sem_arr[lChop]);
    mdat_sem_wait(&sem_arr[rChop]);
    eat();
    mdat_sem_post(&sem_arr[rChop]);
    mdat_sem_post(&sem_arr[lChop]);

    think();
  }
}

section2.c-不需要死锁

//  sections1.c: mutual exclusion model sections

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include "sections.h"
#include "mdat.h"

// TODO: Declare shared variables here
int numPhils;
sem_t * sem_arr;

void sectionInitGlobals(int numPhilosophers)
{
  // TODO: Complete this function
  int i;

  char char_arr[50];

  sem_t arr[numPhilosophers];

  numPhils = numPhilosophers;

  for(i = 0; i < numPhilosophers; i++)
  {
    sprintf(char_arr,"%d", i);
    mdat_sem_init(char_arr, &arr[i], 0, 1);
  }

  sem_arr = arr;
}

void sectionRunPhilosopher(int philosopherID, int numRounds)
{
  int lChop = philosopherID;
  int rChop;

  int i;

  if(philosopherID == 0)
    rChop = numPhils - 1;
  else
    rChop = philosopherID - 1;

  for(i = 0; i < numRounds; i++)
  {
    if(philosopherID != numPhils - 1)
    {
      mdat_sem_wait(&sem_arr[lChop]);
      mdat_sem_wait(&sem_arr[rChop]);
      eat();
      mdat_sem_post(&sem_arr[rChop]);
      mdat_sem_post(&sem_arr[lChop]);
    }
    else
    {
      mdat_sem_wait(&sem_arr[rChop]);
      mdat_sem_wait(&sem_arr[lChop]);
      eat();
      mdat_sem_post(&sem_arr[lChop]);
      mdat_sem_post(&sem_arr[rChop]);
    }

    think();
  }
}

1 个答案:

答案 0 :(得分:1)

经典的餐饮哲学家问题有N个哲学家和N个叉子;但每个人需要2叉子吃。给定的分叉信号量的最大值可能为1(可用),最小值为-1(一个拥有分叉,一个正在等待分叉)。您的叉子的值是10和20?

按照您的逻辑,检查信号量的值,如果> = 0,则表示您“拥有”,然后继续检查另一个信号量;但是你没有。在您等待 之前,您没有信号灯。 eat()ing之后,即使您可能从未等待过它们中的任何一个,也可以将它们都张贴到它们上。这就是为什么信号量的值如此之高的原因。

第二,在返回 sem_get_value 时,信号量的值可能已更改。这是一个常见的同步问题,因此很常见,它的名称为:TOCTOU(检查时间到使用时间)。您需要使用一种机制,您可以根据行动做出决定,而不仅仅是检查状态。

第三,您将其更改为有效地循环放置:

  sem_wait(left);
  sem_wait(right);
  eat();
  sem_post(right);
  sem_post(left);

您将遇到一个完全不同的同步问题,这就是餐饮哲学家打算说明的问题。狩猎愉快!