计算机操作系统调度算法——短作业优先算法简单实现

本文通过C++代码实现了一个简单的短作业优先(SJF)调度算法模拟,用于理解操作系统中的进程调度。代码中定义了PCB结构体,初始化了5个进程,然后通过排序和状态更新模拟了调度过程。
//调度算法的模拟
//1.SJF 短作业优先算法

#include<stdio.h>
#include <malloc.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;

#define N 5  //假设5个进程

struct PCB{
    string name;//进程name
	int reachTime;//标志进程到达时间
	int needTime;//进程所需的时间
	bool state;
	/*进程的状态,false表示挂起,true表示激活*/
	int alltime;//总共所用的时间,即周转时间
	bool over;   //进程的状态,true表示结束
	bool isrunning;
}P[5];  //声明5个进程
void init();
void processSchedule();
void printPCB(PCB P);

int arr[10];  //队列存放可以运行但是没有被调度的进程
int Size = 0; //队列的长度
int RunningP = -1; //当前运行进程编号

void Sort(int a[],int Size);

int main(){
    init();  //初始化这五个进程
    processSchedule();//进程调度

    cout<<"*-----------------------输出部分------------------------*"<<endl;
    cout<<"进程名称"<<"\t"<<"到达时间"<<"\t"<<"所需时间"<<"\t"<<"周转时间"<<endl;
    for(int i = 0; i < 5; i++){
        printPCB(P[i]); //打印进程信息
    }

    return 0;
}

void init(){

    cout<<"*------------------输入部分-------------------*"<<endl;
    for(int i=0;i<N;i++){
        cout<<"输入进程"<<i+1<<"名称,到达时间,所需时间(用空格键隔开):"<<endl;
        cin>>P[i].name>>P[i].reachTime>>P[i].needTime;
    }
    for(int i=0;i<N;i++){
        P[i].state=0;
        P[i].alltime=0;
        P[i].isrunning=0;
        P[i].over=0;
    }
    /*P[0].name = "P1";
    P[0].reachTime = 0;
    P[0].needTime = 4;
    P[0].state = 0;
    P[0].alltime = 0;
    P[0].over = 0;
    P[0].isrunning = 0;

    P[1].name = "P2";
    P[1].reachTime = 1;
    P[1].needTime = 3;
    P[1].state = 0;
    P[1].alltime = 0;
    P[1].over = 0;
    P[1].isrunning = 0;

    P[2].name = "P3";
    P[2].reachTime = 2;
    P[2].needTime = 5;
    P[2].state = 0;
    P[2].alltime = 0;
    P[2].over = 0;
    P[2].isrunning = 0;

    P[3].name = "P4";
    P[3].reachTime = 3;
    P[3].needTime = 2;
    P[3].state = 0;
    P[3].alltime = 0;
    P[3].over = 0;
    P[3].isrunning = 0;

    P[4].name = "P5";
    P[4].reachTime = 4;
    P[4].needTime = 4;
    P[4].state = 0;
    P[4].alltime = 0;
    P[4].over = 0;
    P[4].isrunning = 0;*/
}

void printPCB(PCB P){
    cout<<P.name<<"\t\t"<<P.reachTime<<"\t\t"<<P.needTime<<"\t\t"<<P.alltime<<endl;
}

void setState(int all){ //找到该时刻到达的进程,并将它放入队列中
    for(int i = 0; i < N; i++){
        if(P[i].reachTime == all){
            arr[Size] = i;  //放到队列的最后
            P[i].state = 1;  //标志该进程状态激活
            Size++;   //队列长度++
        }
    }
}

void Sort(int Size){  //对队列中的进程按照执行需要的时间排序,从长到短
    for(int i = 0; i < Size;i++){
        for(int j = i+1; j < Size; j++){
            if(P[arr[i]].needTime < P[arr[j]].needTime){  //短作业放最后
                int tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
        }
    }
}

void processSchedule(){
    int all = 0; //记录系统运行总时间

    for(int n = 0; n < N; n++){ //五个进程
        int time = 0;  //记录该进程执行时间
        while(true){

            setState(all);
            Sort(Size);
<span style="white-space:pre">	</span>    
            time++;
            all++;
            if(RunningP == -1){  //如果没有进程在执行
                if(Size > 0){
                    RunningP = arr[Size-1];//调入运行所需时间最短的进程,即队列中最后边的进程
                    Size--; //队列长度减一
                }
            }
            if(RunningP != -1){ //如果有进程在运行
                if(P[RunningP].needTime == time){//如果该进程已经执行完成
                    P[RunningP].alltime = all - P[RunningP].reachTime;//计算该进程周转时间
                    RunningP = -1;//重新标记没有进程在运行
                    break;//跳出while
                }
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值