操作系统实验1——进程调度

欢迎批评指正

#include <iostream>
#include <fstream>
#include<time.h>
using namespace std;
bool valid = false;
struct PCB {
    int ID, Priority;//优先权单位1
    double  StartBlock, StartTime;//距离进入阻塞状态的时间与在阻塞状态进入就绪状态时间
    double CPUTime, ArrivalTime, AllTime;    //已用cpu时间,到达时间,与还需要的时间
    double TravelTime, AverageTTime;
    char State;//就绪、执行、阻塞、完成
    PCB* Next;
    
}pcb;
PCB* front = new PCB(); int n; 
ofstream out("Input.txt");
PCB* FCFS(int i) {
    int min = 999999; 
    PCB* tmp = front->Next; PCB* tmp2 = new PCB();
    valid = false;
    while (tmp->Next != 0) {          //找到适合条件的最大优先权的进程
        if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R' && tmp->ArrivalTime <min) {
            min = tmp->ArrivalTime;
            tmp2 = tmp;
            valid = true;

        }tmp = tmp->Next;
    }
    return tmp2;
}
PCB* SJF(int i) {
    int min = 999999; valid = false;
    PCB* tmp = front->Next; PCB* tmp2 = new PCB();
    while (tmp->Next != 0) {          //找到适合条件的最大优先权的进程
        if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R' && tmp->AllTime < min) {
            min = tmp->AllTime;
            tmp2 = tmp;
            valid = true;

        }tmp = tmp->Next;
    }
    return tmp2;
}
PCB* RR(int i) {
    int count = 0;
    int t = i % n; valid = false;
    PCB* tmp = front->Next; 
    while (tmp->Next != 0) {
        if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R') {
        count++;
        if (count == t) {
            valid = true;
            break;
        }
        }
        tmp = tmp->Next;
    }
    return tmp;
}
PCB* Priority(int i) {
    PCB* tmp = front->Next; valid = false;
    PCB* tmp2=new PCB();
    int max = 0;

    while (tmp->Next != 0) {          //找到适合条件的最大优先权的进程
    if ((tmp->ArrivalTime <= (double)i) && tmp->AllTime > 0 && tmp->State == 'R' && tmp->Priority > max) {
        max = tmp->Priority;
        tmp2 = tmp;
        valid = true;

        }tmp = tmp->Next;
    }
       return tmp2;
}

void Input() {
    int i;
   
    cout << "进程数目: " << endl;
    out<< "进程数目: " << endl;
    n = (1 + rand() % 10);
    out << n << endl;
    PCB* tmp = new PCB();
    front->Next = tmp;
    cout << "依次输入进程ID、优先权、到达时间、需要运行时间" << endl;
    out << "依次输入进程ID、优先权、到达时间、需要运行时间" << endl;
    for (i = 0; i < n; i++) {
        //cin >> tmp->ID >> tmp->Priority >> tmp->ArrivalTime >> tmp->AllTime;
        tmp->ID = i + 1;
        tmp->Priority = (rand() % 300 + 1);
        tmp->ArrivalTime = (1.0 * (rand() % 10) + 1);
        tmp->AllTime = (1.0*(rand() % 10) + 1);
        tmp->CPUTime = 0; tmp->State = 'R';
        out << " tmp->ID: " << tmp->ID << " tmp->Priority: " << tmp->Priority << " tmp->ArrivalTime: "
            << tmp->ArrivalTime << " tmp->AllTime: " << tmp->AllTime << " tmp->CPUTime: " << tmp->CPUTime << endl;
        PCB* tmp2 = tmp;
        tmp= new PCB();
        tmp2->Next = tmp;
    }
}
void Output() {
    PCB* tmp = front->Next;
    while (tmp->Next != 0) {
        cout<<"进程ID:" << tmp->ID << ' ' <<"进程状态:"<<tmp->State << ' '<<"进程优先权: " << tmp->Priority << ' ' <<"进程到达时间 "<<
            tmp->ArrivalTime << ' ' <<"进程还需执行时间(片): "<< tmp->AllTime << ' ' << "已用CPU时间:" << tmp->CPUTime << ' ' << "距离阻塞时间:" << tmp->StartBlock << ' ' << "阻塞状态到开始时间:" << tmp->StartTime << ' ' << endl;
        out<<"进程ID:" << tmp->ID << ' ' << "进程状态:" << tmp->State << ' ' << "进程优先权: " << tmp->Priority << ' ' << "进程到达时间 " <<
            tmp->ArrivalTime << ' ' << "进程还需执行时间(片): " << tmp->AllTime << ' ' << "已用CPU时间:" << tmp->CPUTime << ' ' << "距离阻塞时间:" << tmp->StartBlock << ' ' << "阻塞状态到开始时间:" << tmp->StartTime << ' ' << endl;
        cout <<"tmp->TravelTime:" << tmp->TravelTime << "tmp->AverageTTime:" << tmp->AverageTTime << endl;
        out << "tmp->TravelTime:" << tmp->TravelTime << "   tmp->AverageTTime:" << tmp->AverageTTime << endl;
        tmp = tmp->Next;
    }
}
void Cal() {
    PCB* tmp = front->Next;
    int count = 0;
    double m = 0, n = 0;
    while (tmp->Next != 0) {
        m += tmp->TravelTime;
        n += tmp->AverageTTime;
        count++;
        tmp = tmp->Next;
    }
    cout << endl << endl << "周转时间:" << m / count << endl;
    out<< endl << endl << "周转时间:" << m / count << endl;
    cout << "平均周转时间:" << n / count << endl;
    out << endl << endl << "平均周转时间:" << n / count << endl;

 }

