?? proc.c
字號:
/*
Name: proc.c 進程調度模擬實驗源碼 存儲結構鏈表
Copyright:
Author: Wugy
Date: 08-04-07 17:04
Description:
實現一個有 N個進程并發執行的進程調度模擬程序。
進程調度算法:采用最高優先數優先的調度算法(即把處理機分配給優先數最高的進程)和先來先服務算法。
*/
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
struct pcb { /* 定義進程控制塊PCB */
char name[10];
char state;
int prio;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
sort() /* 進程進行優先級排列函數*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->prio)>(ready->prio))) /*優先級最大者,插入隊首*/
{
p->link=ready;
ready=p;
}
else /* 進程比較優先級,插入適當的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->prio)>(second->prio)) /*若插入進程比當前進程優先數大,*/
{ /*插入到當前進程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else /* 插入進程優先數最低,則插入到隊尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
input() /* 建立進程控制塊函數*/
{
int i,num;
/*clrscr(); */ /*清屏*/
printf("\n 請輸入進程數?");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n 進程號No.%d:\n",i);
p=getpch(PCB); /*宏(type*)malloc(sizeof(type)) */
printf("\n 輸入進程名:");
scanf("%s",p->name);
printf("\n 輸入進程優先數:");
scanf("%d",&p->prio);
printf("\n 輸入進程運行時間:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='r';
p->link=NULL;
sort(); /* 調用sort函數*/
}
}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
disp(PCB * pr) /*單個進程顯示函數*/
{
printf("\n qname \t state \t prio \t ndtime \t runtime \n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->prio);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
check() /* 顯示所有進程狀態函數 */
{
PCB* pr;
printf("\n **** 當前正在運行的進程是:%s",p->name); /*顯示當前運行進程*/
disp(p);
pr=ready;
printf("\n ****當前就緒隊列狀態為:\n"); /*顯示就緒隊列狀態*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
destroy() /*進程撤消函數(進程運行結束,撤消進程)*/
{
printf("\n 進程 [%s] 已完成.\n",p->name);
free(p);
}
running() /* 運行函數。判斷是否完成,完成則撤銷,否則置就緒狀態并插入就緒隊列*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 調用destroy函數*/
else
{
(p->prio)--;
p->state='r';
sort(); /*調用sort函數*/
}
}
main() /*主函數*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
/*getchar();*/
h++;
printf("\n The execute number:%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n 按任一鍵繼續......");
ch=getchar();
}
printf("\n\n 進程已經完成.\n");
ch=getchar();
ch=getchar();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -