Advertisement
CHU2

FCFS & SJF

Dec 4th, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Process {
  6.     int pid;
  7.     int arrival_time;
  8.     int burst_time;
  9.     int start_time;
  10.     int finish_time;
  11.     int turnaround_time;
  12.     int waiting_time;
  13. };
  14.  
  15. void fcfs(Process [], int);
  16. void sjf(Process [], int);
  17. void printTable(Process [], int);
  18.  
  19. int main() {
  20.     int n;
  21.     cout << "Enter the number of processes: ";
  22.     cin >> n;
  23.  
  24.     Process proc[n];
  25.     for (int i = 0; i < n; i++) {
  26.         cout << "Enter arrival time and burst time for process " << i + 1 << ": ";
  27.         cin >> proc[i].arrival_time >> proc[i].burst_time;
  28.         proc[i].pid = i + 1;
  29.     }
  30.  
  31.     cout << "\nFCFS Scheduling:\n";
  32.     fcfs(proc, n);
  33.     printTable(proc, n);
  34.  
  35.     cout << "\nSJF Scheduling:\n";
  36.     sjf(proc, n);
  37.     printTable(proc, n);
  38.  
  39.     return 0;
  40. }
  41.  
  42. void fcfs(Process proc[], int n) {
  43.     int time = 0;
  44.     for (int i = 0; i < n; i++) {
  45.         if (time < proc[i].arrival_time) {
  46.             time = proc[i].arrival_time;
  47.         }
  48.         proc[i].start_time = time;
  49.         time += proc[i].burst_time;
  50.         proc[i].finish_time = time;
  51.         proc[i].turnaround_time = proc[i].finish_time - proc[i].arrival_time;
  52.         proc[i].waiting_time = proc[i].turnaround_time - proc[i].burst_time;
  53.     }
  54. }
  55.  
  56. void sjf(Process proc[], int n) {
  57.     int time = 0, completed = 0;
  58.     while (completed != n) {
  59.         int shortest = INT_MAX, index = -1;
  60.         for (int i = 0; i < n; i++) {
  61.             if (proc[i].arrival_time <= time && proc[i].burst_time >= 0 && proc[i].burst_time < shortest) {
  62.                 shortest = proc[i].burst_time;
  63.                 index = i;
  64.             }
  65.         }
  66.  
  67.         if (index != -1) {
  68.             proc[index].start_time = time;
  69.             time += proc[index].burst_time;
  70.             proc[index].finish_time = time;
  71.             proc[index].turnaround_time = proc[index].finish_time - proc[index].arrival_time;
  72.             proc[index].waiting_time = proc[index].turnaround_time - proc[index].burst_time;
  73.             proc[index].burst_time = 0;
  74.             completed++;
  75.         } else {
  76.             time++;
  77.         }
  78.     }
  79. }
  80.  
  81. void printTable(Process proc[], int n) {
  82.     cout << "Process ID\tArrival Time\tBurst Time\tStart Time\tFinish Time\tTurnaround Time\tWaiting Time\n";
  83.     for (int i = 0; i < n; i++) {
  84.         cout << proc[i].pid << "\t\t" << proc[i].arrival_time << "\t\t" << proc[i].burst_time << "\t\t" << proc[i].start_time << "\t\t" << proc[i].finish_time << "\t\t" << proc[i].turnaround_time << "\t\t" << proc[i].waiting_time << "\n";
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement