操做系统实验报告(时间片轮调度算法)

操做系统实验报告

  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).实验过程代码调试遇到的问题及故障排除体会

实验过程当中许多空填的不是很确保正确,经过翻阅操做系统书本,了解了基本知识,完成了实验。

相关文章
相关标签/搜索