オペレーティングシステムの実験——プロセススケジューリング(2)


じっけんタスク


異なるスケジューリングアルゴリズムに基づいてオペレーティングシステムのプロセスのスケジューリングをシミュレートするプログラムを設計します.スケジューリングアルゴリズム:タイムスライス循環法1、プロセス制御ブロックPBC表構造を設計し、サイクルタイムスライス回転アルゴリズムを適用する.2、PBC構造は、通常、プロセス名、プロセス優先数、ローテーションタイムスライス、プロセスのCPU時間、プロセス状態などの情報を含む.スケジューリングアルゴリズムによっては、PCB構造を適切に調整することができる.3、プロセスキューを作成する.異なるアルゴリズムに対して異なるチェーンプログラムを作成する.プログラム要求の実行効果:プロセス数、スケジューリングアルゴリズムを設定した後、システムは設定したパラメータで実行することができ、画面に準備キューと完了キューのプロセス名などの情報を交互に表示することができる.
Windows環境でのコード:
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int n;
class PCB
{
    public:
        int runtime;
        string name;
        string state;
        int needtime;
        int round;
        int Counter;
        PCB * next;
};
PCB * run = NULL;
PCB * ready = NULL;
PCB * finish = NULL;
PCB * tial = ready;
void Dtime(int t);
void Prinft()
{
    cout<<"    "<<"\t"<<"     "<<"\t"<<"     "<<"\t"<<"   "<<"\t"<<"   "<<"\t"<<"  "<<endl;
}
void Prinft(PCB * p)
{
    cout<<p->name<<"\t\t\t"<<p->runtime<<"\t\t"<<p->needtime<<" \t"<<p->Counter<<"\t"<<p->round<<"\t"<<p->state<<endl;
}
void display()
{
    PCB *p;
    if(run != NULL)
        Prinft(run);
    Dtime(1);
    p = ready;
    while(p != NULL)
    {
        Prinft(p);
        p = p->next;
    }
    Dtime(1);
    p = finish;
    while(p != NULL)
    {
        Prinft(p);
        p = p->next;
    }
    cout<<"--------------------------------------------------------------
"
; } void queue(PCB *p) { if(ready == NULL) { p->next = NULL; ready = p; tial = p; } else { tial->next = p; tial = p; p->next = NULL; } } bool CTProcessOfRuntime() { PCB * Node; int m; cout <<" :"<<endl; cin >>n; cout <<" :"<<endl; cin >>m; for(int j = 0;j < n; j++) { Node = new PCB; if(Node==NULL) return false; else { cout <<" , CPU :"<<endl; cin >>Node->name>>Node->needtime; Node->runtime = 0; Node->state =" "; Node->Counter = 0; Node->round = m; // cout <name< } queue(Node); } return true; } void Runtime() { run = ready; ready = ready->next; run->state = " "; Prinft(); while(run!=NULL) { run->runtime = run->runtime+1; run->needtime = run->needtime-1; run->Counter = run->Counter + 1; if(run->needtime == 0) { run->state = " "; run->next = finish; finish = run; run = NULL; if(ready!=NULL) { run = ready; run->state = " "; ready = ready->next; } } else if(run->Counter == run->round) { run->Counter = 0; run->state = " "; queue(run); run=NULL; if(ready!=NULL) { run = ready; run->state = " "; ready = ready->next; } } display(); } } int main() { CTProcessOfRuntime(); Runtime(); return 0; } void Dtime(int t) { time_t current_time; time_t start_time; time(&start_time); do { time(& current_time); }while((current_time-start_time)<t); }