Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 创建线程时未完全运行传递的方法_C_Multithreading_Mutex_Bankers Algorithm - Fatal编程技术网

C 创建线程时未完全运行传递的方法

C 创建线程时未完全运行传递的方法,c,multithreading,mutex,bankers-algorithm,C,Multithreading,Mutex,Bankers Algorithm,当我在main中的线程创建中调用request_resources方法时,该方法没有运行。它应该创建一个线程,请求资源,检查安全状态,然后退出。我不知道为什么它会在两个线程之后暂停,并且不会从方法中的测试语句中给出任何输出 #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h> #include<semaphore.h>

当我在main中的线程创建中调用request_resources方法时,该方法没有运行。它应该创建一个线程,请求资源,检查安全状态,然后退出。我不知道为什么它会在两个线程之后暂停,并且不会从方法中的测试语句中给出任何输出

        #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");

}
#包括
#包括
#包括
#包括
#包括
/*这些值可以是>=0的任何值*/
#定义客户数量5
#定义资源的数量3
/*每个资源的可用数量*/
int可用[资源的数量];
/*每个客户的最大需求*/
int最大[客户数量][资源数量];
/*当前分配给每个客户的金额*/
int分配[客户数量][资源数量];
/*每个客户的剩余需求*/
国际需求[客户数量][资源数量];
pthread_mutex_t mutex=
PTHREAD_MUTEX_初始化器;
int安全状态(int客户数量){
int work[客户数量];
int完成;
对于(int w=0;w<客户数量;w++){
工作[w]=可用[w];
printf(“%d”,作品[w]);
}
int finish[客户数量];
对于(int i=0;i<客户数量;i++){
完成[i]=0;
}
对于(int k=0;k<客户数量;k++){

如果(finish[k]==0&&need[customer\u num][k]由
pthread\u create()
调用的线程函数的正确声明为

void *start_routine( void *arg );
:

简介

#include <pthread.h>

int pthread_create(pthread_t *restrict thread,
       const pthread_attr_t *restrict attr,
       void *(*start_routine)(void*), void *restrict arg);
调用
request\u resources()
来启动每个线程。但是
request\u resources()
定义为

int* request_resources(int customer_num, int request[]){
   ...
}
这不是指定的
void*start_例程(void*arg)

您正在调用未定义的行为


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

对于
pthread\u create()
调用的线程函数,正确的声明如下:

void *start_routine( void *arg );
:

简介

#include <pthread.h>

int pthread_create(pthread_t *restrict thread,
       const pthread_attr_t *restrict attr,
       void *(*start_routine)(void*), void *restrict arg);
调用
request\u resources()
来启动每个线程。但是
request\u resources()
定义为

int* request_resources(int customer_num, int request[]){
   ...
}
这不是指定的
void*start_例程(void*arg)

您正在调用未定义的行为

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

C不支持方法。您的代码没有方法。只有普通函数。C不支持方法。您的代码没有方法。只有普通函数。