ラウンドロビン スケジューラを実装するために、次のコードを書きました。コードの仕組み: 'p' を押した後、プロセス名(プロセスを識別するために使用される浮動小数点数) とプロセスのバースト時間(プロセスが実行される合計時間) を入力するようユーザーに求めます。現在実行中のプロセスを常に含むジョブ キュー (つまり、常に 1 つのプロセスのみを含む) と、他のすべてのプロセスを含む準備完了キューの2 つのキューがあります。コードが複雑になりすぎないように、ここには待機キューはありません。タイマーは time(NULL) を使用して実装されます関数とマルチスレッド。ユーザーが新しいプロセスに入るたびに、それは準備完了キューに入れられ、rrscheduler が呼び出されます。
プロセスがすでに実行されていて「p」が押された場合、rrscheduler は新しいプロセスを準備完了キューに追加した後、そのプロセスを続行します (ただし、タイマーは再び開始されました)。それ以外の場合は、準備完了キューのフロント プロセスが開始されます。
returnname()-> キューの先頭にあるプロセスの名前を返す peek()-> キューの先頭にあるプロセスが完了するまでの残り時間を返す deletecell()-キューの先頭のプロセスを削除する append()-キューの最後にプロセスを追加します
キュー内の各プロセスは、その名前と残り時間によって特徴付けられます。
キューが空の場合でも、peek() は 0 を返します。これは、キューが空かどうかを確認するために使用されます。
コード:
queue<float> job(0);
queue<float> ready(0);
queue<float> waiting(0);
char c;
time_t quantum;
time_t quantrem, timerem;
pthread_t thread1=0;
/////////////////////////////////////////////////
struct arg //this is the argument passed to the timer function
{
time_t time1;
float name1;
time_t quantum1;
};
//////////////////////////////////////////////////
void* timer(void *);
void rrscheduler(queue<float> &ready, queue<float> &job)
{
if(thread1!=0) //when 'p' was pressed, another process was executing
pthread_cancel(thread1);
thread1=0;
if(job.peek()==0 && ready.peek()!=0) //if there are no processes currently running or the process' time or quantum has finished
{
cout<<"on the front end of ready queue is process "<<ready.returnname()<<endl;
float a=ready.peek();
float b=ready.returnname();
ready.deletecell(); //remove a process ffrom ready queue and put it in job queue
job.append(a,b);
cout<<"Process "<<b<<"left the ready queue and joined the job queue"<<endl;
}
arg arg1;
arg1.time1=job.peek();
arg1.name1=job.returnname();
arg1.quantum1=quantum;
pthread_create(&thread1, NULL, timer, (void*)(&arg1)); //call the timer function thread
}//rrscheduler ends
///////////////////////////////////////////////////////
void* timer(void *argum)
{
arg *arg1= (arg*)argum;
cout<<"PROCESS "<<arg1->name1<<" HAS ENTERED THE TIMER"<<endl;
time_t d, timejob;
d=arg1->quantum1 + time(NULL);
timejob=arg1->time1 + time(NULL);
while((quantrem=d-time(NULL))>0 && (timerem=timejob-time(NULL))>0)
{
//execute till either the process time or the process quantum gets finished
}
if(timerem>0)
{
cout<<"Time quantum finished for "<<arg1->name1<<endl;
job.deletecell();
cout<<"JOB DELETED"<<endl;
ready.append(timerem, arg1->name1);
}
else
{
cout<<"Process "<<arg1->name1<<" finished"<<endl;
job.deletecell();
}
rrscheduler(ready, job);
}
///////////////////////////////////////////////////////////
int main()
{
cout<<"Enter time quantum"<<endl;
cin>>quantum;
cout<<"Press p for entering a new process "<<endl;
while(1)
{
c=cin.get();
switch(c)
{
case 'p':
if(thread1!=0) //if when 'p' is pressed, another process was executing
{
float n = job.returnname();
job.deletecell();
job.append(timerem, n);
pthread_cancel(thread1);
}
thread1=0;
cout<<"Enter the process number and time"<<endl;
float a, timeini;
cin>>a >>timeini;
ready.append(timeini, a);
rrscheduler(ready, job);
break;
default:
break;
}//switch ends here
}//while finishes
}//main finishes
プロセスは、出力「PROCESS 1 HAS ENTERED THE TIMER」の後に while ループに入り、出力「Timequantum finished for 3.99296e-34」でブロックします。
セグメンテーション違反が発生するのはなぜですか? また、プロセス名が 3.99296e-34 と表示されるのはなぜですか?
キュー ヘッダー ファイルを見たい人は、ここにあります。
前もって感謝します!!