c - 使用 pthreads 的银行家算法

标签 c pthreads

我正在尝试用 c 语言编写银行家算法问题,但出现段错误。我已经多次运行我的代码,但我似乎无法弄清楚错误出在哪里。我对互斥量和 pthread 库还很陌生。任何帮助,将不胜感激。谢谢

//Bankers Algorithm
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

#define MAXN 10             /* maximum number of processes                   */
#define MAXM 10             /* maximum number of resource types              */
int ProcCurr[3][3];     /* 3 threads(processes), 3 resources    */
int temp[3][3];       /* temp array location           */
int Available[10];        /* Available[m] = # resources unallocated */
int Max[3][3] = { {10,10,10},{10,10,10},{10,10,10} }; /* Max[n][m] = max demand of processes n for resource m    */
int Allocation[3][3] = { {1,2,3},{3,2,1},{1,1,1} }; /* Allocation[n][m] = # resources m allocated to processes n*/
int Need[3][3];       /* Need[n][m] = resources m needed by processes n         */
int counti = 0;             /* Need[n][m] = Max[n][m] - Allocation[n][m]     */
int countj = 0;
int threadsi = 3;
int threadsj = 3;

void *inc_count(void *r);
void *watch_count(void *r);

pthread_mutex_t mutex; /*mutex id*/
pthread_cond_t count_threshold_cv;

int main()
{
  pthread_t ProcCurr[3][3]; /*id of thread*/
  pthread_attr_t attr;
  int  i, j;
  long r1 = 1,r2 = 2,r3 = 3;

  if(pthread_mutex_init(&mutex, NULL) < 0){
     perror("Pthread_mutex_init error.");
     exit(1);    
     }
  else
     //pthread_mutex_init(&mutex, NULL);

  pthread_cond_init(&count_threshold_cv, NULL);

  pthread_attr_init(&attr); /*get default attributes*/

  pthread_create(&ProcCurr[0][0], &attr, watch_count, (void *)r1);
  pthread_create(&ProcCurr[1][0], &attr, inc_count, (void *)r2);
  pthread_create(&ProcCurr[2][0], &attr, inc_count, (void *)r3);


  for(i=0; i<=threadsi; i++){ 
      for(j=0; j<=threadsj; j++){ 
          //pthread_join(ProcCurr[i][j],NULL); /*wait for thread to exit*/
          pthread_join(ProcCurr[0][0],NULL); 
          pthread_join(ProcCurr[1][0],NULL);             
          pthread_join(ProcCurr[2][0],NULL); 
          }
      }
  printf("Main: waited on %d, %d threads. Done.\n", threadsi, threadsj);

  pthread_attr_destroy(&attr);
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&count_threshold_cv);
  pthread_exit(NULL);

}

void *inc_count(void *r)
{  /*processes are running, thread of process is initalize to something <=3, each threads request up to 3 resources, when all resources are commited then next thread will have to wait (mutex goes to resource from a thread letting other threads know not to this resource)*/
  int i, j, n, m;
  long my_id = (long)r;

  for(i=0; i<10; i++){
      for(j=0; j<10; j++){
    Need[n][m] = Max[n][m] - Allocation[i][j];
    printf("Allocation = %d, Need = %d\n", Allocation[i][j], Need[n][m]);
    }
    pthread_mutex_lock(&mutex);
    if(counti == MAXN && countj == MAXM){
       pthread_cond_signal(&count_threshold_cv);
       printf("inc_count: thread %ld, Need = %d. Threshold reached.\n",my_id, Need[n][m]);
       }
    printf("inc_count: thread %ld, Need = %d. Unlocking mutex.\n", my_id, Need[n][m]);
    pthread_mutex_unlock(&mutex);
    sleep(1);
    watch_count(r);
    }
  pthread_exit(NULL);
  watch_count(r);

}

void *watch_count(void *r)
{
  long my_id = (long)r;
  int n, m;

  printf("Start watch_count: thread %ld\n", my_id);

   while(counti < MAXN && countj <MAXM)
  { pthread_mutex_lock(&mutex);
   Available[n] = Max[n][m] - Allocation[counti++][countj++];
   printf("Available = %d\n", Available[n]);
   pthread_cond_wait(&count_threshold_cv, &mutex);
   printf("watch_count: thread %ld, available = %d. Conditional Signal Received.\n", my_id, Available[m]);
   countj++;
   printf("watch_count: thread %ld, Need now = %d.\n", my_id, Need[counti][countj]);
  }
  pthread_mutex_unlock(&mutex);
  pthread_exit(NULL);
}

最佳答案

inc_count 中:

您没有初始化 nm 但您正在使用它们:

Need[n][m] = Max[n][m] - Allocation[i][j];

但是你已经声明:

int Need[3][3]

如果 n 和/或 m >= 3 怎么办?段错误!

此外,您还嵌套了 for 循环,索引 ij 从 0 到 10,那么您正在使用 Allocation[i][j] 但是你已经用 3x3 维度静态分配了它:

int Allocation[3][3] = { {1,2,3},{3,2,1},{1,1,1} };

关于c - 使用 pthreads 的银行家算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27098349/

相关文章:

C:信号函数(参数?)

c - 递归函数中的逻辑错误

c - 为什么程序打印 0?

c++ - 检查 Linux 功能以设置线程优先级

c++ - 如何在C++中实现线程

c - pthread_setname_np() 设置功能时

iphone - 从 C 到 Objective-C 的回调方法

c++ - 从用户获取字符串中的多个单词的代码

linux - 关于 POSIX 线程上互斥锁的问题

c - 线程执行问题