最短ジョブ優先(SJF)スケジューリングのためのC ++プログラム(プリエンプティブ)
与えられたプロセス、それぞれのプロセスのバースト時間と量子限界。タスクは、Shortest Job First Schedulingプリエンプティブ方式を使用して、待機時間、所要時間、およびそれぞれの平均時間を見つけて印刷することです。
最初のスケジュールの最短ジョブは何ですか?
最短ジョブ優先スケジューリングは、非プリエンプティブスケジューリング規律に従うジョブまたはプロセススケジューリングアルゴリズムです。この場合、スケジューラーは、完了時間が最小の待機キューからプロセスを選択し、CPUをそのジョブまたはプロセスに割り当てます。 SJFは平均待機時間を短縮し、スループットを向上させるため、SJFの方が最適であるため、FIFOアルゴリズムよりも最短ジョブ優先の方が望ましいです。
SJFアルゴリズムは、プリエンプティブでも非プリエンプティブでもかまいません。プリエンプティブスケジューリングは、最小残余時間優先とも呼ばれます。 スケジューリング。プリエンプティブアプローチでは、すでに実行中のプロセスがあるときに新しいプロセスが発生します。新しく到着したプロセスのバーストが実行中のプロセスのバースト時間よりも短い場合、スケジューラーはより短いバースト時間でプロセスの実行をプリエンプトします。
所要時間、待機時間、完了時間はどのくらいですか?
- 完了時間 プロセスが実行を完了するのに必要な時間です
-
所要時間 プロセスの送信から完了までの時間間隔です。
所要時間=プロセスの完了–プロセスの提出
-
待機時間 ターンアラウンドタイムとバーストタイムの差です
待機時間=所要時間–バースト時間
例
プロセスP1、P2、P3、P4、およびP5が与えられ、対応するバースト時間が以下に示されます
。| プロセス | バースト時間 | 到着時間 |
|---|---|---|
| P1 | 4 | 0 |
| P2 | 2 | 1 |
| P3 | 8 | 2 |
| P4 | 1 | 3 |
| P5 | 9 | 4 |
P1の到着時間は0であるため、別のプロセスが到着するまで最初に実行されます。 1でプロセスP2に入り、P2のバースト時間がP1のバースト時間よりも短い場合、スケジューラーはプロセスP2でCPUをディスパッチします。
平均待機時間は、ガントチャートに基づいて計算されます。 P1は(0 + 4)4を待つ必要があり、P2は1を待つ必要があり、P3は7を待つ必要があり、P4は3を待つ必要があり、P5は15を待つ必要があります。 / P>
アルゴリズム
Start
Step 1-> Declare a struct Process
Declare pid, bt, art
Step 2-> In function findTurnAroundTime(Process proc[], int n, int wt[], int tat[])
Loop For i = 0 and i < n and i++
Set tat[i] = proc[i].bt + wt[i]
Step 3-> In function findWaitingTime(Process proc[], int n, int wt[])
Declare rt[n]
Loop For i = 0 and i < n and i++
Set rt[i] = proc[i].bt
Set complete = 0, t = 0, minm = INT_MAX
Set shortest = 0, finish_time
Set bool check = false
Loop While (complete != n)
Loop For j = 0 and j < n and j++
If (proc[j].art <= t) && (rt[j] < minm) && rt[j] > 0 then,
Set minm = rt[j]
Set shortest = j
Set check = true
If check == false then,
Increment t by 1
Continue
Decrement the value of rt[shortest] by 1
Set minm = rt[shortest]
If minm == 0 then,
Set minm = INT_MAX
If rt[shortest] == 0 then,
Increment complete by 1
Set check = false
Set finish_time = t + 1
Set wt[shortest] = finish_time - proc[shortest].bt -proc[shortest].art
If wt[shortest] < 0
Set wt[shortest] = 0
Increment t by 1
Step 4-> In function findavgTime(Process proc[], int n)
Declare and set wt[n], tat[n], total_wt = 0, total_tat = 0
Call findWaitingTime(proc, n, wt)
Call findTurnAroundTime(proc, n, wt, tat)
Loop For i = 0 and i < n and i++
Set total_wt = total_wt + wt[i]
Set total_tat = total_tat + tat[i]
Print proc[i].pid, proc[i].bt, wt[i], tat[i]
Print Average waiting time i.e., total_wt / n
Print Average turn around time i.e., total_tat / n
Step 5-> In function int main()
Declare and set Process proc[] = { { 1, 5, 1 }, { 2, 3, 1 }, { 3, 6, 2 }, { 4, 5, 3 } }
Set n = sizeof(proc) / sizeof(proc[0])
Call findavgTime(proc, n)
Stop 例
#include <bits/stdc++.h>
using namespace std;
//structure for every process
struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
void findTurnAroundTime(Process proc[], int n, int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
//waiting time of all process
void findWaitingTime(Process proc[], int n, int wt[]) {
int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) && (rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}
// decrementing the remaining time
rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
// If a process gets completely
// executed
if (rt[shortest] == 0) {
complete++;
check = false;
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Function to calculate average time
void findavgTime(Process proc[], int n) {
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
// Function to find waiting time of all
// processes
findWaitingTime(proc, n, wt);
// Function to find turn around time for
// all processes
findTurnAroundTime(proc, n, wt, tat);
cout << "Processes " << " Burst time " << " Waiting time " << " Turn around time\n";
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t" << proc[i].bt << "\t\t " << wt[i] << "\t\t " << tat[i] << endl;
}
cout << "\nAverage waiting time = " << (float)total_wt / (float)n; cout << "\nAverage turn around time = " << (float)total_tat / (float)n;
}
// main function
int main() {
Process proc[] = { { 1, 5, 1 }, { 2, 3, 1 }, { 3, 6, 2 }, { 4, 5, 3 } };
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
} 出力
-
QuickSort用のC++プログラム?
クイックソートは、比較を使用してソートされていないリスト(配列)をソートするソート手法です。クイックソートは、パーティション交換ソートとも呼ばれます。 等しいソート項目の相対的な順序が保持されないため、安定したソートではありません。クイックソートは配列を操作できるため、ソートを実行するために少量の追加メモリが必要です。常に最悪の場合のパーティションを選択するわけではないことを除いて、選択ソートと非常によく似ています。したがって、選択ソートのより適切な形式と見なすことができます。 QuickSortは、最も効率的な並べ替えアルゴリズムの1つであり、配列を小さい配列に分割することに基づいていま
-
最初のn個の自然数の二乗和のためのC++プログラム?
この問題では、最初のn個の自然数の2乗の合計を取得する方法を確認します。ここでは、1からnまで実行されるforループを使用しています。各ステップで、項の2乗を計算し、それを合計に追加します。このプログラムは、完了するまでにO(n)時間かかります。しかし、これをO(1)または一定時間で解きたい場合は、この級数式-を使用できます。 アルゴリズム squareNNatural(n) begin sum := 0 for i in range 1 to n, do sum := sum + i^2 &