這是作業系統課上的一次作業,我是屬於旁聽去的,也許正是由於旁聽的緣故,對作業總是抱著一中拖延的心態,沒花什麼心思在這門課的作業上。這次的進程調度作業其實早已經上交了,這兩天想寫點程式了,順便更新下部落格裡面的內容。閑話少說,上程式了,不會組織語言,只能多寫點注釋了
基本構造:
使用資料結構struct PCB表示進程, 記錄進程的相關資訊,設定三個隊列(鏈表),分別是就緒隊列、執行隊列和等待PCB隊列。程式初始化的時候先建立空閑PCB隊列,然後根據使用者輸入的進程數量建立進程並構成進程就緒隊列。隨機調度進程,最後結束調度,輸出所有進程狀態。
#define RUNNING 0 #define READY 1 #define WAIT 2 #include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <time.h>typedef struct PCB{int pid;//進程的標識號int priority;//進程的優先順序,數值越大,優先順序越高int pstatus;//進程的狀態struct PCB * next;//指向下一個進程的指標}PCB;PCB * Create(int n){//head指向前端節點的指標,cur表示指向當前結點的指標PCB * head,* temp,* cur;int i;head=(PCB *)malloc(sizeof(PCB));//分配空間if(head==NULL)//分配失敗{printf("ERROR");exit(1);}cur=head;head->next=NULL;//頭結點for(i=0;i<n;i++){temp=(PCB *)malloc(sizeof(PCB));if(!temp){printf("ERROR");exit(1);}temp->pid=i+1; //初始化進程的標識號temp->pstatus=READY;//進程剛建立時,初始化其狀態為"就緒態"printf("\n請輸入進程 %d 的優先順序別(Int型) :",temp->pid);scanf("%d",&temp->priority);//由使用者輸入進程的優先順序並初始化,寫入對應的PCB表temp->next=NULL;cur->next=temp;cur=cur->next;}return head;//返回指向頭結點的頭指標}void Insert(PCB * head,PCB * cur){if(cur==NULL)return;for(;head->next!=NULL;head=head->next);cur->next=NULL;head->next=cur;}PCB * readyTorun(PCB * head){PCB * temp1=head,* temp=head->next;if(head->next==NULL){printf("The Ready is NUll!");temp=NULL;}for(;head->next!=NULL;head=head->next){if(temp->priority < head->next->priority)//當前優先順序小於下一結點優先順序{temp1=head;temp=head->next;//temp記錄選中的進程的PCB}}if(temp!=NULL)temp1->next=temp->next;//從就緒進程的PCB鏈表中去除選中進程的PCBreturn temp;//返回該結點}void blockToready(PCB * ready,PCB * wait,int j){PCB * temp1=wait,* temp=wait->next;if(wait->next==NULL){printf("The Wait is Null!");}for(;wait->next!=NULL;wait=wait->next)//尋找指定進程的PCB{if(wait->next->pid==j){temp1=wait;temp=wait->next;}}if(temp!=NULL){temp1->next=temp->next;//從等待PCB鏈表中去除選中的PCBtemp->pstatus=READY;//將選中進程的PCB狀態改為就緒態}Insert(ready,temp);//向就緒PCB鏈表中插入選中的PCB}void print(PCB * head){PCB * temp=head->next;if(head->next==NULL){printf("It is NULL");return;}for(;temp!=NULL;temp=temp->next){printf("p<%d> ",temp->pid);}}void Display(PCB * r){for(;r->next!=NULL;r=r->next){printf("\np<%d> information : PID: %d Priority: %d Pstatus: %d",r->next->pid,r->next->pid,r->next->priority,r->next->pstatus);}}void Destroy(PCB * head){PCB * temp=head->next,* cur=NULL;if(temp==NULL)return;for(;temp!=NULL;){cur=temp;temp=temp->next;free(cur);}}int main(){int n,k=1,j;struct PCB * ready_list=NULL,* wait_list=NULL,* run=NULL;wait_list=(struct PCB *)malloc(sizeof(struct PCB));wait_list->next=NULL;printf("請輸入準備建立的進程數 :");scanf("%d",&n);ready_list=Create(n);//根據使用者輸入,建立進程run=readyTorun(ready_list);//從就緒PCB鏈表中選擇優先順序最高的進程,進入CPU運行srand((unsigned)time(NULL));//產生一個隨機種子 while(k!=0){int i = rand() % 4 ;//在0-3中隨機產生一個數,0表示退出k=i; printf("\n=================進程調度==================");if(run!=NULL){run->pstatus=RUNNING;printf("\np<%d> is Running ......",run->pid);}printf("\nThe Ready : ");print(ready_list);printf("\nThe Wait : ");print(wait_list);switch(i){case 1 : run->pstatus=READY;Insert(ready_list,run);//將當前運行態的進程轉為就緒態run=readyTorun(ready_list);//系統自動從就緒PCB鏈表選擇高優先順序進程投入運行態break;case 2 :run->pstatus=WAIT;Insert(wait_list,run);//將當前運行態的進程轉為等待態run=readyTorun(ready_list);//系統自動從就緒PCB鏈表選擇高優先順序進程投入運行態break;case 3 : printf("\n輸入等待態(Wait)轉換為就緒態(Ready)的進程標識號 : ");//將指定的等待態進程轉為就緒態scanf("%d",&j);blockToready(ready_list,wait_list,j);break;default :break;}}//調度結束,輸出建立進程的相關資訊printf("\n進程類比調度結束\n");printf("\n所有進程資訊詳細資料 :\n");Display(wait_list);Display(ready_list); printf("\n p<%d> information : PID: %d Priority: %d Pstatus: %d\n",run->pid,run->pid,run->priority,run->pstatus);//銷毀建立進程的鏈表Destroy(ready_list);Destroy(wait_list);ready_list=NULL;return 0;}