进程调度 实验报告

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
cout<<" ***********进程运行前****************"<<endl;
print1(r);
p->cpu =p->cpu-1;
p->privilege =p->privilege-3;
if (p->privilege<0)
{
p->privilege =0;
}
cout<<" ***********进程运行后**********************"<<endl;
#ifndef FCFS_H
#define FCFS_H
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
#include<iomanip.h>
#define random(x) (rand()%x)
struct process
cin>>choice;
switch (choice)
{
case 1: {struct process *first=creatlist(n);print(first);fcfs(first);break;}
case 2: {struct process1 *first1=creatlist1(n);print1(first1);privilege(first1);break;}
case 3:{struct process2 *first=creatlist2(n);print2(first);round(first);break;}
case 4: {system("cls");break;}
case 5: break;
default: break;
}
}
//*/
}
先来先服务调度算法:fcfs.h
print1(r);
cout<<"/*************************************************/\n"<<endl;
if (p->cpu == 0)
{
del1(r,p);
}
else
{
t=p;
del1(r,p);
insert1(r,p);
}
b++;
}
}
#endif
{
struct process *s;
s=new process;
s->num=i;
s->attime=i;
s->servtime=random(20);
insert(first,s);
}
return first;
}
void insert(struct process *first,struct process *s)
struct process *r;
cout<<"/**************************************************/"<<endl;
cout<<"FCFS调度算法基本信息\n"<<endl;
cout<<"进程名"<<"到达时间"<<"服务时间"<<"开始执行时间"<<"完成时间"<<"周转时间"<<"带权周转时间"<<endl;
while (p)
{
finishtime =p->servtime + startime;
wasttime= finishtime-p->attime;
weighttime = wasttime / p->servtime;
cout<<p->num<<setw(8)<<p->attime<<setw(10)<<p->servtime<<setw(10)<<startime<<setw(14)<<finishtime<<setw(8)<<wasttime<<'\t'<<weighttime<<endl;
}
}
void privilege(struct process1 *first)
{
struct process1* p,*r=first,*t;
p=first->next;
int b=0;
while (first->next)
{
Байду номын сангаасr=first;
p=first->next;
cout<<"/********************"<<b<<"*****************************/"<<endl;
实验一进程调度实验报告
一、实验目的
多道程序设计中,经常是若干个进程同时处于就绪状态,必须依照某种策略来决定那个进程优先占有处理机。因而引起进程调度。本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。
二、实验内容
1.优先权法、轮转法
简化假设
1)进程为计算型的(无I/O)
2)进程状态:ready、running、finish
{
struct process1 *p,*q;
p=first;
q=first->next;
while(q!=NULL&&q->privilege>=s->privilege)
{
p=q;
q=q->next;
}
return p;
}
void del1(struct process1 *first,struct process1 *s)
process1 *next;
};
/*******************优先权调度算法所需函数声明*****************************************************/
struct process1*creatlist1(); //新建链表(就绪队列)
void insert1(struct process1 *first,struct process1 *s); //增加一个进程或将一个进程查入到队列中
5.至少三种调度算法
6.若有可能请在图形方式下,将PCB的调度用图形成动画显示。

主函数控制台Main.c
#include<stdio.h>
#include<windows.h>
#include "fcfs.h"
#include "privilege.h"
#include "round.h"
void main()
{
int num; //进程号
int attime,servtime; //进程到达时间,服务时间
process *next;
};
struct process*creatlist(int); //新建一个链表
void insert(struct process *first,struct process *s); //插入一个节点(尾插法)
3)进程需要的CPU时间以时间片为单位确定
2.算法描述
1)优先权法——动态优先权
当前运行进程用完时间片后,其优先权减去一个常数。
2)轮转法
三、流程图
四、实验要求
1. 产生的各种随机数的取值范围加以限制,如所需的CPU时间限制在1~20之间。
2.进程数n不要太大通常取4~8个
3.使用动态数据结构
4.独立编程
void print(struct process *first); //打印函数
struct process*creatlist(int n)
{
srand((int)time(0));
struct process*first=new process;
first->next=NULL;
for (int i=0;i<n;i++)
{
int n;
cout<<"输入随机产生的进程数目"<<endl;
cin>>n;
int choice=1;
while (choice!=5)
{
cout<<"请输入你的选择"<<endl;
cout<<"1:FCFS调度算法\n"<<"2:动态优先权调度算法\n"<<"3:时间片轮转法\n"<<"4:清屏\n"<<"5:退出"<<endl;
{
struct process1 *p;
p =first->next;
cout<<"进程号"<<"进程优先权"<<"进程所需CPU片数"<<endl;
while (p)
{
cout<<p->pcb<<setw(8)<<p->privilege<<setw(12)<<p->cpu<<endl;
p=p->next;
r=p;
p=p->next;
startime +=r->servtime;
}
}
#endif
动态优先权调度算法privilege.h
#ifndef PRIVILEGE_H
#define PRIVILEGE_H
struct process1
{
int pcb; //进程号PCB
int privilege,cpu; //进程优先权,所需CPU时间
}
return first;
}
void insert1(struct process1 *first,struct process1 *s) //插入节点
{
struct process1 *p=search(first,s);
s->next=p->next;
p->next=s;
//return;
}
struct process1* search(struct process1 *first,struct process1 *s) //查找第一个到达时间大于等于AT的节点,返回其前一个指针
/*******************优先权调度算法所需函数*****************************************************/
struct process1*creatlist1(int n)
{
srand((int)time(0));
struct process1 *first =new process1;
process2 *next;
};
struct process2*creatlist2(int); //新建一个链表
void print2(struct process2 *first); //打印函数
void insert2(struct process2 *first,struct process2 *s); //插入节点
first ->next=NULL;
for (int i=0;i<n;i++)
{
struct process1 *s;
s =new process1;
s->pcb =i;
s-> privilege=random(20)+5;
s->cpu=random(20)+1;
insert1(first,s );
{
struct process *r=first;
struct process *p;
while(r){p=r;r=r->next;}
p->next=s;p=s;
p->next=NULL;
}
void print(struct process *first) //打印函数
{
struct process *p;
时间片轮转调度算法round.h
#ifndef ROUND_H
#define ROUND_H
struct process2
{
int pcb; //进程号PCB
int roundpiece; //轮转时间片数
int needpiece; //进程所需时间片数
int cpupiece; //占用CPU时间片数
void del1(struct process1 *first,struct process1 *s); //删除(撤销)一个进程
void print1(struct process1 *first); //打印函数
struct process1* search(struct process1 *head,struct process1 *s);
{
struct process1 *p,*r;
p=first->next;
r=first;
int a=1;
while (a)
{
if (p==s)
{
r->next=p->next;
//free(p) ;
a=0;
}
else
{
r=p;
p=p->next;
}
}
}
void print1(struct process1 *first) //打印函数
}
}
void fcfs(struct process *first)
{
int startime=0;//开始执行时间
int finishtime=0;//完成时间
int wasttime=0;//周转时间
int weighttime=0;//带权周转时间
struct process *p=first->next;
p =first->next;
cout<<"随机产生的进程的信息如下"<<endl;
cout<<"进程名"<<"进程到达时间"<<"进程服务时间"<<endl;
while (p)
{
cout<<p->num<<'\t'<<p->attime<<setw(14)<<p->servtime<<endl;
p=p->next;
相关文档
最新文档