操作系统实验 进程调度算法

**

进程调度算法

**

1. 实验要求

  • 编写并调试一个模拟的进程调度程序
  • 基础实验:要求实现动态优先数调度
  • 加分实验:要求将动态优先数调度修改成静态优先数调度
    (只需要排一次序,rtime不需要)

2.算法描述

  1. “最高优先数优先”调度算法的基本思想是把CPU分配给就绪队列中优先数最高的进程。 每个进程有一个进程控制块( PCB)表示。

  2. 进程控制块可以包含如下信息:进程名、优先数、到达时间、需要运行时间、已用CPU时间、进程状态等等。进程的优先数及需要的运行时间可以事先人为地指定(也可以由随机数产生)。进程的到达时间为进程输入的时间。

  3. 静态优先数是在创建进程时确定的,并在整个进程运行期间不再改变。 (只需要排一次序,rtime错误)

  4. 动态优先数是指进程的优先数(0-7,0-255)在创建进程时可以给定一个初始值,并且可以按一定原则修改优先数。例如:在进程获得一次CPU后就将其优先数减少1

  5. 进程等待的时间超过某一时限时增加其优先数的值,等等。动态优先数调度算法流程图如下:

进程调度

3.实验源程序代码

动态优先数:
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0

//进程调度源程序如下: ntime  ==   rtime 结束


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=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);
}
void 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");
}
void check() /* 建立进程查看函数 */
{
    PCB* pr;
    printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/
    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函数*/
    }
}
int 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();

    return 0;
}

运行截图:
操作系统实验2-1

操作系统实验2-2
操作系统实验2-3

赋值注意事项:
1、进程个数>=3
2、进程所需要运行时间ntime<=5
3、ntime == rtime进程结束
静态优先数:

在动态优先数上进行改变即可:只需要排一次序,删除rtime

#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=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); 
} 

void 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"); 
}

void check() /* 建立进程查看函数 */ 
{ 
	PCB* pr; 
	printf("\n **** 当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ 
	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函数*/ 
//} 
//} 
int 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(); 
	
	return 0;
}

  • 14
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值