操作系统实验报告之处理机管理

时间:2024.4.29

试验一:

时间:20##-11-29

计算机实验机房2

朱蓉蓉

第一题:先来先服务算法

#include<iostream>

#include<string>

#include<iomanip>

using namespace std;

struct PCB

{

       string name;//进程名

       float ta;//进程到达时间

       float ts;//进程估计运行的时间

       float tb;//进程开始运行时间

       float tm;//进程仍需运行的时间

       float to;//进程完成的时间

       float rn;//进程运行的次数

       float totalTime;//周转时间

       double weightTotalTime;//带权周转时间(周转时间/估计运行时间)

       PCB *next;//定义指向下一个进程的指针

};

#define MAX_NUM 15

int pronum;//定义进程数为pronum

float total;//记录所有进程的总时间

double weight;//记录所有进程的带权周转时间

PCB *create(PCB *head);//创建进程队列

void deal(PCB *head);//FCFS记录处理

void sort(PCB *head);//将进程按到达的先后顺序排列

void fcfsrun(PCB *head);//先来先服务算法

PCB *create(PCB *head)

{

       PCB *p1,*p2;

       p1=p2=new PCB;

       head=p1;

       cout<<"请输入进程数:";

       cin>>pronum;

       for(int i=0;i<pronum;i++)

       {    

              p2=p1;

              p1=new PCB;

              p1->next=NULL;

              cout<<"请依次输入第"<<i+1<<"个进程的信息(进程名、到达时间、估计运行时间):";

              cin>>p1->name>>p1->ta>>p1->ts;

              p1->tm=p1->ts;

              p1->rn=1;

           p2->next=p1;

       }

       return head;

}

void sort(PCB *head)//将进程按到达的先后顺序排列

{

       PCB *p,*q,*r,*s;

       if(head->next!=NULL)

       {

              p=head->next->next;

              head->next->next=NULL;

       }

       while(p)

       {

              q=p;

              p=p->next;

              r=head;

              s=head->next;

              while(s&&s->ta<=q->ta)

              {

                     r=s;

                     s=s->next;

              }

              r->next=q;

              q->next=s;

       }

}

void deal(PCB *head)//FCFS记录处理

{

       sort(head);

       PCB *p,*q;

      

       q=head->next;

       q->tb=q->ta;

       q->to=q->tb+q->ts;

       q->totalTime=q->to-q->ta;

    q->weightTotalTime=q->totalTime/(double)q->ts;

       total+=q->totalTime;

    weight+=q->weightTotalTime;

       p=q->next;

    while(p!=NULL)

       {

             

       p->tb=q->to;

       p->to=p->tb+p->ts;

       p->totalTime=p->to-p->ta;

    p->weightTotalTime=p->totalTime/(double)p->ts;

       total+=p->totalTime;

    weight+=p->weightTotalTime;

       q=p;

       p=p->next;

       }

}

void fcfsrun(PCB *head)//先来先服务算法

{

       deal(head);

       PCB *p,*q,*s;

       p=head->next;

       cout<<"进程执行顺序为:";

       while(p!=NULL)

       {

              cout<<"--"<<p->name;

              p=p->next;

       }

       cout<<endl;

       cout<<"进程名 提交时间  开始时间  结束时间  周转时间  带权周转时间\n";

    s=head->next;

       while(s!=NULL)

       {

       cout<<setw(4)<<s->name<<setw(7)<<s->ta<<setw(10)<<s->tb<<setw(11)<<s->to<<setw(10)<<s->totalTime<<setw(10)<<s->weightTotalTime<<endl;

              s=s->next;

       }

       cout<<endl;

    cout<<"    平均周转时间:"<<total/(double)pronum<<endl;

       cout<<"平均带权周转时间:"<<weight/(double)pronum<<endl;

       cout<<"******************************************************"<<endl;

       total=0;

       weight=0;

}

void main()

{

       cout<<"*E01114336-朱蓉蓉-先来先服务调度算法*"<<endl;

       PCB *head=NULL;

     head=create(head);

     fcfsrun(head);

}

第二题:

//优先级调度算法

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#define getpch(type) (type*)malloc(sizeof(type))

#define NULL 0

struct pcb { /* 定义进程控制块PCB */

 char name[10];

 char state;

 int super;

 int ntime;

 int rtime;

 struct pcb* link;

}*ready=NULL,*p;

typedef struct pcb PCB;

void sort() /* 建立对进程进行优先级排列函数*/

