操作系统-进程管理实验C语言
时间:2025-04-20
时间:2025-04-20
短作业优先、时间片轮转、动态可剥夺优先数
#include"stdio.h"
#include"stdlib.h"
#define ready 1
#define run 2
struct pcb
{
char name[10];
int priority; /*进程的优先级*/
int state; /*进程的状态:可以有run、ready、finish(可有可无)*/ int needtime; /*进程需要运行的时间*/
int runtime;
int time; /*进程已运行的时间*/
struct pcb *next; /*指向下一个进程PCB的指针*/
};
typedef struct pcb PCB;
PCB *head=NULL;
/*此函数用于创建进程队列*/
void create(void)
{ PCB *p, *q;
int n,i;
printf("Enter the number of the process:");
scanf("%d",&n); /*输入要创建的进程的数量*/ head=(PCB *)malloc(sizeof(PCB)); /*创建一个表头结点*/
p=head;
for(i=1;i<=n;i++) /*用循环来创建指定个结点*/ { q=(PCB*)malloc(sizeof(PCB));
p->next=q;
p=q;
printf("\nenter the NO.%d name of process:",i);
scanf("%s",&p->name);
printf("enter the priority of process:");
scanf("%d",&p->priority);
printf("enter the time need:");
scanf("%d",&p->needtime);
p->state=ready;
p->runtime=0;
p->next=NULL;
}
}
短作业优先、时间片轮转、动态可剥夺优先数
/*删除执行完毕的进程*/
void delete(PCB *head,PCB *p)
{PCB *q;
q=head;
while(q->next!=p)
q=q->next;
q->next=p->next;
free(p);
}
/*找出执行时间最短的进程*/
PCB *getminneedtime(PCB *head)
{PCB *p,*q;
p=head->next;
q=p->next;
while(q)
{if(p->needtime>q->needtime)
p=q;
q=q->next;
}
return(p);
}
/*找出优先级最高的进程*/
PCB *getpriority(PCB *head)
{PCB *p,*q;
p=head->next;
q=p->next;
while(q)
{if (p->priority>q->priority)
p=q;
}
return (p);
}
/*时间片轮转*/
void RR (void)
{ PCB *p,*q,*r;
int time;
printf("input the time:\n ");
scanf("%d",&time);
for(p=head->next;p->next;p=p->next)
{r=p;}
短作业优先、时间片轮转、动态可剥夺优先数
while(head->next)
{p=head->next; /*选出就绪队列的第一个进程*/
p->state=run;
printf("\n***Now the running process is:\n"); /*输出该进程的信息*/
printf("%s\t",p->name);
printf("state:run\t");
printf("needtime: %d\t",p->needtime);
printf("runtime: %d\n",p->needtime);
q=head->next;
if(p->needtime-p->runtime<=p->time) /*时间片内,进程运行结束否*/ {p->runtime=p->needtime;
printf("runtime:%d\n",p->runtime);
}
else
{p->runtime=p->runtime+p->time;
printf("runtime:%d\n",p->runtime);
}
q=p->next;
if(q!=NULL) /*输出就绪队列中的进程信息*/ printf("***Now the ready quenue is:\n");
else printf("***Now the ready quenue is NONE!");
while(q)
{
printf("%s\t",q->name);
printf("state:ready\t");
printf("needtime:%d\t",q->needtime);
printf("runtime:%d\t",q->runtime);
printf("\n");
q=q->next;
}
if(p->runtime==p->needtime)
{delete(head,p);
}
else
{head->next=p->next;
r->next=p;
r=p;
r->state=ready;
r->next=NULL;
}}
}
/*优先权调度算法*/
void HPF (void)
{PCB *p,*q;
短作业优先、时间片轮转、动态可剥夺优先数
int flag=1;
while(head->next)
{ if(flag==1)
{p=getpriority(head); /*调用“找出执行时间最短的进程”函数*/
p->state=run;
} /*将选出的进程的状态改为“run”*/
(p->runtime)++;
(p->priority)--;
printf("\n***Now the running process is:\n"); /*输出该进程的信息*/
printf("%s\t",p->name);
printf("state:run\t");
printf("needtime: %d\t",p->needtime);
printf("runtime: %d\n",p->runtime);
q=head->next;
if(q->next!=NULL) /*输出就绪队列中的进息*/ printf("***Now the ready quenue is:\n");
else printf("***Now the ready quenue is NONE!");
while(q)
{if(q!=p)
{ printf("%s\t",q->name);
printf("state:ready\t");
printf("needtime:%d\t",q->needtime);
printf("runtime:%d\t",q->runtime);
printf("\n");
}
q=q->next;
}
if(p->runtime==p->needtime)
{ delete(head,p);
}
else
{for(q=head->next;q;q=q->next)
if(p->priority>=q->priority)
{flag=0;
}
else { flag=1;
p->state=ready;
}
}
/*将该运行结束的进程删除掉*/
短作业优先、时间片轮转、动态可剥夺优先数
}
}
/*短作业优先*/
void SJF (void)
{PCB *p,*q;
while(head->next)
{ p=getminneedtime(head); /*调用“找出执行时间最短的进程”函数*/ p->state=run; /*将选出的进程的 …… 此处隐藏:2419字,全部文档内容请下载后查看。喜欢就下载吧 ……