gpt4 book ai didi

c - 银行家算法线程创建未完全运行传递的方法

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:50 25 4
gpt4 key购买 nike

当我从 main 中的线程创建调用它时,我的 request_resources 方法没有运行。它应该是创建线程、请求资源、检查安全状态然后退出。我不确定为什么它在 2 个线程后停止并且没有给出方法中测试语句的输出。

        #include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>


/* 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];

pthread_mutex_t mutex =
PTHREAD_MUTEX_INITIALIZER;

int safe_state(int customer_num){
int work[NUMBER_OF_CUSTOMERS];
int done;
for(int w = 0; w < NUMBER_OF_CUSTOMERS; w++){
work[w] = available[w];
printf("%d", work[w]);
}

int finish[NUMBER_OF_CUSTOMERS];

for(int i = 0; i < NUMBER_OF_CUSTOMERS; i++){
finish[i] = 0;
}

for(int k = 0; k < NUMBER_OF_CUSTOMERS; k++){
if(finish[k] == 0 && need[customer_num][k] <= work[k]){
work[k] += allocation[customer_num][k];
finish[k] = 1;
}
else{
done = -1;
break;
}
}

for(int x = 0; x < NUMBER_OF_CUSTOMERS; x++){
if(finish[x] == 0){
done = 1;
}
else{
done = -1;
}
}
printf("\n jj %d", done);

return done;

}


int* request_resources(int customer_num, int request[]){
pthread_mutex_lock(&mutex);
int pass = 2;
for(int i = 0; i < NUMBER_OF_RESOURCES; i++){
if(request[i] <= need[customer_num][i] && request[i] <= available[i]){
printf("Sata");
int state = safe_state(customer_num);
if(state == 1){
available[i] -+ request[i];
allocation[customer_num][i] += request[i];
need[customer_num][i] -+ request[i];
pass = 1;

}
else{
printf("This results in unsafe state\n");
pass = -1;
break;
}
}
else{
printf("Not enough resources\n");
pass = -1;
break;
}
}
printf("I'm a thread\n");
pthread_mutex_unlock(&mutex);
return pass;

}

int release_resources(int customer_num, int release[]){

}




int main(int argc, char *argv[]){



pthread_t threads [NUMBER_OF_CUSTOMERS];
int result;
unsigned index = 0;

for(index = 0; index < NUMBER_OF_RESOURCES; index++){
available[index] = strtol(argv[index+1], NULL,10);
}

int req[] = {1,2,3};
for(index = 0; index < NUMBER_OF_CUSTOMERS; ++index){
printf("\nCreating thead %d\n", index);
result = pthread_create(&threads[index],NULL,request_resources,req);

}





printf("\nDone");

}

最佳答案

pthread_create() 调用的线程函数的正确声明是

void *start_routine( void *arg );

per the POSIX documentation :

SYNOPSIS

#include <pthread.h>

int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);

您的代码:

    result = pthread_create(&threads[index],NULL,request_resources,req);    

调用request_resources() 来启动每个线程。但是 request_resources() 定义为

int* request_resources(int customer_num, int request[]){
...
}

这不是指定的 void *start_routine( void *arg )

您正在调用未定义的行为。

此外,当 main() 返回时,您的整个进程(包括它生成的每个线程)都将结束。您需要等待每个线程以 pthread_join() 结束.

关于c - 银行家算法线程创建未完全运行传递的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42980437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com