{

 PCB *first, *second;

 int insert=0;

 if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/

 {

  p->link=ready;

  ready=p;

 }

 else /* 进程比较优先级,插入适当的位置中*/

 {

  first=ready;

  second=first->link;

  while(second!=NULL)

  {

   if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/

   { /*插入到当前进程前面*/

    p->link=second;

    first->link=p;

    second=NULL;

    insert=1;

   }

   else /* 插入进程优先数最低,则插入到队尾*/

   {

    first=first->link;

    second=second->link;

   }

  }

  if(insert==0) first->link=p;

 }

}

void input() /* 建立进程控制块函数*/

{

 int i,num;

 system("cls"); /*清屏*/

 printf("\n 请输入进程数: ");

 scanf("%d",&num);

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

 {

  printf("\n 进程号No.%d:\n",i);

  p=getpch(PCB);

  printf("\n 输入进程名:");

  scanf("%s",p->name);

  printf("\n 输入进程优先数:");

  scanf("%d",&p->super);

  printf("\n 输入进程运行时间:");

  scanf("%d",&p->ntime);

  printf("\n");

  p->rtime=0;p->state='W';

  p->link=NULL;

  sort(); /* 调用sort函数*/

 }

}

int space()

{

 int l=0;

 PCB* pr=ready;

 while(pr!=NULL)

 {

  l++;

  pr=pr->link;

 }

 return(l);

}

void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/

{

 printf("\n 进程名\t 状态\t 优先数\t 需要运行时间\t 已经运行时间\n");

 printf("|%s\t",pr->name);

 printf("|%c\t",pr->state);

 printf("|%d\t",pr->super);

 printf("|%d\t\t",pr->ntime);

 printf("|%d\t",pr->rtime);

 printf("\n");

}

void check() /* 建立进程查看函数 */

{

 PCB* pr;

 printf("\n **** 当前正在运行的进程是:\n"); /*显示当前运行进程*/

 disp(p);

 pr=ready;

 printf("\n **** 当前就绪队列状态为:\n"); /*显示就绪队列状态*/

 while(pr!=NULL)

 {

  disp(pr);

  pr=pr->link;

 }

}

void destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/

{

 printf("\n 进程 [%s] 已完成.\n",p->name);

 free(p);

}

void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/

{

 (p->rtime)++;

 if(p->rtime==p->ntime)

  destroy(); /* 调用destroy函数*/

 else

 {

  (p->super)--;

  p->state='W';

  sort(); /*调用sort函数*/

 }

}

void main() /*主函数*/

{

 int len,h=0;

 char ch;

 input();

 len=space();

 while((len!=0)&&(ready!=NULL))

 {

  ch=getchar();

  h++;

  printf("-----------------------------------------------------");

  printf("\n 现在是第%d次运行: \n",h);

  p=ready;

  ready=p->link;

  p->link=NULL;

  p->state='R';

  check();

  running();

  printf("\n 按任意键继续......\n");

 }

 printf("\n\n 进程已经完成.\n");

}

第三题:

按时间片轮法调度

#define    N     10

#include<stdio.h>

#include<conio.h>

typedef    struct      pcb

{

       char pname[N];

       int   runtime;

       int   arrivetime;

       char state;

       struct  pcb*next;

}PCB;

PCB head_input;

PCB head_run;

PCB *pcb_input;

static       char R='r',C='c';

unsigned long current;

void inputprocess();

int readydata();

int runprocess();

int readyprocess();

FILE * f;

/*检查就绪队列并准备运行进程的函数*/

int   readyprocess()

{

       while(1)

       {

              if(readydata()==0)

                     return 1;

              else

                     runprocess();

       }

}

/*判断就绪队列是否为空的函数*/

int readydata()

{

       if(head_input.next==NULL)

       {

              if(head_run.next==NULL)

                     return 0;

              else

                     return 1;

       }

       PCB *p1,*p2,*p3;

       p1=head_run.next;

       p2=&head_run;

       while(p1!=NULL)

       {

              p2=p1;

              p1=p2->next;

       }

       p1=p2;

       p3=head_input.next;

       p2=&head_input;

       while(p3!=NULL)

       {

              if(((unsigned long)p3->arrivetime<=current)&&(p3->state==R))

              {

                     printf("Time is %4d ; Process %s start,\n",\

                            (current+500)/1000,p3->pname);

                     fprintf(f,"Time is %4d ; Process %s start,\n",\

                            (current+500)/1000,p3->pname);

                     p2->next=p3->next;

                     p3->next=p1->next;

                     p1->next=p3;

                     p3=p2;

              }

              p2=p3;

              p3=p3->next;

       }

       return 1;

}

int runprocess()

