我是靠谱客的博主 从容星星,最近开发中收集的这篇文章主要介绍操作系统实验3(银行家算法),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本次实验由本菜鸟一步一步做出来。最大的感想就是,果然语言本身才是浪费时间最多的地方,比如说那个pthread_create()的最后的参数,不知如何传,上网查看,都是一大坨一大坨的,贴一大段复杂的不得了的代码,随便说两句,谁看得懂?只好瞎试。最后明白了
int i = 99;
pthread_create(&tid[i], NULL, runner, (void*)i);

而传到了runner函数中,在void *runner( void * param)中,如果需要提取,则int x = (int)param;

而关于银行家算法本身,遵循算法本身的规则,然后注意一下细节,就比较容易做出来了。具体细节不再赘述,先写报告。

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#define NUMBER_OF_CUSTOMERS 5
#define NUMBER_OF_RESOURCES 3

int request_resource(int customer_num, int request[]);
int release_resource(int customer_num, int request[]);
void *runner(void *param);

sem_t s, st;

int real_finish[NUMBER_OF_CUSTOMERS]={0};
int real_count = 0;

int available[NUMBER_OF_RESOURCES];
int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] =
{7, 5, 3, 3, 2, 2, 9, 0, 2, 2, 2, 2, 4, 3, 3};
int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] =
{0, 1, 0, 2, 0, 0, 3, 0, 2, 2, 1, 1, 0, 0, 2};
int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] =
{7, 4, 3, 1, 2, 2, 6, 0, 0, 0, 1, 1, 4, 3, 1};

int main(int argc, char *argv[])
{
pthread_t tid[5];
    sem_init(&s, 0, 1);
    sem_init(&st, 0, 1);
    int i;
    for(i = 0; i <= 2; ++i)
    available[i] = atoi(argv[i+1]);
    //printf("%s, %s, %s, %sn", argv[0], argv[1], argv[2], argv[3]);
    //the first command decides the value of available
   
//start the main code

//in circle, request input values
for(; ; )
{
    //the end condition
    if(NUMBER_OF_CUSTOMERS == real_count) break;
    //create five threads
    for(i = 0; i <= 4; ++i)
    {
    //judge whether request can be accepted
   
    pthread_create(&tid[i], NULL, runner, (void*)i);
   
   
    //test
    //printf("test: here is %dn", i);
    }

    //wait for the threads end
    int j;
    for(j = 0; j <= 4; ++j)
    pthread_join(tid[j], NULL);
}
    printf("task completen");
    return 0;
}

void *runner(void *param)
{
    int x = (int)param;
    sem_wait(&st);
    int request[3];
    printf("to pthread[%d] please input the request you wantn", x);
    scanf("%d %d %d", &request[0], &request[1], &request[2]);
    sem_post(&st);
    //request the resource and may release it
   
    sem_wait(&s);//--------------------------critical area s
    int judge = request_resource(x, request);
    //when the request is successful
    if(0 == judge)
    {
        //get into a new status
        release_resource(x, request);
   
    }
    sem_post(&s);//--------------------------critical area e
   
    //when failing, it needs to be hung
    if(-1 == judge)
    {
        //with the releasing, the bigger the available is,
        //the eassilier meet the request
        //no need to put the request fucton into the mutex area
        while(-1 == request_resource(x, request));
        sem_wait(&s);//------------------critical area s
        release_resource(x, request);
        sem_post(&s);//------------------critical area e
    }

    //test the pthread_join
/*    if(0 == x)
    {   
    while(99 != y);
    sem_wait(&s);
    printf("%dn", x);
    sem_post(&s);
    }
   
    else
    {
    sem_wait(&s);
    printf("%dn", x);
    if(4 == x)
    y = 99;
    sem_post(&s);
    }
*/

pthread_exit(0);

}

int request_resource(int customer_num, int request[])
{
//return (1 == customer_num?0:-1);
int judge = 0;
int i, j;
int finish_flag = 0;//is there a p finished?
int finish_num;//what's the number

int t_allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
int t_available[NUMBER_OF_RESOURCES];
int t_need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
    //judge the first step and save the status
    //printf("here is the request of the customer%dn", customer_num);
    if(request[0] <= available[0] &&
    request[1] <= available[1] &&
    request[2] <= available[2] &&
    request[0] <= need[customer_num][0] &&
    request[1] <= need[customer_num][1] &&
    request[2] <= need[customer_num][2])
    {
    //initialize the new status 1
    //initialize the t_allocation, t_need
        for(i = 0; i <= NUMBER_OF_CUSTOMERS-1; ++i)
        for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
        {
        t_allocation[i][j] = allocation[i][j];
        t_need[i][j] = need[i][j];
            if(customer_num == i)
            {
                t_allocation[i][j] = allocation[i][j] +             request[j];
                t_need[i][j] = need[i][j]-
            request[j];
            }
        }
    //judge to release to initialize the new status 2
    //in the mean time, initialize the t_available
    int judge_release = 1;
    for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
    if(request[j] != need[customer_num][j])
    judge_release = 0;

        if(1 == judge_release)
        {
        printf("get into the new status 2n");
        finish_flag = 1;
        finish_num = customer_num;
        for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
        t_available[j] = available[j] + allocation[customer_num][j];
        }
        else
        for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
        t_available[j] = available[j] - request[j];
       

    }
    else    return -1;

    //judge whether it is the safet state
    //critical point: how to jump out of the dangerous status?
    int count = 0;//count the number of the finishing
    int count_d = -1;//count the time of undone to judge whether to jump
    int finish[NUMBER_OF_CUSTOMERS]= {0};
    //init the real finish
    if(1 == finish_flag)
    {real_finish[finish_num] = 1; ++real_count;}
    //initialize the finish
    for(i = 0; i <= NUMBER_OF_CUSTOMERS-1; ++i)
    {finish[i] = real_finish[i]; count = real_count;}

    //start the check of the safety
    while(NUMBER_OF_CUSTOMERS != count)
    {
    //in one round, if there's no change, we consider it is unsafe
    if(0 == count_d) return -1;
    count_d = 0;
        for(i = 0; i <= NUMBER_OF_CUSTOMERS-1; ++i)
        {
   
        if(1 == finish[i]) continue;

        int j_release = 1;
            for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
            if(t_need[i][j] > t_available[j])
            {j_release = 0; break;}
       
        if(1 == j_release)
        {
        finish[i] = 1;
        count_d = 1;
        ++count;
            for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
            t_available[j] += t_allocation[i][j];
        }

        }
    }
    //printf("testn");
    return 0;
}

int release_resource(int customer_num, int request[])
{
int j;
    printf("accept the request of the customer%dn", customer_num);
   
    for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
    {
        available[j] = available[j] - request[j];
        allocation[customer_num][j] += request[j];
        need[customer_num][j] -= request[j];
        printf("dec available[%d] is %dn", j, available[j]);
    }

int j_release = 1;
    for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
    if(need[customer_num][j] > 0)
    {
    j_release = 0;
    break;
    }

    if(1 == j_release)
    {   
        for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j)
        {
        allocation[customer_num][j] -= request[j];
        available[j] += request[j] + allocation[customer_num][j];
        printf("inc available[%d] is %dn",
         j, available[j]);
        }
   
    }   
return 0;
}

最后

以上就是从容星星为你收集整理的操作系统实验3(银行家算法)的全部内容,希望文章能够帮你解决操作系统实验3(银行家算法)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部