我正在研究银行家算法并使用循环来创建我的线程。问题是,当应该创建 5 个线程时,循环只创建 4 个线程。我检查了我的循环,除非我遗漏了什么,否则一切似乎都是正确的。
/* these may be any values >= 0 */
#define NUMBER_OF_CUSTOMERS 5
#define NUMBER_OF_RESOURCES 3
/* the available amount of each resource */
int available[NUMBER_OF_RESOURCES];
/*the maximum demand of each customer */
int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
/* the amount currently allocated to each customer */
int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
/* the remaining need of each customer */
int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
int release_resources(int customer_num, int release[]){
allocation[customer_num][NUMBER_OF_RESOURCES];
return 1;
}
pthread_mutex_t mutex;
int main(int argc, char *argv[]){
pthread_mutex_init(&mutex, NULL);
pthread_t threads [3];
int result;
unsigned index;
for(index = 0; index < NUMBER_OF_RESOURCES; index++){
available[index] = strtol(argv[index+1], NULL,10);
}
for(index = 0; index < NUMBER_OF_CUSTOMERS; ++index){
printf("\nCreating thead %d", index);
result = pthread_create(&threads[index],NULL,release_resources,1);
}
//printf("Done");
return 0;
}