{

       PCB *p1,*p2;

       if(head_run.next==NULL)

       {

              current++;

              return 1;

       }

       else

       {

              p1=head_run.next;

              p2=&head_run;

              while(p1!=NULL)

              {

                     p1->runtime--;

                     current++;

                     if(p1->runtime<=0)

                     {

                            printf("Time is %4d;Process %s end.\n",(current+500)/1000,p1->pname);

                            fprintf(f,"Time is %4d;Process %s end.\n",(current+500)/1000,p1->pname);

                            p1->state=C;

                            p2->next=p1->next;

                            delete p1;

                            p1=NULL;

                     }

                     else

                     {

                            p2=p1;

                            p1=p2->next;

                     }

              }

              return 1;

       }

}

void inputprocess()

{

       PCB *p1,*p2;

       int num;

       printf("How many processes do you want to run:");

       fprintf(f,"How many processes do you want to run:");

       scanf("%d",&num);

       fprintf(f,"%d\n",&num);

       p1=&head_input;

       p2=p1;

       p1->next=new PCB;

       p1=p1->next;

       for(int i=0;i<num;i++)

       {

              printf("Input NO.%3d process pname:",i+1);

              fprintf(f,"Input NO.%3d process pname:",i+1);

              scanf("%s",p1->pname);

              fprintf(f,"%s\n",p1->pname);

              printf("          runtime:");

              fprintf(f,"              runtime:");

              scanf("%d",&(p1->runtime));

              fprintf(f,"%d\n",&(p1->runtime));

              printf("          arrivetime:");

              fprintf(f,"             arrivetime:");

              scanf("%d",&(p1->arrivetime));

              fprintf(f,"%d\n",&(p1->arrivetime));

              p1->runtime=(p1->runtime)*1000;

              p1->arrivetime=(p1->arrivetime)*1000;

              p1->state=R;

              p1->next=new PCB;

              p2=p1;

              p1=p1->next;

       }

       delete p1;

       p1=NULL;

       p2->next=NULL;

}

/* 主函数  */

void main()

{

       f=fopen("result.txt","w");

       printf("\n time l=1000 time slice\n");

       fprintf(f,"\n time l=1000 time slice\n");

       current=0;

       inputprocess();

       readyprocess();

       getch();

       fclose(f);

}

更多相关推荐:
操作系统 内存管理实验报告

同组同学学号同组同学姓名注实验内容及步骤项目的内容如果较多可以加附页

《操作系统》存储管理实验报告

《操作系统》存储管理实验报告____大学____学院实验报告

操作系统内存管理实验报告

实验报告12345678910111213

操作系统“内存管理”实验报告

洛阳理工学院实验报告1828384858687888

操作系统存储管理实验报告

北京邮电大学操作系统实验实验报告实验日期20xx1220实验名称存储管理一实验目的2二实验内容2三实验分析2对于伙伴算法2对于虚拟存储区和内存工作区的不同算法3四编程实现3伙伴算法3原理3伙伴的概念3内存的释放...

西安邮电大学操作系统内存管理实验报告含源码

西安邮电大学计算机学院课内实验报告实验名称内存管理专业名称班级1201班学生姓名学号8指导教师实验日期20xx年11月25日一实验目的及实验环境一实验环境1硬件1主机PentiumIII以上2内存128MB以上...

北理工操作系统内存管理实验报告

班级学号实验三内存管理姓名一实验目的1通过编写和调试存储管理的模拟程序以加深对存储管理方案的理解2熟悉虚存管理的页面淘汰算法3通过编写和调试地址转换过程的模拟程序以加强对地址转换过程的了解二实验要求1设计一个请...

计算机操作系统实验报告

计算机操作系统实验报告,内容附图。

计算机操作系统实验课实验报告

实验报告实验课程计算机操作系统学生姓名XXX学号专业班级软件20xx年12月25日目录3实验一熟悉WindowsXP中的进程和线程实验二进程调度7实验三实验四死锁避免银行家算法的实现16存储管理22实验一熟悉W...

东华大学操作系统存储管理实验报告

东华大学计算机学院操作系统实验报告实验名称存储管理问题姓名姜元杰学号111310228班级计算机1102指导老师李继云报告日期20xx112操作系统实验报告一实验概述1实验目标存储管理的主要功能之一是合理地分配...

兰州大学操作系统实验八存储管理模拟题目和答案,实验报告

实验报告实验八实验名称存储管理模拟实验目的1掌握请求分页存储管理系统的基本原理2实现一个模拟的虚拟分页存储管理系统实验要求编写一个程序模拟一个虚拟分页存储管理系统其中由系统随机产生进程进程大小进程到达次序时间进...

操作系统实验报告

操作系统实验报告,内容附图。

操作系统内存管理实验报告(40篇)