作業系統程序排程程式碼
//資料:程序,佇列結構
//處理流程:
//1 初始化--程序佇列結構(包括:就緒佇列,等待佇列,執行佇列)等必要的資料結構 init();
//2 進入無限迴圈,反覆排程佇列
#define MAX 5
#include<stdio.h>
#include<stdlib.h>
int total_time=20;
int time_slice=3;
typedef struct process { // 程序控制塊
char pname[10];
int WaitTime;
int BurstTime;
int priority; // 數字越小優先順序越高
struct process *next;
}PROCESS;
//typedef struct process PROCESS;
PROCESS * in_queue(PROCESS *head,PROCESS *p); //宣告
PROCESS *init() //程序初始化
{
int i=0;
char a;
PROCESS *head_new; //佇列的隊頭
head_new=(struct process*)malloc(sizeof(struct process));
if(!head_new) exit(1);
head_new=NULL;
do
{
struct process *s;
printf("initialize the process:\n");
s=(struct process *)malloc(sizeof(struct process));
if(!s) exit(1);
printf("please input the pname:WaitTime: BurstTime: priority:\n");
scanf("%c",&(s->pname));
scanf("%d",&(s->WaitTime));
scanf("%d",&(s->BurstTime));
scanf("%d",&(s->priority));
s->next=NULL;
in_queue(head_new,s);
i++;
printf("do u want to insert process more ?? 'Y'or'N'\n");
printf("----------------------------------------'\n");
scanf("%c",&a);
scanf("%c",&a);
// if(a=='Y'||a=='y') continue;
// else if(a=='N'||a=='n') break;
}while((i<MAX) &&(a=='Y'||a=='y'));
return head_new;
}
///////////////////////////////////////////////////////////
PROCESS *in_queue(PROCESS *head,PROCESS *p) //入隊函式
{
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
}
// printf("the process insert into the mothball queue :\n");
return head;
}
/////////////////////////////////////////////////////////////
/*
void new_queue() //後備佇列 先來先服務方式進入就緒
{
return *head_new;
}
*/
PROCESS *FCFS_process()
{
PROCESS *p,*q,*a; //a用來記錄選中結點的前一個結點
q=p=init(); //這裡是不是有個問題??
while(p->next!=NULL)
{
a=p;
if(p->WaitTime>=q->WaitTime)
{
q=p;
p=p->next;
}
}
q->WaitTime--;
if(q->WaitTime==0) //如果等待時間為0則把該程序從後備佇列中移除
{
a->next=p->next;
free(p);
}
return q; //選擇等待時間最久的)
}
//////////////////////就緒佇列,入口函式為就緒佇列的頭指標/////////////
int count=0;
PROCESS *ready_queue(PROCESS *head) //就緒佇列 優先順序進入執行 4道
{
PROCESS *p;
while(count<4)
{
p=FCFS_process();
p->next=head->next;
head=p;
count++;
printf("the process has inserted into the ready queue :\n");
}
return head;
}
//insert_ready() //
PROCESS *high_priority(PROCESS *P) //選擇優先順序最高的程序
{
PROCESS *q,*p; //問題,入口形參中
q=p;
while(p->next!=NULL)
{
if(q->priority>p->priority)
q=p;
p=p->next;
}
return q;
}
PROCESS *pick_ready(PROCESS *a) //從就緒佇列中選擇程序執行
{
PROCESS *p=ready_queue(a);
PROCESS *b=high_priority(p);
return b;
}
void run(PROCESS *a) //執行一個時間片
{
while((time_slice>0)&&(a->BurstTime>0)) //指標用-> 變數用.
{
printf("the process %c is runing",a->pname);
printf("the process BurstTime time is %d ",a->WaitTime);
printf("the process BurstTime time is %d ",a->BurstTime);
printf("the process priority is %d ",a->priority);
a->BurstTime--;
time_slice--;
}
a->priority--;
total_time--;
/*
if(a->runtime>0)
return a;
else return NULL;*/
}
void main()
{
PROCESS *p;
PROCESS *head=init();
while(total_time!=0)
{
ready_queue(head);
p=pick_ready(head);
if(p->BurstTime==0)
{
p=p->next;
free(p);
count--;
}
run(p);
time_slice=3;
}
}