操作系统资源分配管理

一、实验名称:资源分配管理
二、实验目的与要求
目的:通过本次实验,使学生加深了对死锁概念的理解和掌握,深刻领会银行家算法的实质及实现过程。
要求:编写一个系统动态分配资源的简单模拟程序,采用银行家算法防止死锁。
三、实验内容
设计五个进程{P0,P1,P2,P3,P4}共享三类资源{A,B,C}的系统,{A,B,C}的资源数量分别为10,5,7。进程可动态地申请资源和释放资源,系统按各进程的申请动态地分配资源。在T0时刻的资源分配情况如下图所示:

(1)若进程P1请求资源,发出请求向量Request1(1,0,2),编写程序用银行家算法判断系统能否将资源分配给它;
(2)若进程P2提出请求Request2(0,1,0),用银行家算法程序验证系统能否将资源分配给它。
要求程序具有显示和打印各进程的某一时刻的资源分配表和安全序列。
#include

using namespace std;
int Max[5][3]={7,5,3,3,2,2,9,0,2,2,2,2,4,3,3};
int Need[5][3]={7,4,3,1,2,2,6,0,0,0,1,1,4,3,1};
int Allocation[5][3]={0,1,0,2,0,0,3,0,2,2,1,1,0,0,2};
int Available[3]={3,3,2};
bool finish[5]={false,false,false,false,false};
int work[3];
bool Safety() //安全性算法
{

for(int i=0;i<3;i++) // 执行时,工作向量work=Available
{
work[i]=Available[i];
}

 for(int i=0;i<5;i++)
 {
    if(finish[i]==false&&Need[i][0]<=work[0]&&Need[i][1]<=work[1]&&Need[i][2]<=work[2])  //满足条件释放进程
    {
        work[0]+=Allocation[i][0];  //释放A,B,C类资源
        work[1]+=Allocation[i][1];
        work[2]+=Allocation[i][2];
        cout<<"可利用资源数"<<"A:"<<work[0]<<" "<<"B:"<<work[1]<<" "<<"C:"<<work[2]<<endl;
        finish[i]=true;
        cout<<"P"<<i<<"释放"<<endl;
        i=-1;                        //有一个进程释放了重头考试找满足finish[i]=false,Need<=work的进程
    }

 }
 bool k=true;
 for(int i=0;i<5;i++)
 {
     if(finish[i]==false)  //判断是否有进程没有释放
     {
         k=false;
         break;
     }
 }
 if(k==false)           //有进程没有释放返回false
 {
     return false;
 }
 else                  //进程释放完返回true
 {
     return true;
 }

}
void Request(int i,int a,int b,int c) //申请资源函数
{

if(Need[i][0]>=a&&Need[i][1]>=b&&Need[i][2]>=c) //若满足条件试着对资源进行分配,然后判断是否满足安全性算法
{
if(Available[0]>=a&&Available[1]>=b&&Available[2]>=c) //满足条件对资源进行配
{
Available[0]=Available[0]-a; //对资源进行分配
Available[1]=Available[1]-b;
Available[2]=Available[2]-c;
Allocation[i][0]=Allocation[i][0]+a;
Allocation[i][1]=Allocation[i][1]+b;
Allocation[i][2]=Allocation[i][2]+c;
Need[i][0]=Need[i][0]-a;
Need[i][1]=Need[i][1]-b;
Need[i][2]=Need[i][2]-c;
if((Allocation[i][0]==Max[i][0])&&(Allocation[i][1]==Max[i][1])&&(Allocation[i][2]==Max[i][2])) //判断进程是否满足释放条件
{
Available[0]=Available[0]+Allocation[i][0]; //释放A,B,C类资源
Available[1]=Available[1]+Allocation[i][1];
Available[2]=Available[2]+Allocation[i][2];
finish[i]=true;
cout<<“P”<<i<<“释放”<<endl;
}
if(Safety()==false) //若不满足安全性算法,回收所分配的资源
{
Available[0]=Available[0]+a; //回收分配的资源
Available[1]=Available[1]+b;
Available[2]=Available[2]+c;
Allocation[i][0]=Allocation[i][0]-a;
Allocation[i][1]=Allocation[i][1]-b;
Allocation[i][2]=Allocation[i][2]-c;
cout<<“申请之后,系统处于不安全状态”<<endl;
}
else
{
cout<<“申请之后,系统处于安全状态”<<endl;
}
}
else //可利用资源小于申请资源,进程等待
{
cout<<“无足够资源,P”<<i<<“等待”<<endl;
}
}
else //申请的资源大于京城所需要的资源
{
cout<<“申请资源超过最大值”<<endl;
}

}

