#include"stdio.h" |
#include"stdlib.h" |
#include<iostream> |
using namespace std; |
#define ready 1 |
#define run 2 |
struct pcb |
{ |
char name[10]; |
int priority; /*进程的优先级*/ |
int state; /*进程的状态:可以有运行和就绪*/ |
int needtime; /*进程需要运行的时间*/ |
int runtime; |
int time ; /*进程已运行的时间*/ |
struct pcb *next; /*指向下一个进程PCB的指针*/ |
}; |
typedef struct pcb PCB; |
PCB *head=NULL; |
void create() /*此函数用于创建进程队列*/ |
{ |
PCB *p, *q; |
int n,i; |
printf ( "请输入要创建的进程的数量:" ); |
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 ( "\n请输入 NO.%d 个进程的标识符:" ,i); |
scanf ( "%s" ,&p->name); |
printf ( "请输入进程的优先级:" ); |
scanf ( "%d" ,&p->priority); |
printf ( "请输入进程所需的时间:" ); |
scanf ( "%d" ,&p->needtime); |
p->state=ready; |
p->runtime=0; |
p->next=NULL; |
} |
} |
void lunzhuan () /*时间片轮转*/ |
{ |
PCB *p,*q,*r; |
int time ; |
printf ( "请输入时间片大小:\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正在运行的进程是:\n" ); /*输出该进程的信息*/ |
printf ( "%s\t" ,p->name); |
printf ( "state: 运行\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 ( "就绪队列是:\n" ); |
else |
printf ( "就绪队列为空!\n" ); |
while (q) |
{ |
printf ( "%s\t" ,q->name); |
printf ( "state: 就绪\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 main() |
{ |
cout<< "开始创建进程:" <<endl; |
create(); |
if (head==NULL || head->next==NULL) |
printf ( "就绪队列没有进程,请先创建进程\n" ); |
else |
lunzhuan(); |
} |
|
初级程序员
by: 云代码会员 发表于:2016-06-20 09:55:41 顶(0) | 踩(0) 回复
不错不错不错
回复评论