#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
#define N 50
struct pcb /* 定义进程控制块PCB */
{ char name[10]; //进程名
char state; //进程状态
int super; //进程优先级
int ntime; //进程需要运行时间
int rtime; //进程已经运行的时间
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
struct process
{
int come_time;
int temp_time;
int run_time;
}process[100],processing[100];
int RR=1;
int n;
struct groud
{
char name[15];
int max;
};
struct groud job[N];
struct groud copy[N];
void input_groud()
{
char ch;
for(n=0;;)
{
printf("\ninput job %d:\n",n);
printf("name:");
scanf("%s",job[n].name);
fflush(stdin);
printf("Max Time:");
scanf("%d",&job[n].max);
n++;
if(n==N-1)
{
printf("**************ENOUGH**************");
break;
}
else
{ system("cls");
printf("any more job?(y/n):");
fflush(stdin);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{
continue;
}
else break;
}
}
}
void output_groud()
{
int m;
int i;
int j;
m=n;
printf("\n-----------------------------------\n\n");
printf("The running queue is :\n\n");
for(;;)
{
for(i=0;i<m;)
{
if(copy[i].max<=0)
{
for(j=i;j<m;j++)
{
copy[j]=copy[j+1];
}
m--;
}
else
{
printf("%s-->",copy[i].name);
copy[i].max=copy[i].max-RR;
i++;
}
}
if(m==0) break;
}
}
void rr()
{
int x[50];
int i,j;
system("cls");
printf("NOTE1:job's name's long<=15\n");
printf("NOTE2:job's number<=50\n\n");
printf("-----------------------------------\n");
input_groud();
for(i=0;i<n;i++)
{
for(j=0;j<15;j++)
{
copy[i].name[j]=job[i].name[j];
}
copy[i].max=job[i].max;
}
output_groud();
printf("END\n\n\n");
getch();
}
void sjf()
{
int n,i,k,min;
int run_time=0;
int zong_time=0;
system("cls");
printf("请输入要执行进程的个数:\n");
scanf("%d",&n);
printf("请输入要执行进程的到达时间,区间时间:\n");
for(i=0;i<n;i++)
{
printf("P%d:",i+1);
scanf("%d,%d",&process[i].come_time,&process[i].temp_time);
}
for(i=0;i<n;i++)
{
zong_time=zong_time+process[i].temp_time;
}
while(run_time!=zong_time)
{
for(i=0;i<n;i++)
{
if(run_time==process[i].come_time)
{
processing[i]=process[i];
}
}
k=0;
for(i=0;i<n;i++)
{
if((processing[i].temp_time-processing[i].run_time)>0)
{
min=processing[i].temp_time-processing[i].run_time;
k=i;
break;
}
}
for(i=k;i<n;i++)
{
if((min>processing[i].temp_time-processing[i].run_time)&&(processing[i].temp_time-processing[i].run_time)>0)
{
min=processing[i].temp_time-processing[i].run_time;
k=i;
}
}
run_time++;processing[k].run_time++;
printf("P%d:%d,",k+1,run_time);
}
getch();
printf("\n");
}
void info() //初始化界面
{
printf("\n\tOS 进程调度实验(SJF/RR/Priorty)\n");
}
void sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *end;
int insert=0;
if ((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
end=first->link;
while (end!=NULL)
{
if ((p->super)>(end->super)) /*若插入进程比当前进程优先数大,*/
{
/*插入到当前进程前面*/
p->link=end;
first->link=p;
end=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first->link;
end=end->link;
}
}
if (insert==0) first->link=p;
}
}
void input() /* 建立进程控制块函数*/
{
int i,j;
system("cls"); /*清屏*/
printf("\n 所需运行进程个数: ");
scanf("%d",&j);
printf("\n 请输入%d个进程信息:\n ",j);
for (i=0;i<j;i++)
{
printf("\n No.%d",i+1);
p=getpch(PCB);
printf("\n\t进程名:");
scanf("%s",p->name);
printf("\n\t优先级:");
scanf("%d",&p->super);
printf("\n\t运行时间片数:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;
p->state='W';
p->link=NULL;
sort(); /* 调用sort函数*/
}
}
int space() //计算进程控制块的个数
{
int l=0;
PCB* pr=ready;
while (pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
int a,b,c;
printf("\n\t\t进程\t状态\t优先级\t已运行时间\t还需时间\n");
printf("\t\t%s\t",pr->name);
printf("%c \t ",pr->state);
printf("%d\t",pr->super);
printf("%d\t",pr->rtime);
a=pr->ntime;
b=pr->rtime;
c=a-b;
printf("\t%d\t",c);
printf("\n");
}
void check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n\t-->进程[%s]正在运行--\n",p->name); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n\t-->就绪队列状态--\n"); /*显示就绪队列状态*/
while (pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n**进程 [%s] 已完成**\n",p->name);
free(p);
}
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if (p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
(p->super)--;
p->state='W';
sort(); /*调用sort函数*/
}
}
void yanshi() // 高优先级优先算法的程序入口
{
int len,h=0;
char ch;
system("cls");
input();
len=space();
while ((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
system("cls");
printf("\n☆ 第 %d 个时间片☆\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n继续执行......");
}
printf("\n\n\t=======所有进程已经完成=======\n");
ch=getchar();
}
void menu() //菜单
{
int m;
system("cls");
putchar('\n');
printf("\t\t OS 进程调度实验(SJF/RR/Priorty)\n");
printf("\t\t----------------------------------\t\t\n");
printf("\n\n\t\t\t1.Priorty模拟过程演示.");
printf("\n\n\t\t\t2.SJF模拟过程演示.");
printf("\n\n\t\t\t3.RR模拟过程演示.");
printf("\n\n\t\t\t0.退出.");
printf("\n\n\t\t请选择:");
scanf("%d",&m);
switch (m)
{
case 3:
rr();
getch();
system("cls");
menu();
break;
case 2:
sjf();
getch();
system("cls");
menu();
break;
case 1:
yanshi(); //高优先级优先算法的程序入口
system("cls");
menu();
break;
case 0:
system("cls");
break;
default:
system("cls");
menu();
}
}
main() /*主函数*/
{
menu();
info();
}
操作系统进程调度实验C代码(SJF/RR/Priorty)
5星 · 超过95%的资源 需积分: 9 14 浏览量
2010-12-20
23:13:02
上传
评论
收藏 6KB RAR 举报
benfen007
- 粉丝: 11
- 资源: 17
最新资源
- Python 版冒泡排序算法源代码
- tensorflow-gpu-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- tensorflow-2.7.3-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.7.2-cp39-cp39-manylinux2010-x86-64.whl
- Python版本快速排序源代码
- Python 语言版的快速排序算法实现
- 450815388207377安卓_base.apk
- 超微主板 X9DRE-TF+ bios 支持 nvme启动
- 基于Python通过下载气象数据和插值拟合离散数据曲线实现对寒潮过程的能量分析
- 智能车仿真软件.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