int main()
{

//T0时刻
//输入Max

int a,b,c,d;
cout<<"请输入要申请资源的进程:";
cin>>d;
cout<<"输入申请A类资源的个数:";
cin>>a;
cout<<"输入申请B类资源的个数:";
cin>>b;
cout<<"输入申请C类资源的个数:";
cin>>c;
Request(d,a,b,c);

return 0;

}
运行结果
(1)若进程P1请求资源,发出请求向量Request1
(1,0,2),编写程序用银行家算法判断系统能否将资源分配给它;
在这里插入图片描述

(2)若进程P2提出请求Request2(0,1,0),用银行家算法程序验证系统能否将资源分配给它。
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
操作系统中,通常使用多种数据结构来管理和分配资源,其中最常用的包括链表、队列、栈、堆等。下面是一个简单的 C 语言示例,演示如何按序分配操作系统资源: ```c #include <stdio.h> #include <stdlib.h> // 定义一个链表节点结构体 struct Node { int data; // 数据 struct Node* next; // 指向下一个节点的指针 }; // 定义一个链表结构体 struct List { struct Node* head; // 指向链表头节点的指针 }; // 初始化链表 void initList(List* list) { list->head = NULL; } // 在链表尾部插入节点 void insertNode(List* list, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); // 创建一个新的节点 newNode->data = data; newNode->next = NULL; if (list->head == NULL) { // 如果链表为空,则将新节点作为头节点 list->head = newNode; } else { Node* cur = list->head; while (cur->next != NULL) { // 找到链表尾部 cur = cur->next; } cur->next = newNode; // 将新节点插入到链表尾部 } } // 按序分配资源 int allocate(List* list) { if (list->head == NULL) { // 如果链表为空,返回-1表示分配失败 return -1; } else { Node* cur = list->head; int data = cur->data; // 取出链表头节点的数据 list->head = cur->next; // 将头节点指向下一个节点 free(cur); // 释放原头节点的内存空间 return data; // 返回分配的资源 } } int main() { List list; initList(&list); insertNode(&list, 10); insertNode(&list, 20); insertNode(&list, 30); int res1 = allocate(&list); int res2 = allocate(&list); int res3 = allocate(&list); int res4 = allocate(&list); printf("%d %d %d %d\n", res1, res2, res3, res4); // 输出:10 20 30 -1 return 0; } ``` 在上面的代码中,`struct Node` 表示链表中的一个节点,包含一个 `data` 数据域和一个 `next` 指针域,`struct List` 则表示整个链表,包含一个 `head` 指针,指向链表的头节点。`initList()` 函数用于初始化链表,`insertNode()` 函数用于在链表尾部插入新的节点,`allocate()` 函数用于按序分配资源,如果链表为空,则返回 `-1` 表示分配失败。 在 `main()` 函数中,我们首先创建一个空的链表,然后依次插入三个节点,分别存储数值为 `10`、`20` 和 `30` 的资源。接着,我们依次调用 `allocate()` 函数,尝试按序分配这三个资源,最后再尝试分配一个不存在的资源,输出结果为 `10 20 30 -1`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值