实验一 进程调度
任务一
一、实验名称
进程调度-代码阅读并调试实验
二、实验目的
jingchendiaodu.cpp #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; sort() /* 建立对进程进行优先级排列函数*/ {
PCB *first, *second; int insert=0; if((ready==NULL)||((p->super)>(ready->super))) /*1 就绪队列为空或p的优先级大于就绪队列中第一个的优先级 */ {
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 /* 插入进程优先数最低,则插入到队尾*/ {
2 first=first->link ; second=second->link; } } if(insert==0) first->link=p; } } input() /* 建立进程控制块函数*/ {
int i,num; clrscr(); /*清屏*/ printf("\n 请输入进程号?"); scanf("%d",&num); for(i=0;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); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ {
printf("\n qname \t state \t super \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ {
PCB* pr; printf("\n 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("\n 当前就绪队列状态为:\n"); /*显示就绪队列状态*/ while(pr!=NULL) {
disp(pr); pr=pr->link; } } destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ {
printf("\n 进程 [%s] 已完成.\n",p->name); free(p); } running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ {
(p->rtime)++; if(p->rtime==p->ntime) destroy(); /* 调用destroy函数*/ else {
(p->super)--; p->state='w'; sort(); /*调用sort函数*/ } } main() /*主函数*/ {
int len,h=0; //定义两个int类型的变量 char ch; //定义一个字符型变量 input(); //调用进程控制块函数,录入进程信息并排序 len=space(); //用len表示进程块数 while((len!=0)&&(ready!=NULL)) //当进程没有全部运行完时 {
ch=getchar(); //读取按任意键继续的“任意键” h++; //执行次数加一 printf("\n The execute number:%d \n",h); //打印当前执行次数 p=ready; //p指向第一个就绪进程块 ready=p->link; //就绪指针指向p的下一个进程块 p->link=NULL; //将p的下一个进程块置为空 p->state='R'; //状态置为正在运行 check();//调用进程查看函数,打印进程调度信息 running(); //运行进程p printf("\n 按任一键继续......"); //提示用户按任意键继续 ch=getchar(); //存储“任意键”字符 } printf("\n\n 进程已经完成.\n"); //显示进程全部调度完成 ch=getchar();//存储“任意键”字符 }
三、实验过程

已在上文作业调度源程序呈现
四、实验总结
1、先来先服务算法(FCFS)按照进程变为就绪状态的先后次序分派CPU,比较有利于长作业,而不利于短作业(因为长作业能够长时间占据处理机);有利于CPU繁忙的作业,而不利于I/O繁忙的作业。
任务二
一、实验名称
进程调度-代码阅读并调试实验
二、实验目的
三、实验过程
1、“最高优先数优先”调度算法
进程调度结果如下:



2、“轮转法”调度算法
输入进程信息如下:
四、实验总结
五、代码
1、“最高优先数优先”调度算法
#include "stdio.h" #include <stdlib.h> #include <conio.h> #define getpch(type) (type*)malloc(sizeof(type)) #define NULL 0 #include<iostream> using namespace std; struct pcb /* 定义进程控制块PCB */ {
char name[10]; char state; int super; int ntime; int rtime; struct pcb* link; }*ready=NULL,*p; typedef struct pcb PCB; sort() /* 建立对进程进行优先级排列函数*/ {
PCB *first, *second; int insert=0; if((ready==NULL)||((p->super)>(ready->super))) /*1就绪队列为空或p的优先级大于就绪队列中第一个的优先级*/ {
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; } } input() /* 建立进程控制块函数 */ {
int i; for(i=0; i<5; 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); } disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ {
printf("\n qname \t state \t super \t ndtime \t runtime \n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); } check() /* 建立进程查看函数 */ {
PCB* pr; printf("\n 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ disp(p); pr=ready; printf("\n 当前就绪队列状态为:\n"); /*显示就绪队列状态*/ while(pr!=NULL) {
disp(pr); pr=pr->link; } } destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/ {
printf("\n 进程 [%s] 已完成.\n",p->name); free(p); } running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ {
(p->rtime)++; if(p->rtime==p->ntime) destroy(); /* 调用destroy函数*/ else {
(p->super)--; p->state='w'; sort(); /*调用sort函数*/ } } main() /*主函数*/ {
int len,h=0; char ch; input(); len=space(); while((len!=0)&&(ready!=NULL)) {
ch=getchar(); h++; printf("\n The execute number:%d \n",h); p=ready; ready=p->link; p->link=NULL; p->state='R'; check(); running(); printf("\n 按任一键继续......"); ch=getchar(); } printf("\n\n 进程已经完成.\n"); ch=getchar(); }
2、“轮转法”调度算法
#include<stdio.h> #include<malloc.h> enum process_status{
READY , RUN , FINISH}; typedef struct pcb {
char process_tag[20] ; struct pcb *next ; int time_slice ; // 轮转时间片 int take_cpu_time ; //占用CPU时间片数 int process_time ; //进程所需时间片数 process_status status ; } PCB ; typedef struct {
PCB *run ; //当前运行的进程指针 PCB *ready ; //当前准备队列的头指针 PCB *tail ; //准备队列的队尾指针 PCB *finish ; //完成队列的指针 } PCBC ; void init_pcbc(PCBC *p) {
p->run = NULL ; p->ready = NULL ; p->tail = NULL ; p->finish = NULL ; } void input_process(PCBC *pcbc) {
PCB *pcb ; pcb = (PCB*)malloc(sizeof(PCB)) ; printf("请输入进程标识符:") ; scanf("%s" , &pcb->process_tag) ; printf("输入格式为: (优先级,占用CPU时间片数,进程所需时间片数) : ") ; scanf("%d%d%d" , &pcb->time_slice , &pcb->take_cpu_time , &pcb->process_time) ; pcb->status = READY ; //初始化就绪状态 //当就绪队列为空时 if(pcbc->ready == NULL && pcbc->tail == NULL) {
pcbc->ready = pcbc->tail = pcb ; pcb->next = NULL ; } else {
pcb->next = pcbc->tail->next ; pcbc->tail->next = pcb ; pcbc->tail = pcb ; } } void print_log(PCBC *pcbc) {
PCB *ready , *finish ; ready = pcbc->ready ; finish = pcbc->finish ; printf("-------------------------------------------------- \n") ; printf("Run: \n") ; if(pcbc->run != NULL) {
printf("%s %04d %04d %04d \n" , pcbc->run->process_tag ,pcbc->run->time_slice , pcbc->run->take_cpu_time , pcbc->run->process_time) ; } else {
printf("Run is empty! \n") ; } printf("Ready:\n") ; while(ready != NULL) {
printf("%s %04d %04d %04d \n" , ready->process_tag ,ready->time_slice , ready->take_cpu_time , ready->process_time) ; ready = ready->next ; } printf("Finish:\n") ; while(finish != NULL) {
printf("%s %04d %04d %04d \n" , finish->process_tag ,finish->time_slice , finish->take_cpu_time , finish->process_time) ; finish = finish->next ; } } void run_pcbc_timeSlice(PCBC *xpcbc) {
PCBC *pcbc = xpcbc ; PCB *temp , *pre , *tail ; while(pcbc->ready != NULL) {
pcbc->run = pcbc->ready ; pcbc->ready = pcbc->ready->next ; //改变队首元素 print_log(pcbc) ; pcbc->run->take_cpu_time += 1 ; pcbc->run->process_time -= 1 ; if(pcbc->run->process_time == 0) {
if(pcbc->finish == NULL) {
pcbc->finish = pcbc->run ; pcbc->finish->next = NULL ; tail = pcbc->finish ; } else {
tail->next = pcbc->run ; tail = tail->next ; tail->next = NULL ; } } else {
if(pcbc->run->time_slice == pcbc->run->take_cpu_time) //占用CPU时间片数到 {
pcbc->run->take_cpu_time = 0 ; pcbc->run->next = NULL ; pcbc->tail->next = pcbc->run ; pcbc->tail = pcbc->run ; if(pcbc->ready == NULL) {
pcbc->ready = pcbc->tail ; pcbc->ready->next = NULL ; } } else {
pcbc->run->next = pcbc->ready ; pcbc->ready = pcbc->run ; } } } pcbc->run = NULL ; print_log(pcbc) ; } int main() {
PCBC *pcbc ; //创建进程控制块链 ; pcbc = (PCBC*)malloc(sizeof(PCBC)) ; init_pcbc(pcbc) ; //初始化进程控制块链 for(int i = 0 ; i < 5 ; i++) {
input_process(pcbc) ; } printf("------------------------\n") ; run_pcbc_timeSlice(pcbc) ; return 0 ; }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221529.html原文链接:https://javaforall.net
