银行家的pthreads算法

时间:2014-11-24 05:25:01

标签: c pthreads

我正在尝试在c中编写一个Banker算法问题,但是我遇到了分段错误。我已经多次运行了我的代码,但我似乎无法弄清楚故障的位置。我对互斥体和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);
}

2 个答案:

答案 0 :(得分:1)

int threadsi = 3;
int threadsj = 3;
:
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);
    }
}

您似乎正在启动三个线程,然后加入九个,其中六个将具有零填充的线程ID值。

这不太可能结束: - )

答案 1 :(得分:1)

inc_count

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

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

但你已宣布:

int Need[3][3]

如果n和/或m是&gt; = 3,该怎么办? Segfault!

此外,您有嵌套for循环,索引ij从0到10,然后您使用Allocation[i][j]但是您已静态分配它维度3x3:

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