东南大学操作系统实验报告银行家算法_银行家算法实验报告
东南大学操作系统实验报告银行家算法由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“银行家算法实验报告”。
操作系统实验三:银行家算法的实现
一、基本信息:
a)实验题目:银行家算法的实现 b)完成人姓名:韩璐璐 c)学号:71114115 d)报告日期:2016.5.27
二、实验目的通过实验,加深对多实例资源分配系统中死锁避免方法——银行家算法的理解,掌握Windows环境下银行家算法的实现方法,同时巩固利用Windows API进行共享数据互斥访问和多线程编程的方法。
三、实验内容
1.在Windows操作系统上,利用Win32 API编写多线程应用程序实现银行家算法。
2.创建n个线程来申请或释放资源,只有保证系统安全,才会批准资源申请。3.通过Win32 API提供的信号量机制,实现共享数据的并发访问。
四、程序运行时的初值和运行结果(系统截图)
五、源程序并附上注释 #include #include #include #include using namespace std;int r[3] = { 3, 3, 2 };//系统拥有的资源 int r0 = 0, r1 = 0, r2 = 0;//记录申请资源 cla pcb { public: int id;bool state;int max[3];int alc[3];int need[3];
pcb(){ } void init(){
state = false;
cout
cin >> id;
cout
cin >> max[0] >> max[1] >> max[2];
cout
cin >> alc[0] >> alc[1] >> alc[2];} int rd(int n){
return rand()%(n + 1);
} int request(){
// Sleep(1000);
r0 = rd(max[0]alc[1]);
r2 = rd(max[2]alc[0]))&& r1 ==(max[1]alc[2]))
{
r[0] = r[0] + alc[0];
r[1] = r[1] + alc[1];
r[2] = r[2] + alc[2];
return 1;
}
return 2;
} };bool safe(vector
temp, int i){ int u = r[0]r1, l = r[2]1;j++)
temp[j] = temp[j + 1];temp.pop_back();int size = temp.size();//记录下容器内还有多少个进程
// int range[size];//记录下队列
int x = 0;//计数器
while(!temp.empty()){
static int j = 0;
if((temp[j].max[0]temp[j].alc[1])
(temp[j].max[2]1;e++)
temp[e] = temp[e + 1];
temp.pop_back();
if(j >= temp.size())
j = 0;
}
else
{
j++;
if(j >= temp.size())
j = 0;
}
x++;
if(x ==(size*size))
{
cout
cout
return false;
}
} return true;} int main(){ srand(time(0));pcb p[4];vector
vp;for(int i = 0;i
p[i].init();
vp.push_back(p[i]);} int x = 0;//计算器
int c;cout > c;switch(c){ case 1:
while(!vp.empty())
{
int a;
static int i = 0;
if((a = vp[i].request())!= 0)
{
if(a == 1)
endl;
r[1]
{
cout
for(int j = i;j
r[1] = r[1]r2;
cout
cout
}
i++;
if(i >= vp.size())
i = 0;
}
}
else
i++;
if(i >= vp.size())
i = 0;
x++;
if(x >= 200)
{
cout
return 0;
}
}
cout
break;case 2:
while(!vp.empty())
{
int a2;
static int i2 = 0;
if((a2 = vp[i2].request())!= 0)
{
if(a2 == 1)
{
cout
for(int j = i2;j
r[1] = r[1]r2;
cout
cout
i2++;
if(i2 >= vp.size())
i2 = 0;
}
}
else
i2++;
if(i2 >= vp.size())
i2 = 0;
x++;
if(x >= 200)
{
cout
return 0;
}
}
cout
break;default:
cout
break;
} system(“pause”);return 1;}
要求:实验报告以电子版的形式通过Email提交给助教,做到内容翔实、图表清晰,层次分明,标题突出。
一班 助教老师:丁文江 Email:dingwj@seu.edu.cn 二班 助教老师:张润环 Email:seu-zrh@seu.edu.cn 两位助教老师工作地点:九龙湖校区计算机楼333房间