プロセス入力機能の実装
このステップでは、スケジューリング対象のプロセスに関する情報を収集する機能を実装します。FCFS スケジューリングでは、以下の情報が必要です。
- プロセスの数
- 各プロセスのバースト時間(各プロセスが必要とする実行時間)
fcfs.cpp
ファイルを修正して、この機能を追加しましょう。
#include <iostream>
#include <iomanip> // For formatted output
using namespace std;
int main() {
cout << "FCFS Scheduling Algorithm Implementation" << endl;
cout << "----------------------------------------" << endl;
// Variables declaration
int n; // Number of processes
int burst_time[20]; // Array to store burst time of each process
// Get the number of processes from the user
cout << "\nEnter the number of processes (maximum 20): ";
cin >> n;
// Input validation
if (n <= 0 || n > 20) {
cout << "Invalid number of processes. Please enter a value between 1 and 20." << endl;
return 1;
}
// Get burst time for each process
cout << "\nEnter the Burst Time for each process:" << endl;
for (int i = 0; i < n; i++) {
cout << "Process P" << i + 1 << ": ";
cin >> burst_time[i];
// Input validation for burst time
if (burst_time[i] <= 0) {
cout << "Burst time must be a positive integer. Please restart the program." << endl;
return 1;
}
}
// Display the entered data
cout << "\nProcess\tBurst Time" << endl;
cout << "--------------------" << endl;
for (int i = 0; i < n; i++) {
cout << "P" << i + 1 << "\t" << burst_time[i] << endl;
}
return 0;
}
追加した部分を見ていきましょう。
- プロセスの数 (
n
) と各プロセスのバースト時間 (burst_time[20]
) を格納する変数。
- プロセスの数と各プロセスのバースト時間を収集するための入力プロンプト。
- プロセスの数が合理的な範囲(1 - 20)内であり、バースト時間が正であることを確認する入力検証。
- 入力されたデータを確認するためのテーブル表示。
更新したプログラムをコンパイルして実行しましょう。
g++ fcfs.cpp -o fcfs
./fcfs
プログラムを実行すると、プロセスの数とそれぞれのバースト時間を入力するように促されます。以下のデータを入力してみましょう。
3
5
9
4
これは、それぞれバースト時間が 5、9、4 時間単位の 3 つのプロセスを表しています。以下のような出力が表示されるはずです。
FCFS Scheduling Algorithm Implementation
----------------------------------------
Enter the number of processes (maximum 20): 3
Enter the Burst Time for each process:
Process P1: 5
Process P2: 9
Process P3: 4
Process Burst Time
--------------------
P1 5
P2 9
P3 4
これにより、入力機能が正しく動作していることが確認できます。次のステップでは、各プロセスの待機時間とターンアラウンド時間を計算します。