void adjust(int n) {
    int j=1, count = 0;
    bool tag;
    PCB* tmp = front->Next;
    PCB* tmp2=new PCB();
    for (int i = 1;; i++) {         //代表时间片
        cout << "时间片:" <<i<< endl;
        out<<"时间片:" << i << endl;
        tmp2 = Priority(i);
    cout << "正在执行的进程:ID:" << tmp2->ID << endl << "进程状态" << tmp2->State << endl
        << "进程优先权 " << tmp2->Priority << endl << "进程到达时间 " <<
        tmp2->ArrivalTime << endl << "进程还需执行时间(片) " << tmp2->AllTime << endl;
    out<< "正在执行的进程:ID:" << tmp2->ID << endl << "进程状态" << tmp2->State << endl
        << "进程优先权 " << tmp2->Priority << endl << "进程到达时间 " <<
        tmp2->ArrivalTime << endl << "进程还需执行时间(片) " << tmp2->AllTime << endl;
    cout << "已用CPU时间:" << tmp2->CPUTime << endl << "距离阻塞时间" << tmp2->StartBlock << endl << "阻塞状态到开始时间" << tmp2->StartTime << endl << endl;
    out << "已用CPU时间:" << tmp2->CPUTime << endl << "距离阻塞时间" << tmp2->StartBlock << endl << "阻塞状态到开始时间" << tmp2->StartTime << endl << endl;
   
    if (valid) {                //找到将其转为执行进程,并进行修改
            
            
            tmp2->Priority -= 3;
            if (tmp2->Priority <= 0)
                tmp2->Priority = 1;

            cout << "进程是否有I/O或其他事件进入?: (有输入1,无输入0)" << endl;
            out<< "进程是否有I/O或其他事件进入?: (有输入1,无输入0)" << endl;
            tag = rand() % 2;
            out << "tag: " << tag << endl;
          

            if (tag) {
                cout << "输入距离进入阻塞时间,和阻塞进入就绪时间:" << endl;
                tmp2->StartBlock = 1.0 * rand()/RAND_MAX;
                tmp2->StartTime = 1.0*(rand() % 5) + 1;
                out << "tmp2->StartBlock" << tmp2->StartBlock << "tmp2->StartTime" << tmp2->StartTime << endl;
                tmp2->State = 'C';
                tmp2->CPUTime += tmp2->StartBlock;
                tmp2->StartTime -= (1 - tmp2->StartBlock);
                tmp->StartBlock = 0;
            }
            else {
               
                tmp2->CPUTime += 1.0; 
                tmp2->AllTime -= 1.0;
                tmp2->StartBlock = 0;
                tmp2->StartBlock = 0;
            }
            

            if (tmp2->AllTime <= 0) {
                tmp2->State = 'F';
                count++;
                tmp2->TravelTime = (double)i - tmp2->ArrivalTime;
                tmp2->AverageTTime = tmp2->TravelTime / n;
                if (count == n)return;
            }

        }
        PCB* tmp3 = new PCB();
        tmp3 = front->Next;
        while (tmp3&&tmp3!=tmp2)        //除tmp2外,其他阻塞进程均进行startime-1操作
        {
            if (tmp3->State == 'C') {
                tmp3->StartTime -= 1;
                if (tmp3->StartTime <= 0) {
                    tmp3->StartTime = 0;
                    tmp3->State = 'R';
                }
            }
            tmp3 = tmp3->Next;
        }
        Output();
    }

}

int main()
{
    srand(time(0));
    Input();
    adjust(n);
    Output();
    Cal();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值