First Come First Served (FCFS) CPU Scheduling Algorithm in C++ with Explanation:
CPU gets a lot of processes to handle. The problem is shortening the waiting time for a process to reach CPU and get processed. Now consider a CPU and also consider a list in which the processes are listed as follows,
Arrival
|
Process
|
Burst Time
|
0
|
1
|
3
|
1
|
2
|
2
|
2
|
3
|
1
|
Process-1
|
Process-1
|
Process-1
|
Process-2
|
Process-2
|
Process-3
|
0s
|
1s
|
2s
|
3s
|
4s
|
5s
|
| Process-1
|
| Process-2
|
| Process-3
|
|
|
0
|
3
|
5
|
6
|
Process-2 at 3s. So process-2 waited for 2s.
Process-1
|
Process-1
|
Process-1
|
Process-2
|
Process-2
|
Process-3
|
0s
|
1s
|
2s
|
3s
|
4s
|
5s
|
Process-1
|
Process-1
|
Process-1
|
Process-2
|
Process-2
|
Process-3
|
0s
|
1s
|
2s
|
3s
|
4s
|
5s
|
The Program:
Input
You will ask the user for the number of processes. Then for each process you will take its Process Number, Arrival Time and its Burst time. You don’t have to worry, the number of processes wont be more than 5 or 6, Arrival time of a process can only be equal or greater than the arrival time of its previous process and Process will be entered as a serial number, so no problem.
Output:
In the output you will have to print out the shortened time-line we showed you above. For example, if the input is as follows,
Arrival
|
Process
|
Burst Time
|
0
|
1
|
3
|
1
|
2
|
2
|
2
|
3
|
1
|
| Process-1
|
| Process-2
|
| Process-3
|
|
|
0
|
3
|
5
|
6
|
You can also print out the time-line vertically if you want (that’s what I have done, see the output of my program)
Process
|
Arrival
|
Finish
|
Total
|
Wait
|
1
|
0
|
2
|
3
|
0
|
2
|
1
|
4
|
2
|
2
|
3
|
2
|
5
|
1
|
3
|
Exception:
One exception is that, I said “Arrival time of a process can only be equal or greater than the arrival time of its previous process”. So take a look at the following list of process,
Arrival
|
Process
|
Burst Time
|
0
|
1
|
3
|
55
|
2
|
2
|
60
|
3
|
1
|