操做系統實驗報告(時間片輪調度算法)

操做系統實驗報告

  1. 實驗題目
     進程調度,先來先服務調度算法和時間片輪轉調度算法。
  2. 實驗目的及要求

進程調度是處理機管理的核心內容。經過本實驗加深理解有關進程控制塊、進程隊列的概念。並體會和了解先來先服務調度算法和時間片輪轉調度算法的具體實施辦法。node

  1. 總的實驗思想,及語言環境,工具

語言環境:C語言;ios

工具:vc 6.0;算法

 

調度算法的實現思想:系統把全部就緒進程按先進先出的原則排成一個隊列。新來的進程加到就緒隊列末尾。每當執行進程調度時,進程調度程序老是選出就緒隊列的隊首進程,讓它在CPU上運行一個時間片的時間。當時間片到,產生時鐘中斷,調度程序便中止該進程的運行,並把它放入就緒隊列末尾,而後,把CPU分給就緒隊列的隊首進程。時間片:是一個小的時間單位,一般10~100ms數量級。數據結構

  1. 數據結構與模塊說明

(1  ).數據結構:函數

typedef struct node工具

{spa

char name[10];操作系統

int prio;調試

int round;隊列

int cputime;

int needtime;

int count;

char state;

struct node* next;

}PCB;

(2).插入函數模塊:

void insert(PCB* q)

{

PCB *p1,*s,*r;

s = q;

p1 = ready;

r = p1;

while(p1!=NULL)

      if(p1->round<=s->round)

      {

            r = p1;

            p1 = p1->next;

      }

      if(r!=p1)

      {

            r->next = s;

            s->next = p1;

      }

      else

      {

            s->next = p1;

            ready = s;

      }

}

(3).建立函數模塊:

void create()

{

PCB* p;

int i,time;

char na[10];

ready = NULL;

finish = NULL;

run = NULL;

cout<<"輸入進程名及其須要的運行時間:"<<endl;

for(i=1;i<N;i++)

{

      p = new PCB;

      cin>>na;

      cin>>time;

      strcpy(p->name,na);

      p->cputime = 0;

      p->needtime = 0;

      p->state = 'W';

      p->round = 0;

      if(ready!=NULL)

            insert(p);

      else

      {

            p->next = ready;

            ready = p;

      }

      cout<<"輸入進程名及其須要運行的時間:"<<endl;

}

prt();

run = ready;

ready = ready->next;

run->state = 'R';

}

(4).時間片輪轉函數模塊

void timeslicecycle()

{

while(run != NULL)

{

      run->cputime = run->cputime+8;

      run->needtime = run->needtime-8;

      run->round = run->round+8;

      if(run->needtime<=0)

      {

            run->next = finish;

            finish = run;

            run->state = 'F';

            run = NULL;

            if(ready != NULL)

                 firstin();

      }

      else

      {

            run->state = 'W';

            insert(run);

            firstin();

      }

      prt();

}

}

  1. 源代碼

#include<stdio.h>

#include<iostream.h>

#include<string.h>

 

typedef struct node

{

char name[10];

int prio;

int round;

int cputime;

int needtime;

int count;

char state;

struct node* next;

}PCB;

 

PCB *finish,*ready,*tail,*run;

int N;

 

void firstin()

{

run = ready;

run->state = 'R';

ready = ready->next;

}

 

void prt1()

{

cout<<" "<<endl;

cout<<"Name  CPUtime Needtime Round State"<<endl;

}

 

void prt2(PCB *q)

{

cout<<q->name<<"        "<<q->cputime<<"        "<<q->needtime<<"    "<<q->round<<"    "<<q->state<<endl;

}

 

void prt()

{

PCB *p;

cout<<"時間片輪轉調度算法"<<endl;

prt1();

if(run!=NULL)

      prt2(run);

p = ready;

while(p!=NULL)

{

      prt2(p);

      p = p->next;

}

p = finish;

while(p!=NULL)

{

      prt2(p);

      p = p->next;

}

getchar();

}

 

void insert(PCB* q)

{

PCB *p1,*s,*r;

s = q;

p1 = ready;

r = p1;

while(p1!=NULL)

      if(p1->round<=s->round)

      {

            r = p1;

            p1 = p1->next;

      }

      if(r!=p1)

      {

            r->next = s;

            s->next = p1;

      }

      else

      {

            s->next = p1;

            ready = s;

      }

}

 

 

void create()

{

PCB* p;

int i,time;

char na[10];

ready = NULL;

finish = NULL;

run = NULL;

//    cout<<"輸入進程名及其須要的運行時間:"<<endl;

//    fflush(stdin);

for(i=1;i<N+1;i++)

{

      cout<<"輸入進程名及其須要的運行時間:"<<endl;

      p = new PCB;

      cin>>na;

      cin>>time;

      strcpy(p->name,na);

      p->cputime = 0;

      p->needtime = time;

      p->state = 'W';

      p->round = 0;

      if(ready!=NULL)

            insert(p);

      else

      {

            p->next = ready;

            ready = p;

      }

//   cout<<"輸入進程名及其須要運行的時間:"<<endl;

}

prt();

run = ready;

ready = ready->next;

run->state = 'R';

}

 

void timeslicecycle()

{

while(run != NULL)

{

      run->cputime = run->cputime+8;

      run->needtime = run->needtime-8;

      run->round = run->round+8;

      if(run->needtime<=0)

      {

            run->needtime = 0;

            run->next = finish;

            finish = run;

            run->state = 'F';

            run = NULL;

            if(ready != NULL)

                 firstin();

      }

      else

      {

            run->state = 'W';

            insert(run);

            firstin();

      }

      prt();

}

}

 

void main()

{

cout<<"輸入進程的個數:";

cin>>N;

create();

timeslicecycle();

}

  1. 運行結果與運行環境

運行環境爲Visual C++ 6.0,運行結果如圖:

 

 

 

  1. 總結與評價

(1).完成本實驗的收穫

經過本次實驗,我基本理解了操做系統的進程調度時間片輪轉調度算法的具體實現過程。

(2).基本理論知識的切入點

操做系統進程調度,以及時間片輪轉調度算法;

(3).實驗過程代碼調試遇到的問題及故障排除體會

實驗過程當中許多空填的不是很確保正確,經過翻閱操做系統書本,瞭解了基本知識,完成了實驗。

相關文章
相關標籤/搜索