在作業系統中的避免死鎖的銀行家演算法
程式運行到一半停止了

求大佬指教
#include <stdbool.h>
/* these may be any values >= 0 */
#define NUMBER_OF_CUSTOMERS 5
#define NUMBER_OF_RESOURCES 3
/* the available amount of each resource */
extern int available[NUMBER_OF_RESOURCES];
/* tne maximum demand of each customer */
extern int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
/* tne amount currently allocated to each customer */
extern int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
/* the remaining need of each customer */
extern int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
/* mutex lock */
pthread_mutex_t lock;
/* checks state safety. return ture if safe else false. */
bool is_safe()
{
/* Let work and finish be vectors of length
'NUMBER_OF_RESOURCES' and 'NUMBER_OF_CUSTOMERS' */
bool finish[NUMBER_OF_CUSTOMERS];
int work[NUMBER_OF_RESOURCES];
bool process_can = true;
int customer_count = 0;
bool safety_state = true;
for(int i=0; i<NUMBER_OF_CUSTOMERS; i++){
work[i] = available[i];
}
for(int i=0; i<NUMBER_OF_CUSTOMERS; i++){
finish[i] = false;
}
/*start of safety algorithm */
while(customer_count < NUMBER_OF_CUSTOMERS){
if(finish[customer_count] == false){
/* check if the process can be granted the resource */
for(int j=0; j<NUMBER_OF_RESOURCES; j++){
if(need[customer_count][j]>work[j]){//wrong state
process_can = false;
break;
}//if
}//for
/* process can finish */
if(process_can){
for(int k=0; k<NUMBER_OF_RESOURCES; k++){
work[k] += allocation[customer_count][k];
}//for
finish[customer_count] = true;
customer_count++;
}//if
else
break;
}//if
}//while
/* if finish[i] = true for all i, then the system */
for(int i=0; i<NUMBER_OF_CUSTOMERS; i++){
if(!finish[i]){
safety_state = false;
break;
}//if
}//for
return safety_state;
}
bool release_resources(int release[], int customer_num)
{
/* add resources */
for(int i=0; i<NUMBER_OF_RESOURCES; i++){
available[i] += release[i];
}
return true;
}
bool request_resources(int request[], int customer_num)
{
printf("\nCustomer %d is requesting resources: \n", customer_num);
for(int i=0; i<NUMBER_OF_RESOURCES; i++){
printf("%d", request[i]);
}
printf("\nAvailable resources: \n");
for(int i=0; i<NUMBER_OF_RESOURCES; i++){
printf("%d ", available[i]);
}
printf("\nThe need: \n");
for(int i=0; i<NUMBER_OF_RESOURCES; i++){
printf("%d ", need[customer_num][i]);
}
printf("\n");
for (int i=0; i<NUMBER_OF_RESOURCES; i++){
if(request[i] <= need[customer_num][i]){
if(request[i] > available[i]){
printf("NOT SAFE with this request (request > available)\n");
return false;
}//if
else{//take resources
if(is_safe()){
printf("The system is safe.\nResources granted.\n");
for(int i=0; i<NUMBER_OF_RESOURCES; i++){
available[i] -= request[i];
allocation[customer_num][i] += request[i];
need[customer_num][i] -= request[i];
}//for
return true;
}//if
else{
printf("NOT SAFE! Can't grant resources.\n");
return false;
}//if-else
}//if-else
}//if
else{
printf("The request is more than the need. Abort!\n");
return false;
}//if-else
}//for
}
void release_resources_mutex(int customer_num)
{
pthread_mutex_lock(&lock);
release_resources(allocation[customer_num], customer_num);
pthread_mutex_unlock(&lock);
printf("Thread %d finished execution.\n", customer_num);
}
bool request_resources_mutex(int request[], int customer_num)
{
bool released = false;
pthread_mutex_lock(&lock);
released = request_resources(request, customer_num);
pthread_mutex_unlock(&lock);
return released;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include "banker.h"
#define NUMBER_OF_CUSTOMERS 5
#define NUMNER_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] = {
{7,5,3},
{3,2,2},
{9,0,2},
{2,2,2},
{4,3,3}};
/* the amount currently allocated to each customer */
int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] = {
{0,1,0},
{2,0,0},
{3,0,2},
{2,1,1},
{0,0,2}};
/*the remaining need of each customer */
int need[ NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
pthread_t tid[NUMBER_OF_CUSTOMERS];
void *get_resources(void *arg);
int main(int argc, char *argv[])
{
pthread_attr_t attr;
if(argc != NUMBER_OF_RESOURCES+1){
printf("Usage: a.out<# of src 1><# of src 2><# of src 3>\n");
exit(1);
}
for(int i=1; i<argc; i++){
if(atoi(argv[i]) < 0){
printf("Argument %d must be no negative\n", atoi(argv[i]));
exit(1);
}
}
/* setting up the available vector */
for(int i=0; i<NUMBER_OF_RESOURCES; i++){
available[i] = atoi(argv[i+1]);
}
for(int i=0; i<NUMBER_OF_CUSTOMERS; i++)
for(int j=0; j<NUMBER_OF_RESOURCES; j++)
need[i][j] = maximum[i][j] - allocation[i][j];
/*set the default attributes */
pthread_attr_init(&attr);
int pthread_num[] = {0,1,2,3,4};
/* create the threads */
for (int k=0; k<NUMBER_OF_CUSTOMERS; k++){
pthread_create(&tid[k], &attr, get_resources, &pthread_num[k]);
}
/* wait for the threads to exit */
for(int i=0; i<NUMBER_OF_CUSTOMERS; i++){
pthread_join(tid[i], NULL);
}
printf("Reached the end of the program!\n");
pthread_mutex_destroy(&lock);
// sleep(10);
return 0;
}
void *get_resources(void *arg)
{
bool released = false;
int customer_num = *(int *)arg;
int request_one[] = {1,0,2};
int request_two[] = {3,3,0};
int request_three[] = {0,2,0};
request_resources_mutex(request_one, customer_num);
request_resources_mutex(request_two, customer_num);
request_resources_mutex(request_three, customer_num);
release_resources_mutex(customer_num);
}
uj5u.com熱心網友回復:
是崩潰了嗎,還是死鎖了?uj5u.com熱心網友回復:
我也不知道,用別人的代碼運行出來也是這樣。uj5u.com熱心網友回復:
可以除錯一下看看uj5u.com熱心網友回復:
好的,我去試試,謝謝轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63054.html
標籤:C++ 語言
上一篇:ACM輸入問題
下一篇:Qt中的ui設計命名問題
