#define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0
struct pcb { /* 定义进程控制块PCB */ char name[10]; char state; int super; int ntime; int rtime;
struct pcb* link; }*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立对进程进行优先级排列函数*/ {
PCB *first, *second; int insert=0;
if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/ {
p->link=ready; ready=p; }
else /* 进程比较优先级,插入适当的位置中*/ {
first=ready;
second=first->link; while(second!=NULL) {
if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/ { /*插入到当前进程前面*/ p->link=second; first->link=p; second=NULL; insert=1; }
else /* 插入进程优先数最低,则插入到队尾*/ {
first=first->link;
second=second->link; } }
if(insert==0) first->link=p; } }
void input() /* 建立进程控制块函数*/ {
int i,num;
system(\"cls\"); /*清屏*/
printf(\"\\n 请输入进程数: \"); scanf(\"%d\ for(i=1;i<=num;i++) {
printf(\"\\n 进程号No.%d:\\n\ p=getpch(PCB);
printf(\"\\n 输入进程名:\"); scanf(\"%s\
printf(\"\\n 输入进程优先数:\"); scanf(\"%d\
printf(\"\\n 输入进程运行时间:\");
scanf(\"%d\ 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) /*建立进程显示函数,用于显示当前进程*/ {
printf(\"\\n 进程名\ 状态\ 优先数\ 需要运行时间\ 已经运行时间\\n\"); printf(\"|%s\\ printf(\"|%c\\ printf(\"|%d\\ printf(\"|%d\\\ printf(\"|%d\\ printf(\"\\n\"); }
void check() /* 建立进程查看函数 */ {
PCB* pr;
printf(\"\\n **** 当前正在运行的进程是:\\n\"); /*显示当前运行进程*/ disp(p); pr=ready;
printf(\"\\n **** 当前就绪队列状态为:\\n\"); /*显示就绪队列状态*/ while(pr!=NULL) {
disp(pr);
pr=pr->link; } }
void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ {
printf(\"\\n 进程 [%s] 已完成.\\n\ free(p); }
void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ {
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/ else {
(p->super)--; p->state='W';
sort(); /*调用sort函数*/ } }
void main() /*主函数*/ {
int len,h=0; char ch; input(); len=space();
while((len!=0)&&(ready!=NULL)) {
ch=getchar(); h++;
printf(\"-----------------------------------------------------\"); printf(\"\\n 现在是第%d次运行: \\n\ p=ready;
ready=p->link; p->link=NULL; p->state='R'; check(); running();
printf(\"\\n 按任意键继续......\\n\"); }
printf(\"\\n\\n 进程已经完成.\\n\"); }
第三题:
按时间片轮法调度 #define N 10 #include #includetypedef struct pcb { char pname[N]; int runtime; int arrivetime; char state; struct pcb*next; }PCB;
PCB head_input;
PCB head_run; PCB *pcb_input;
static char R='r',C='c'; unsigned long current; void inputprocess(); int readydata(); int runprocess(); int readyprocess(); FILE * f;
/*检查就绪队列并准备运行进程的函数*/ int readyprocess() { while(1) { if(readydata()==0) return 1; else runprocess(); } }
/*判断就绪队列是否为空的函数*/ int readydata() { if(head_input.next==NULL) { if(head_run.next==NULL) return 0; else return 1; } PCB *p1,*p2,*p3; p1=head_run.next; p2=&head_run; while(p1!=NULL) { p2=p1; p1=p2->next; } p1=p2;
p3=head_input.next; p2=&head_input; while(p3!=NULL) { if(((unsigned long)p3->arrivetime<=current)&&(p3->state==R)) { printf(\"Time is %4d ; Process %s start,\\n\ (current+500)/1000,p3->pname); fprintf(f,\"Time is %4d ; Process %s start,\\n\ (current+500)/1000,p3->pname); p2->next=p3->next; p3->next=p1->next; p1->next=p3; p3=p2; } p2=p3; p3=p3->next; } return 1; }
int runprocess() { PCB *p1,*p2; if(head_run.next==NULL) { current++; return 1; } else { p1=head_run.next; p2=&head_run; while(p1!=NULL) { p1->runtime--; current++; if(p1->runtime<=0) { printf(\"Time is %4d;Process %s end.\\n\ fprintf(f,\"Time is %4d;Process %s end.\\n\ p1->state=C; p2->next=p1->next;
delete p1; p1=NULL; } else { p2=p1; p1=p2->next; } } return 1; } }
void inputprocess() { PCB *p1,*p2; int num; printf(\"How many processes do you want to run:\"); fprintf(f,\"How many processes do you want to run:\"); scanf(\"%d\ fprintf(f,\"%d\\n\ p1=&head_input; p2=p1; p1->next=new PCB; p1=p1->next; for(int i=0;iruntime=(p1->runtime)*1000; p1->arrivetime=(p1->arrivetime)*1000; p1->state=R;p1->next=new PCB; p2=p1; p1=p1->next; } delete p1; p1=NULL; p2->next=NULL; }
/* 主函数 */ void main() { f=fopen(\"result.txt\ printf(\"\\n time l=1000 time slice\\n\"); fprintf(f,\"\\n time l=1000 time slice\\n\"); current=0; inputprocess(); readyprocess(); getch(); fclose(f); }