#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"iostream.h"
typedef struct node
{
char name[10];
int prio;
int round;
int cputime;
int needtime;
int count;
char state;
struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run;
int N;
void firstin()
{run=ready;
run->state='R';
ready=ready->next;
}
void prt1(char a)
{if(toupper(a)=='p')
cout<<" "<<endl;
cout<<"进程名 占用CPU时间 到完成还要的时间 优先级 状态"<<endl;
}
void prt2(char a,PCB *q)
{if(toupper(a)=='P')
cout<<q->name<<" "<<q->cputime<<" "<<q->needtime<<" "<<q->prio<<" "<<q->state<<endl;
}
void prt(char algo)
{
PCB *p;
prt1(algo);
if(run!=NULL)
prt2(algo,run);
p=ready;
while(p!=NULL)
{prt2
(algo,p);
p=p->next;
}
p=finish;
while(p!=NULL)
{prt2(algo,p);
p=p->next;
}
getchar();
}
void insert(PCB *q)
{PCB *p1,*s,*r;
int b;
s=q;
p1=ready;
r=p1;
b=1;
while((p1!=NULL)&&b)
if(p1->prio>=s->prio)
{r=p1;
p1=p1->next;
}
else
b=0;
if(r!=p1)
{r->next=s;
s->next=p1;
}
else
{s->next=p1;
ready=s;
}
}
void create(char alg)
{
PCB *p;
int i,time;
char na[10];
ready=NULL;
finish=NULL;
run=NULL;
cout<<"输入进程名及其需要运行的时间:"<<endl;
for(i=1;i<=N;i++)
{p=new PCB;
cin>>na;
cin>>time;
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->state='w';
p->prio=100-time;
if(ready!=NULL)
insert(p);
else
{p->next=ready;
ready=p;
}
cout<<"输入进程名及其需要运行的时间:"<<endl;
}
prt(alg);
run=ready;
ready=ready->next;
run->state='R';
}
void priority(char alg)
{
while(run!=NULL)
{run->cputime=run->cputime+10;
run->needtime=run->needtime-10;
run->prio=run->prio-10;
if(run->needtime==0)
{run->next=finish;
finish=run;
run->state='F';
run=NULL;
if(ready!=NULL)
firstin();
}
else
if((ready!=NULL)&&(run->prio<ready->prio))
{
run->state='W';
insert(run);
firstin();
}
prt(alg);
}
}
void main()
{
char algo='p';
cout<<"输入进程的个数:";
cin>>N;
create(algo);
priority(algo);
}
#include <stdio.h>
#include <malloc.h>
typedef struct process
{
char name[4];
int time;
int priority;
char state;
struct process *next;
} *P;
void init(P &head)
{
int i;
P q;
for(i=0;i<5;i++)
{
printf("输入进程名 时间 优先级:");
q=(P)malloc(sizeof(struct process));
scanf("%s %d %d",q->name,&q->time,&q->priority);
q->state='R';
q->next=head->next;
head->next=q;
}
}
void queue( P &head)
{
P q,r,s;
r=head;
q=head->next->next;
head->next->next=NULL;
while(q){
while(r->next){
if(q->priority>r->next->priority)
break;
else r=r->next;
}
s=q->next;
q->next=r->next;
r->next=q;
q=s;
r=head;
}
}
void outqueue(P &head)
{
head->next=head->next->next;
}
void run(P &head)
{
printf("进程运行\n进程名 剩余时间 优先级 状态\n");
while(head->next){
if(head->next->time==1) head->next->state='E';
head->next->time--;
printf("%s %5d %5d %5c\n",head->next->name,head->next->time,head->next->priority,head->next->state);
head->next->priority--;
if(head->next->time==0)
{
if(!head->next->next) head->next=NULL;
else outqueue(head);
}
if(head->next)
queue(head);
}
}
void main()
{
P head,q;
head=(P)malloc(sizeof(struct process));
q=head;
head->next=NULL;
init(head);
queue(head);
q=q->next;
while(q)
{
printf("%s %d %d\n",q->name,q->time,q->priority);
q=q->next;
}
run(head);
}
#include<iostream>
using namespace std;
int Available[100];
int Max[100][100];
int Allocation[100][100];
int Need[100][100];
int Finish[100];
int p[100];
int ProcessNum,SourceNum;
void Safe()
{
int i,j,k;
int count=0;
int n=0;
int Work[100];
for(i=0;i<SourceNum;i++)
Work[i]=Available[i]; //工作向量的初始值为Available
for(i=0;i<ProcessNum;i++)
Finish[i]=0;
for(i=0;i<ProcessNum;i++)
{
if(Finish[i]==1)
continue;
else
{
for(j=0;j<SourceNum;j++)
{
if(Need[i][j]>Work[j])
break;
}
if(j==SourceNum)
{
Finish[i]=1;
for(k=0;k<SourceNum;k++)
{
Work[k]=Work[k]+Allocation[i][k];
}
count++;
p[n++]=i;
i=-1;
}
}
}
if(count==ProcessNum)
{
cout<<"系统是安全的"<<endl;
cout<<"安全序列是:"<<endl;
for(i=0;i<ProcessNum;i++)
{
cout<<p[i]<<" ";
}
cout<<endl;
}
if(count!=ProcessNum)
{
cout<<"系统是不安全的"<<endl;
}
}
void main()
{
int i,j;
int RequestProcess;
int a[100];
cout<<"请输入进程数:"<<endl;
cin>>ProcessNum;
cout<<"请输入资源数:"<<endl;
cin>>SourceNum;
cout<<"请输入每个进程最大需求资源数:"<<endl;
for(i=0;i<ProcessNum;i++)
for(j=0;j<SourceNum;j++)
cin>>Max[i][j];
cout<<"输入每个进程已经分配的资源数:"<<endl;
for(i=0;i<ProcessNum;i++)
for(j=0;j<SourceNum;j++)
cin>>Allocation[i][j];
for(i=0;i<ProcessNum;i++)
for(j=0;j<SourceNum;j++)
Need[i][j]=Max[i][j]-Allocation[i][j];
cout<<"请输入可利用资源数:"<<endl;
for(i=0;i<SourceNum;i++)
cin>>Available[i];
Safe();
cout<<"请输入你要分配的进程:"<<endl;
cin>>RequestProcess;
cout<<"请输入请求分配进程的资源数"<<endl;
for(i=0;i<SourceNum;i++)
cin>>a[i];
for(i=0;i<SourceNum;i++)
{
if(a[i]>Need[RequestProcess][i])
{
cout<<"输入的请求资源错误(请求的资源数已超过它所宣布的最大值)"<<endl;
break;
}
if(a[i]>Available[i])
{
cout<<"输入的请求资源错误(尚无足够资源)"<<endl;
break;
}
else
{
Available[i]=Available[i]-a[i];
Allocation[RequestProcess][i]=Allocation[RequestProcess][i]+a[i];
Need[RequestProcess][i]=Need[RequestProcess][i]-a[i];
Safe();
break;
}
}
}
#include "malloc.h"
#include "stdio.h"
#include "stdlib.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available /*可用资源数*/
{
int value;
struct available *next;
};
struct need /*需求资源数*/
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2;
printf("\n请输入系统资源的种类数:");
scanf("%d",&colum);
printf("请输入现时内存中的进程数:");
scanf("%d",&row);
printf("请输入已分配资源矩阵:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("请输入已分配给进程 p%d 的 %c 种系统资源:",i,'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("请输入最大需求矩阵:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("请输入进程 p%d 种类 %c 系统资源最大需求:",i,'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
OS.rar_visual c
版权申诉
37 浏览量
2022-09-22
22:17:52
上传
评论
收藏 4KB RAR 举报
林当时
- 粉丝: 100
- 资源: 1万+
最新资源
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
- node-v18.20.2-linux-arm64
- 222222222222
- 16张相机标定图片,可复现本文畸变矫正
- dbeaver-ce-23.3.1-x86_64-setup.zip
- 基于X86 AVX2指令的快速卷积实现
- VMware-ESXi-7.0U3n-21930508-depot.zip文件
- MySQL 在 Windows 系统下的安装教程.zip
- Access文件数据库访问的客户端
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