C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

優先スケジューリングのためのC++プログラム


n個のプロセス、つまりP1、P2、P3、.......、Pnと、各プロセスに関連付けられた対応するバースト時間と優先度が与えられます。タスクは、優先CPUスケジューリングアルゴリズムを使用して、平均待機時間、平均ターンアラウンドタイム、およびプロセス実行のシーケンスを見つけることです。

待機時間と所要時間とは何ですか?

所要時間 プロセスの送信から完了までの時間間隔です。

所要時間=プロセスの完了–プロセスの提出

待機時間 ターンアラウンドタイムとバーストタイムの差です

待機時間=所要時間–バースト時間

優先スケジューリングとは何ですか?

優先度スケジューリングでは、すべてのプロセスが0〜10の範囲の優先度に関連付けられます。ここで、整数0は最低の優先度を表し、10は最高の優先度を表します。優先順位は、内部と外部の2つの方法で定義できます。また、優先順位のスケジューリングは、プリエンプティブまたは非プリエンプティブのいずれかになります。

プリエンプティブ優先度スケジューリング 新しく到着したプロセスの優先度が実行中のプロセスの優先度よりも高い場合、スケジューラーはCPUをプリエンプトします。

非プリエンプティブ優先スケジューリングでは、 スケジューラーは、準備完了キューの先頭に新しいプロセスをキューに入れます。

優先度スケジューリングアルゴリズムを使用することのデメリット 無期限のブロッキングまたは飢餓です。優先度の高いプロセスが不足の問題につながるため、リソースを無期限に待たなければならない可能性のある優先度の低いプロセスが存在します。

4つのプロセスP1、P2、P3、P4があり、それぞれのプロセスに対応するバースト時間と優先度が関連付けられているとします。ここで、0は最低の優先度を表し、10は最高の優先度を表します。

プロセス バースト時間 優先度
P1 15 2
P2 13 0
P3 10 4
P4 11 1

複数のプロセスを実行するシーケンスは、以下のガントチャートを使用して表されます

優先スケジューリングのためのC++プログラム

アルゴリズム

Start
Step 1-> Make a structure Process with variables pid, bt, priority
Step 2-> In function bool compare(Process a, Process b)
   Return (a.priority > b.priority)
Step 3-> In function waitingtime(Process pro[], int n, int wt[])
   Set wt[0] = 0
   Loop For i = 1 and i < n and i++
      Set wt[i] = pro[i-1].bt + wt[i-1]
   End
Step 4-> In function turnarround( Process pro[], int n, int wt[], int tat[])
   Loop For i = 0 and i < n and i++
      Set tat[i] = pro[i].bt + wt[i]
   End Loop
Step 5-> In function avgtime(Process pro[], int n)
   Declare and initialize wt[n], tat[n], total_wt = 0, total_tat = 0
   Call function waitingtime(pro, n, wt)
   Call function turnarround(pro, n, wt, tat)
   Print “Processes, Burst time, Waiting time, Turn around time"
   Loop For i=0 and i<n and i++
      Set total_wt = total_wt + wt[i]
      total_tat = total_tat + tat[i]
   End Loop
   Print values of “Processes, Burst time, Waiting time, Turn around time"
   Print Average waiting time, Average turn around time
Step 6-> In function scheduling(Process pro[], int n)
   Call function sort(pro, pro + n, compare)
   Loop For i = 0 and i < n and i++
      Print the order.
   End Loop
   Call function avgtime(pro, n)
Step 7-> In function int main()
   Declare and initialize Process pro[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}}
   Declare and initialize n = sizeof pro / sizeof pro[0]
   Call function scheduling(pro, n)
Stop

#include<bits/stdc++.h>
using namespace std;
struct Process {
   int pid; // Process ID
   int bt; // CPU Burst time required
   int priority; // Priority of this process
};
// sorting the Process acc. to the priority
bool compare(Process a, Process b) {
   return (a.priority > b.priority);
}
void waitingtime(Process pro[], int n, int wt[]) {
   // Initial waiting time for a process is 0
   wt[0] = 0;
   // calculating waiting time
   for (int i = 1; i < n ; i++ )
      wt[i] = pro[i-1].bt + wt[i-1] ;
}
 // Function to calculate turn around time
void turnarround( Process pro[], int n, int wt[], int tat[]) {
   // calculating turnaround time by adding
   // bt[i] + wt[i]
   for (int i = 0; i < n ; i++)
      tat[i] = pro[i].bt + wt[i];
}
//Function to calculate average time
void avgtime(Process pro[], int n) {
   int wt[n], tat[n], total_wt = 0, total_tat = 0;
   //Function to find waiting time of all processes
   waitingtime(pro, n, wt);
   //Function to find turn around time for all processes
   turnarround(pro, n, wt, tat);
   //Display processes along with all details
   cout << "\nProcesses "<< " Burst time " << " Waiting time " << " Turn around time\n";
   // Calculate total waiting time and total turn
   // around time
   for (int i=0; i<n; i++) {
      total_wt = total_wt + wt[i];
      total_tat = total_tat + tat[i];
      cout << " " << pro[i].pid << "\t\t" << pro[i].bt << "\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;
}
void scheduling(Process pro[], int n) {
   // Sort processes by priority
   sort(pro, pro + n, compare);
   cout<< "Order in which processes gets executed \n";
   for (int i = 0 ; i < n; i++)
      cout << pro[i].pid <<" " ;
   avgtime(pro, n);
}
// main function
int main() {
   Process pro[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
   int n = sizeof pro / sizeof pro[0];
   scheduling(pro, n);
   return 0;
}

出力

Order in which processes gets executed
1 3 2
Processes  Burst time  Waiting time  Turn around time
 1              10         0              10
 3              8          10             18
 2              5          18             23
 
Average waiting time = 9.33333
Average turn around time = 17

  1. C++でのピラミッドのボリュームのプログラム

    ピラミッドのベースのタイプに応じて側面が与えられると、タスクはピラミッドの体積を計算することです。 ピラミッドは、ピラミッドの鋭いエッジを形成する共通点で外面が三角形で交わる3D図形です。ピラミッドの体積は、持つベースのタイプによって異なります。 -のように、ピラミッドを構成できるベースにはさまざまな種類があります。 三角形 -ピラミッドの体積よりも、ピラミッドの底辺が三角形になることを意味します 式-:( 1/6)* a * b * h 正方形 -ピラミッドの体積よりも、ピラミッドの底面が正方形になることを意味します 式-:(1/3)*(b ^ 2)* h 五角形 -ピラミッド

  2. QuickSort用のC++プログラム?

    クイックソートは、比較を使用してソートされていないリスト(配列)をソートするソート手法です。クイックソートは、パーティション交換ソートとも呼ばれます。 等しいソート項目の相対的な順序が保持されないため、安定したソートではありません。クイックソートは配列を操作できるため、ソートを実行するために少量の追加メモリが必要です。常に最悪の場合のパーティションを選択するわけではないことを除いて、選択ソートと非常によく似ています。したがって、選択ソートのより適切な形式と見なすことができます。 QuickSortは、最も効率的な並べ替えアルゴリズムの1つであり、配列を小さい配列に分割することに基づいていま