
公共静态无效的主要(字串[] args){
MyQueue的MyQueue的新MyQueue的()/ /声明队列
印刷电路板[PCB = {新的PCB(001 ,8,1),新的PCB(002,7,9),新的PCB(003,3,8),新的PCB(004,1,7),新的PCB(005,7,4)}
>PCB段=新的PCB()
(INT I = 0<pcb.length+ +){/ /初始化先进行排序,选择排序这里使用的是高优先级的一线队
(J =我<pcb.lengthJ + +){
(PCB [I]。特权<PCB [J]。特权){
段= PCB [1]
PCB [I] = PCB [J]
PCB [J] =段
}
}
}
体系。通过out.println(“入队后第一时间的进程的顺序:”)
(INT I = 0<pcb.length+ +){
的System.out调用println(第一次入队#程序名称:“+ PCB [我]。名称+ totaltime:”+ PCB [I]。totaltime +“的”特权“+ PCB [我]。特权)}
()
myqueue.start(PCB)
}
}
类MyQueue的{
INT指数= 0
PCB [] PC =新的PCB [5]
PCB [] PC1 =新的PCB [4]
PCB温度=新的PCB() BR />公共无效排队(PCB工艺){/ /排队算法
(指数== 5){
(“出界!”)
返回
}
PC [索引] =进程
指数+ +
}
公共:PCB DEQUEUE(){/ /出队算法(索引== 0)
返回空
(INT I = 0<pc1.length+ +){
PC1 [I] = PC [ +1]
}
指数 -
温度= PC [0]
(INT I = 0<pc1.length+ +){ BR />PC [I] = PC1 [I]
}
回报条件
}
公共无效启动(PCB [] PC){/ /进程表算法
(PC [0]。isNotFinish ==真| | PC [1 isNotFinish ==真| | PC [2 isNotFinish ==真| | PC [3]。时isNotFinish ==真| | PC [4]。isNotFinish ==){
/ / *注:| |运算符都是假的,所有的表达式结果为假,否则真
(INT I = 0<PC长度+ +){
PC [I]。运行(这一点)/>} 的System.out.println()
(INT I = 0<pc.length+ +){/ /处理每个运行一次运行的时间片的长度重新排序优先一旦
(J =我<pc.lengthJ + +){
如果(PC [I]特权<PC [J]。特权){
温度= PC [I]
PC [I] = PC [J]
PC [J] =温度
}
}
}
}
}
}
类PCB {/ /声明过程级
和int名,totaltime ,运行时特权
布尔isNotFinish的
公众PCB(){
}
公开PCB(名称,诠释totaltime特权){
this.name =的名称/ /进程名
this.totaltime = totaltime / /
this.privilege =特权/ /总时间优先 this.runtime = 2 / /时间片值是2
this.isNotFinish =真/ /是否执行完成
(“初始值:程序名称:”+名+“totaltime:”+ totaltime +“特权”+特权)
System.out的。调用println()
}
MyQueue的MQ公共无效的run(){/ /处理的基础上实施的时间片算法
(totalTime>1){ totaltime =运行/ /总时间大于1,总时间=总时间 - 时间片
特权 -
(“程序名称:”+姓名+“ remaintime:“+ +”特权“+特权)totaltime
的} else if(totaltime == 1){
totaltime - / /总时间为1时,执行时间为1
>特权 -
(“程序名称:”+姓名+“remaintime:”+ totaltime +“特权”+特权)
}其他{
isNotFinish =假/ / 0,将isNotFinish标志设置为假
}
如果(isNotFinish ==真){br mq.deQueue() mq.enQueue(本)}
}
}
真不容易啊,怕是没人弄了!优先级调度算法程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node
{
char name[10] /*进程标识符*/
int prio /*进程优先数*/
int round /*进程时间轮转时间片*/
int cputime/*进程占用CPU时间*/
int needtime/*进程到完成还要的时间*/
int count /*计数器*/
char state/*进程的状态*/
struct node *next/*链指针*/
}PCB
PCB *finish,*ready,*tail,*run/*队列指针*/
int N/*进程数*/
/*将就绪队列中的第一个进程投入运行*/
firstin()
{
run=ready /*就绪队列头指针赋值给运行头指针*/
run->state='R' /*进程状态变为运行态*/
ready=ready->next /*就绪对列头指针后移到下一进程*/
}
/*标题输出函数*/
void prt1(char a)
{
if(toupper(a)=='P') /*优先数法*/
printf(" name cputime needtime priority state\n")
else
printf(" name cputime needtime count round state\n")
}
/*进程PCB输出*/
void prt2(char a,PCB *q)
{
if(toupper(a)=='P') /*优先数法的输出*/
printf(" %-10s%-10d%-10d%-10d %c\n",q->name,
q->cputime,q->needtime,q->prio,q->state)
else/*轮转法的输出*/
printf(" %-10s%-10d%-10d%-10d%-10d %-c\n",q->name,
q->cputime,q->needtime,q->count,q->round,q->state)
}
/*输出函数*/
void prt(char algo)
{
PCB *p
prt1(algo) /*输出标题*/
if(run!=NULL) /*如果运行指针不空*/
prt2(algo,run)/*输出当前正在运行的PCB*/
p=ready /*输出就绪队列PCB*/
while(p!=NULL)
{
prt2(algo,p)
p=p->next
}
p=finish /*输出完成队列的PCB*/
while(p!=NULL)
{
prt2(algo,p)
p=p->next
}
getch() /*压任意键继续*/
}
/*优先数的插入算法*/
insert1(PCB *q)
{
PCB *p1,*s,*r
int b
s=q /*待插入的PCB指针*/
p1=ready/*就绪队列头指针*/
r=p1/*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与p1之间*/
{
r->next=s
s->next=p1
}
else
{
s->next=p1 /*否则插入在就绪队列的头*/
ready=s
}
}
/*轮转法插入函数*/
insert2(PCB *p2)
{
tail->next=p2 /*将新的PCB插入在当前就绪队列的尾*/
tail=p2
p2->next=NULL
}
/*优先数创建初始PCB信息*/
void create1(char alg)
{
PCB *p
int i,time
char na[10]
ready=NULL/*就绪队列头指针*/
finish=NULL /*完成队列头指针*/
run=NULL/*运行队列指针*/
printf("Enter name and time of process\n")/*输入进程标识和所需时间创建PCB*/
for(i=1i<=Ni++)
{
p=malloc(sizeof(PCB))
scanf("%s",na)
scanf("%d",&time)
strcpy(p->name,na)
p->cputime=0
p->needtime=time
p->state='w'
p->prio=50-time
if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
insert1(p)
else
{
p->next=ready/*创建就绪队列的第一个PCB*/
ready=p
}
}
clrscr()
printf(" output of priority:\n")
printf("************************************************\n")
prt(alg) /*输出进程PCB信息*/
run=ready/*将就绪队列的第一个进程投入运行*/
ready=ready->next
run->state='R'
}
/*轮转法创建进程PCB*/
void create2(char alg)
{
PCB *p
int i,time
char na[10]
ready=NULL
finish=NULL
run=NULL
printf("Enter name and time of round process\n")
for(i=1i<=Ni++)
{
p=malloc(sizeof(PCB))
scanf("%s",na)
scanf("%d",&time)
strcpy(p->name,na)
p->cputime=0
p->needtime=time
p->count=0/*计数器*/
p->state='w'
p->round=2 /*时间片*/
if(ready!=NULL)
insert2(p)
else
{
p->next=ready
ready=p
tail=p
}
}
clrscr()
printf(" output of round\n")
printf("************************************************\n")
prt(alg) /*输出进程PCB信息*/
run=ready /*将就绪队列的第一个进程投入运行*/
ready=ready->next
run->state='R'
}
/*优先数调度算法*/
priority(char alg)
{
while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
{
run->cputime=run->cputime+1
run->needtime=run->needtime-1
run->prio=run->prio-3/*每运行一次优先数降低3个单位*/
if(run->needtime==0) /*如所需时间为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'
insert1(run)
firstin()/*将就绪队列的第一个进程投入运行*/
}
prt(alg)/*输出进程PCB信息*/
}
}
/*时间片轮转法*/
roundrun(char alg)
{
while(run!=NULL)
{
run->cputime=run->cputime+1
run->needtime=run->needtime-1
run->count=run->count+1
if(run->needtime==0)/*运行完将其变为完成态,插入完成队列*/
{
run->next=finish
finish=run
run->state='F'
run=NULL
if(ready!=NULL)
firstin()/*就绪对列不空,将第一个进程投入运行*/
}
else
if(run->count==run->round) /*如果时间片到*/
{
run->count=0 /*计数器置0*/
if(ready!=NULL) /*如就绪队列不空*/
{
run->state='W'/*将进程插入到就绪队列中等待轮转*/
insert2(run)
firstin()/*将就绪对列的第一个进程投入运行*/
}
}
prt(alg)/*输出进程信息*/
}
}
/*主函数*/
main()
{
char algo /*算法标记*/
clrscr()
printf("type the algorithm:P/R(priority/roundrobin)\n")
scanf("%c",&algo)/*输入字符确定算法*/
printf("Enter process number\n")
scanf("%d",&N)/*输入进程数*/
if(algo=='P'||algo=='p')
{
create1(algo)/*优先数法*/
priority(algo)
}
else
if(algo=='R'||algo=='r')
{
create2(algo)/*轮转法*/
roundrun(algo)
}
}
#include "stdio.h"#include "stdlib.h"
#include "string.h"
struct PCB {
char NAME[10]/*进程名*/
int ROUND/*进程轮转时间片*/
int REACHTIME/*进程到达时间*/
int CPUTIME/*进程占用CPU时间*/
int COUNT/*计数器*/
int NEEDTIME/*进程完成还要的CPU时间*/
char STATE/*进程的状态*/
struct PCB *NEXT/*链指针*/
}
struct LINK { /*PCB的链结构*/
struct PCB *RUN/*当前运行进程指针*/
struct PCB *READY/*就绪队列头指针*/
struct PCB *TAIL/*就绪队列尾指针*/
struct PCB *FINISH/*完成队列头指针*/
}
void INIT(LINK *)/*对PCB的链结构初始化*/
void INSERT(LINK *)/*将执行了一个单位时间片数且还未完成的进程的PCB插到就绪队列的队尾*/
void FIRSTIN(LINK *)/*将就绪队列中的第一个进程投入运行*/
void PRINT(LINK *)/*打印每执行一个时间片后的所有进程的状态*/
void PR(PCB *)/*打印一个进程的状态*/
int CREATE(LINK *,int)/*创建新的进程*/
void ROUNDSCH(LINK *)/*按时间片轮转法调度进程*/
void main() {
LINK pcbs
int i
INIT(&pcbs)
i=0
printf("创建5个进程\n\n")
while(i<5) {
if(CREATE(&pcbs,i+1)==1) {
printf("进程已创建\n\n")
i++
}
else
printf("进程创建失败\n\n")
}
FIRSTIN(&pcbs)
ROUNDSCH(&pcbs)
}
void ROUNDSCH(LINK *p) {
PCB *pcb
while(p->RUN!=NULL) {
pcb=(PCB *)malloc(sizeof(PCB))
strcpy(pcb->NAME,p->RUN->NAME)
pcb->ROUND=p->RUN->ROUND
pcb->REACHTIME=p->RUN->REACHTIME
pcb->CPUTIME=p->RUN->CPUTIME
pcb->COUNT=p->RUN->COUNT
pcb->NEEDTIME=p->RUN->NEEDTIME
pcb->STATE=p->RUN->STATE
pcb->NEXT=p->RUN->NEXT
pcb->CPUTIME++
pcb->NEEDTIME--
pcb->COUNT++
if(pcb->NEEDTIME==0) {
pcb->NEXT=p->FINISH->NEXT
p->FINISH->NEXT=pcb
pcb->STATE='F'
p->RUN=NULL
if(p->READY!=p->TAIL)
FIRSTIN(p)
}
else {
p->RUN=pcb
if(pcb->COUNT==pcb->ROUND) {
pcb->COUNT=0
if(p->READY!=p->TAIL) {
pcb->STATE='W'
INSERT(p)
FIRSTIN(p)
}
}
}
PRINT(p)
}
}
void INIT(LINK *p) {
p->RUN=NULL
p->TAIL=p->READY=(PCB *)malloc(sizeof(PCB))
p->READY->NEXT=NULL
p->FINISH=(PCB *)malloc(sizeof(PCB))
p->FINISH->NEXT=NULL
}
int CREATE(LINK *p,int n) {
PCB *pcb,*q
pcb=(PCB *)malloc(sizeof(PCB))
flushall()
printf("请输入第%d个进程的名称:\n",n)
gets(pcb->NAME)
printf("请输入第%d个进程的轮转时间片数:\n",n)
scanf("%d",&(pcb->ROUND))
printf("请输入第%d个进程的到达时间:\n",n)
scanf("%d",&(pcb->REACHTIME))
pcb->CPUTIME=0
pcb->COUNT=0
printf("请输入第%d个进程需运行的时间片数:\n",n)
scanf("%d",&(pcb->NEEDTIME))
pcb->STATE='W'
pcb->NEXT=NULL
if(strcmp(pcb->NAME,"")==0||pcb->ROUND<=0||pcb->NEEDTIME<=0) /*输入错误*/
return 0
q=p->READY
while(q->NEXT!=NULL&&q->NEXT->REACHTIME<=pcb->REACHTIME)
q=q->NEXT
pcb->NEXT=q->NEXT
q->NEXT=pcb
if(pcb->NEXT==NULL)
p->TAIL=pcb
return 1
}
void FIRSTIN(LINK *p) {
PCB *q
q=p->READY->NEXT
p->READY->NEXT=q->NEXT
q->NEXT=NULL
if(p->READY->NEXT==NULL)
p->TAIL=p->READY
q->STATE='R'
p->RUN=q
}
void INSERT(LINK *p) {
PCB *pcb
pcb=(PCB *)malloc(sizeof(PCB))
strcpy(pcb->NAME,p->RUN->NAME)
pcb->ROUND=p->RUN->ROUND
pcb->REACHTIME=p->RUN->REACHTIME
pcb->CPUTIME=p->RUN->CPUTIME
pcb->COUNT=p->RUN->COUNT
pcb->NEEDTIME=p->RUN->NEEDTIME
pcb->STATE=p->RUN->STATE
pcb->NEXT=p->RUN->NEXT
p->TAIL->NEXT=pcb
p->TAIL=pcb
p->RUN=NULL
pcb->STATE='W'
}
void PRINT(LINK *p) {
PCB *pcb
printf("执行一个时间片后的所有进程的状态:\n\n")
if(p->RUN!=NULL)
PR(p->RUN)
if(p->READY!=p->TAIL) {
pcb=p->READY->NEXT
while(pcb!=NULL) {
PR(pcb)
pcb=pcb->NEXT
}
}
pcb=p->FINISH->NEXT
while(pcb!=NULL) {
PR(pcb)
pcb=pcb->NEXT
}
}
void PR(PCB *p) {
printf("进程名:%s\n",p->NAME)
printf("进程轮转时间片:%d\n",p->ROUND)
printf("进程到达时间:%d\n",p->REACHTIME)
printf("进程占用CPU时间:%d\n",p->CPUTIME)
printf("计数器:%d\n",p->COUNT)
printf("进程完成还要的CPU时间:%d\n",p->NEEDTIME)
printf("进程的状态:%c\n\n",p->STATE)
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)