📄 caozuo.txt
字号:
#include "string.h"
#include<iostream.h>
#define M 5 //总进程数
#define N 3 //总资源数
#define FALSE 0
#define TRUE 1
//M个进程对N类资源最大资源需求量
int MAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
//系统可用资源数
int AVAILABLE[N]={3,3,2};
//M个进程已经得到N类资源的资源量
int ALLOCATION[M][N]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
//M个进程还需要N类资源的资源量
int NEED[M][N]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int Request[N]={0,0,0};
int FINISH[M]={FALSE,FALSE,FALSE,FALSE,FALSE};
int WORK[N];
bool flag=false;
bool flag1=true;
void main()
{
int i,j;
int request,a,b,c;
cout<<"---------Max-----------Allocation----------Need"<<endl;
cout<<"--------A B C-------------A B C------------A B C"<<endl;
for( i=0;i<M;i++)
{
cout<<i;
cout<<"------";
for ( j=0;j<N;j++)
{
cout<<" "<<MAX[i][j];
}
cout<<"------------";
for ( j=0;j<N;j++)
{
cout<<" "<<ALLOCATION[i][j];
}
cout<<"-----------";
for ( j=0;j<N;j++)
{
cout<<" "<<NEED[i][j];
}
cout<<endl;
}
cout<<"Available"<<endl;
cout<<"a b c"<<endl;
for ( j=0;j<N;j++)
{
cout<<AVAILABLE[j]<<" ";
}
cout<<endl;
cout<<"请输入提出需求的进程(0~4):";
cin>>request;
cout<<"请分别输入对abc类资源的需求需求输:";
cin>>a>>b>>c;
Request[0]=a;
Request[1]=b;
Request[2]=c;
//银行家算法
for ( j=0;j<N;j++)
{
if (Request[j]>NEED[request][j] || Request[j]>AVAILABLE[j])
{
cout<<"进程请求不安全,拒绝分配"<<endl;
flag1=false; break;
}
}
while(flag1) //当进程请求合理时
{
for( j=0;j<N;j++) //分配给进程资源并修改系统资源
{
AVAILABLE[j]=AVAILABLE[j]-Request[j];
NEED[request][j]=NEED[request][j]-Request[j];
ALLOCATION[request][j]=ALLOCATION[request][j]+Request[j];
}
FINISH[request]=FALSE;
for ( j=0;j<N;j++) //安全性算法
{
WORK[j]=AVAILABLE[j];
cout<<"进程号------"<<"WORK---------"<<"NEED--------"<<"ALLOCATION--------"<<"FINISH"<<endl;
for(int ii=0;ii<M;ii++)
{
for ( i=0;i<M;i++)
{
flag=true;
if(FINISH[i]==FALSE)
{
for( j=0;j<N;j++)
{
if(NEED[i][j]>WORK[j])
{
flag=false; break;
}
}
if(flag)
{
cout<<"----"<<i<<"------";
for( j=0;j<N;j++)
{
WORK[j]=WORK[j]+ALLOCATION[i][j];
cout<<WORK[j]<<" ";
}
cout<<"-------";
for ( j=0;j<N;j++)
{
cout<<" "<<NEED[i][j];
}
cout<<"---------";
for ( j=0;j<N;j++)
{
cout<<" "<<ALLOCATION[i][j];
}
FINISH[i]=TRUE;
cout<<"---------"<<FINISH[i];
cout<<endl;
}
}
continue;
}
}
for ( i=0;i<M;i++)
{
flag=true;
if(FINISH[i]!=TRUE)
{
flag=false;
break;
}
}
if(flag)
{
cout<<"算法安全"<<endl;
flag1=false;
}
else
{
cout<<"不安全"<<endl;
flag1=false;
}
}
}
内存分配
#include <iostream.h>
#include "stdlib.h"
typedef struct node
{
int name;
int address; /*地址*/
int length; /*作业长度,所需主存大小*/
int state; /*作业在输入井中的等待时间*/
struct node *next;/*指向下一个作业控制块的指针*/
}node; /*作业控制块类型定义*/
node *head; /*作业队列头指针定义*/
int i;
node *create(int n[],int a[],int l[],int s[],int m);
void show(node *);
node *search(node *,node *);
void insert(node *,node *,node *);
void back(node *,int );
void main()
{
node *head,*yes;
int n[]={-1,1,3,-1,2,-1};
int a[]={0,5,10,14,26,32};
int l[]={5,5,4,12,6,96};
int s[]={2,1,1,0,1,0};
head=create(n,a,l,s,6);
show(head);
cout<<"装入作业4:"<<endl;
node *work4=(node *) new (node);
work4->length=6;
work4->name=4;
yes=search(head,work4);
insert(head,yes,work4);
show(head);
cout<<"作业3撤离:"<<endl;
back(head,3);
show(head);
cout<<"作业2撤离:"<<endl;
back(head,2);
show(head);
}
node *create(int n[],int a[],int l[],int s[],int m)
{
node *head,*r,*h;
for(i=0;i<m;i++)
{
h=(node *) new (node);
h->name=n[i];
h->address=a[i];
h->length=l[i];
h->state=s[i];
h->next=NULL;
if(i==0)
{
head=h;r=h;
}
else
{
r->next=h;r=h;
}
}
return head;
}
void show(node *h)
{
while(h!=NULL)
{
cout<<h->address<<" "<<h->length<<" ";
if(h->state==2)
{
cout<<"操作系统";
}
else if(h->state==1)
{
cout<<"作业"<<h->name;
}
else if(h->state==0)
{
cout<<"空闲区";
}
h=h->next;
cout<<endl;
}
cout<<endl;
}
node *search(node *h,node *r)
{
node *s;
s=h;
while(h!=NULL)
{
if(h->state==0 && h->length>=r->length)
{
return h;
}else{h=h->next;}
}
return s;
}
void insert(node *h,node *s,node *r)
{
node *p=h;
if(s==h)
{
head=r;
head->next=s;
head->name=r->name;
head->address=s->address;
head->length=r->length;
head->state=1;
s->address=s->address+r->length;
s->length=s->length-r->length;
}
else
{
while(p->next!=NULL)
{
if(p->next==s)
{
p->next=r;
r->next=s;
r->address=s->address;
r->state=1;
s->address=s->address+r->length;
s->length=s->length-r->length;
break;
}
else
{
p=p->next;
}
}
}
}
void back(node *h,int w)
{
node *s;
node *p=h;
if(h->name==w)
{
}
else
{
while(p->next!=NULL)
{
if(p->next->name==w)
{
s=p->next;
s->name=-1;
s->state=0;
if(p->state==0)
{
p->length=p->length+s->length;
p->next=s->next;
if(p->next->state==0)
{
p->length=p->length+p->next->length;
p->next=p->next->next;
}
}
if(s->next->state==0 )
{
s->length=s->length+s->next->length;
s->next=s->next->next;
}
break;
}
else
{
p=p->next;
}
}
}
}
作业调度
#include "stdlib.h"
#include<iostream.h>
typedef struct jcb
{
char name; /*作业名*/
int length; /*作业长度,所需主存大小*/
int printer; /*作业执行所需打印机的数量*/
int tape; /*作业执行所需磁带机的数量*/
int runtime; /*作业估计的执行时间*/
int waittime; /*作业在输入井中的等待时间*/
int rp;
struct jcb *next;/*指向下一个作业控制块的指针*/
}JCB; /*作业控制块类型定义*/
JCB *head; /*作业队列头指针定义*/
int tape=4,printer=2;
long memory=64;
int m=3;
int i;
jcb *create(char na[],int len[],int print[],int tap[],int runt[],int wait[],int rp[],int);
void disp(jcb *);
jcb *diaodu(jcb *);
jcb *dele(jcb *,jcb *);
void main()
{
jcb *head;
jcb *node;
char name[]={'1','2','3'};
int length[]={1,2,3};
int printer[]={0,0,0};
int tape[]={0,0,0};
int waittime[]={1,1,3};
int runtime[]={1,2,1};
int rp[]={0,0,0};
for(int j=0;j<m;j++)
{
rp[j]=waittime[j]/runtime[j];
}
head=create(name,length,printer,tape,runtime,waittime,rp,m);
disp(head);
for( i=0;i<m;i++)
{
node=diaodu(head);
cout<<"进程"<<node->name<<"得到调度"<<endl;
cout<<endl;
head=dele(head,node);
disp(head);
}
}
jcb *create(char na[],int len[],int print[],int tap[],int runt[],int wait[],int rp[],int n)
{
jcb *head,*r,*s;
for( i=0;i<n;i++)
{
s=(jcb *)new(jcb);//建立一个新节点
s->name=na[i];
s->length=len[i];
s->printer=print[i];
s->tape=tap[i];
s->runtime=runt[i];
s->waittime=wait[i];
s->rp=rp[i];
s->next= NULL;
if(i==0)
{
head=s;r=s;//head为头指针,r总是指向最后节点
}
else
{
r->next=s;r=s;//链接结点s
}
}
return head;
}
void disp(jcb *h)
{
cout<<"链表结点值为:"<<endl;
cout<<"进程名"<<" "<<"进程长度"<<" "<<"打印机数量"<<" "<<"磁带机数量"<<" "<<"运行时间"<<" "<<"等待时间"<<" "<<"响应比"<<endl;;
while(h!=NULL)
{
cout<<h->name<<" "<<h->length<<" "<<h->printer<<" "<<h->tape<<" "<<h->runtime<<" "<<h->waittime<<" "<<h->rp;
cout<<endl;
h=h->next;
}
}
jcb *diaodu(jcb *h)
{
int maxrp=0;
jcb *head,*r,*s;
head=h;
r=h;
s=h;
while(h!=NULL)
{
if(h->printer<=printer && h->tape<=tape && h->length<=memory && maxrp<h->rp)
{
maxrp=h->rp;
r=h;
h=h->next;
}
else
{
h=h->next;
}
}
while(s!=NULL)
{
s->waittime=s->waittime+r->runtime;
s->rp=s->waittime/s->runtime;
s=s->next;
}
return r;
}
jcb *dele(jcb *head,jcb *r)
{
jcb *s;
if(r==head)
return head->next;
else
{
s=head;
while(s->next!=r)
s=s->next;
if(r->next==NULL)
s->next=NULL;
else
s->next=r->next;
}
return head;
}
进程调度
#include "stdlib.h"
#include<iostream.h>
typedef struct pcb
{
char name; /*作业名*/
int pro; /*优先数*/
int runtime; /*作业估计的执行时间*/
int state; /*作业状态*/
struct pcb *next;/*指向下一个作业控制块的指针*/
}PCB; /*作业控制块类型定义*/
PCB *head; /*作业队列头指针定义*/
int i;
pcb *create(char na[],int pro[],int runt[],int state[],int);
void disp(pcb *);
pcb *diaodu(pcb *);
pcb *dele(pcb *,pcb *);
void main()
{
pcb *head;
pcb *node;
char name[]={'p2','p4','p3','p5','p1'};
int pro[]={5,4,3,2,1};
int runtime[]={3,2,1,4,2};
int state[]={1,1,1,1,1};
head=create(name,pro,runtime,state,5);
disp(head);
while(head!=NULL )
{
head=diaodu(head);
disp(head);
}
}
pcb *create(char na[],int pro[],int runt[],int state[],int n)
{
pcb *head,*r,*s;
for( i=0;i<n;i++)
{
s=(pcb *)new(pcb);//建立一个新节点
s->name=na[i];
s->pro=pro[i];
s->runtime=runt[i];
s->state=state[i];
s->next=NULL;
if(i==0)
{
head=s;r=s;//head为头指针,r总是指向最后节点
}
else
{
r->next=s;r=s;//链接结点s
}
}
return head;
}
void disp(pcb *h)
{
cout<<"链表结点值为:"<<endl;
cout<<"进程名"<<" "<<"优先数"<<" "<<"运行时间"<<" "<<"状态"<<endl;
while(h!=NULL)
{
cout<<" "<<h->name<<" "<<h->pro<<" "<<h->runtime<<" "<<h->state;
cout<<endl;
h=h->next;
}
}
pcb *diaodu(pcb *h)
{
pcb *head,*r;
head=h;
r=h;
if(h!=NULL)
{
cout<<"进程"<<h->name<<"得到调度"<<endl;
cout<<endl;
h->pro=h->pro-1;
h->runtime=h->runtime-1;
if(h->runtime==0)
{
h->state=0;
head=dele(head,h);
}
else if(h->next!=NULL && h->pro<h->next->pro)
{
head=h->next;
h->next=h->next->next;
head->next=h;
}
}
return head;
}
pcb *dele(pcb *head,pcb *r)
{
pcb *s;
if(r==head)
{
return head->next;
}
else
{
s=head;
while(s->next!=r)
s=s->next;
if(r->next==NULL)
s->next=NULL;
else
s->next=r->next;
}
return head;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -