You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The First-Come, First-Served (FCFS) scheduling algorithm is one of the simplest process scheduling algorithms used in operating systems. It follows the principle of serving processes in the order they arrive in the ready queue. In FCFS, the process that arrives first will be the first one to be executed, and it continues until the process completes its execution or enters a waiting state.
4
+
5
+
Algorithm Description:
6
+
7
+
When a process enters the ready queue, it is added to the end of the queue (at the "tail" of the queue).
8
+
9
+
The CPU scheduler selects the process at the front of the queue (the "head" of the queue) for execution.
10
+
11
+
The selected process runs on the CPU until it completes its execution, enters a waiting state (e.g., I/O operation), or its time quantum (if there is a time-sharing system) expires.
12
+
13
+
Once the currently running process finishes, the CPU scheduler selects the process at the front of the queue as the next process to run. This process will continue until it completes or enters a waiting state, and so on.
14
+
15
+
This process of selecting and executing processes continues until all processes in the queue have executed.
16
+
17
+
Example:
18
+
Let's illustrate the FCFS algorithm with a simple example. Consider three processes, P1, P2, and P3, with their respective burst times (the time they need to complete execution):
19
+
20
+
Process P1: Burst time = 2 ms
21
+
Process P2: Burst time = 7 ms
22
+
Process P3: Burst time = 4 ms
23
+
Here's how FCFS will schedule and execute these processes:
24
+
25
+
Initially, the ready queue is empty.
26
+
27
+
Process P1 arrives first, so it is added to the ready queue:
28
+
29
+
Ready Queue: [P1]
30
+
Execution: P1 (2 ms)
31
+
Process P1 completes its execution. Now, process P2 is selected because it's at the front of the queue:
32
+
33
+
Ready Queue: [P2]
34
+
Execution: P2 (7 ms)
35
+
Process P2 completes its execution. Finally, process P3 is selected:
36
+
37
+
Ready Queue: [P3]
38
+
Execution: P3 (4 ms)
39
+
Process P3 completes its execution.
40
+
41
+
The order of execution in FCFS is P1 -> P2 -> P3. FCFS is non-preemptive, meaning once a process starts execution, it continues until it finishes or enters a waiting state. It can suffer from the "convoy effect," where a long process at the head of the queue can block shorter processes behind it.
42
+
43
+
*/
44
+
45
+
#include<stdio.h>
46
+
#include<stdlib.h>
47
+
48
+
// Struct for the info of each process
49
+
typedefstruct
50
+
{
51
+
52
+
intprocess_id;
53
+
intarrival_time;
54
+
intburst_time;
55
+
56
+
} process;
57
+
58
+
59
+
intmain() {
60
+
61
+
// Number of processes
62
+
intnumber=3;
63
+
64
+
// Size of the array which will show the order of execution of the processes
65
+
process*arr;
66
+
arr=malloc(number*sizeof(process));
67
+
68
+
// Initialize for our example
69
+
arr[0].process_id=1;
70
+
arr[0].arrival_time=0;
71
+
arr[0].burst_time=2;
72
+
73
+
arr[1].process_id=2;
74
+
arr[1].arrival_time=1;
75
+
arr[1].burst_time=7;
76
+
77
+
arr[2].process_id=3;
78
+
arr[2].arrival_time=3;
79
+
arr[2].burst_time=4;
80
+
81
+
//Sorting the struct by arrival time
82
+
processtemp;
83
+
for(inti=0; i<number; i++)
84
+
{
85
+
for(intj=0; j<number; j++)
86
+
{
87
+
//if shorter swap
88
+
if(arr[i].arrival_time<arr[j].arrival_time)
89
+
{
90
+
temp=arr[j];
91
+
arr[j] =arr[i];
92
+
arr[i] =temp;
93
+
}
94
+
95
+
}
96
+
}
97
+
98
+
//Variable for the sum of burst time
99
+
intsum_of_burst_time=0;
100
+
101
+
//Calculate sum of burst time for helping us know the size of A
The Shortest Job First (SJF) scheduling algorithm is a non-preemptive scheduling algorithm that selects the process with the shortest burst time for execution. In this algorithm, the process that requires the least amount of time to complete its execution is given the CPU first.
3
+
4
+
Algorithm Description:
5
+
6
+
When a process enters the ready queue, the scheduler compares its burst time with the burst times of all other processes in the queue.
7
+
8
+
The process with the shortest burst time is selected to run next. If two processes have the same shortest burst time, the one that arrived first is chosen.
9
+
10
+
The selected process runs on the CPU until it completes its execution, enters a waiting state (e.g., I/O operation), or its time quantum (if there is a time-sharing system) expires.
11
+
12
+
Once the currently running process finishes, the CPU scheduler again selects the process with the shortest remaining burst time for execution.
13
+
14
+
This process of selecting and executing processes continues until all processes have completed.
15
+
16
+
Example:
17
+
Let's use your example with three processes: P1, P2, and P3, with arrival times and burst times as follows:
18
+
19
+
Process P1: Arrival time = 0 ms, Burst time = 7 ms
20
+
Process P2: Arrival time = 2 ms, Burst time = 3 ms
21
+
Process P3: Arrival time = 4 ms, Burst time = 8 ms
22
+
Here's how SJF will schedule and execute these processes:
23
+
24
+
Initially, the ready queue is empty.
25
+
26
+
Process P1 arrives first, so it is added to the ready queue:
27
+
28
+
Ready Queue: [P1]
29
+
P1 has the shortest burst time (7 ms), so it is selected to run:
30
+
31
+
Execution: P1 (7 ms)
32
+
Ready Queue: []
33
+
Process P2 arrives next, with a shorter burst time than P3, so it is added to the ready queue:
34
+
35
+
Ready Queue: [P2]
36
+
P2 has the shortest burst time (3 ms), so it is selected to run:
37
+
38
+
Execution: P2 (3 ms)
39
+
Ready Queue: []
40
+
Finally, process P3 arrives and is added to the ready queue:
41
+
42
+
Ready Queue: [P3]
43
+
P3 has the shortest burst time (8 ms), so it is selected to run:
44
+
45
+
Execution: P3 (8 ms)
46
+
Ready Queue: []
47
+
The order of execution in SJF is P1 -> P2 -> P3. SJF aims to minimize the average waiting time for processes, making it an effective algorithm when you have information about the burst times of processes. However, it can suffer from starvation if a long job continually arrives after shorter jobs.
48
+
*/
49
+
50
+
#include<stdio.h>
51
+
#include<stdlib.h>
52
+
53
+
// Struct for the info of each process
54
+
typedefstruct
55
+
{
56
+
57
+
intprocess_id;
58
+
intarrival_time;
59
+
intburst_time;
60
+
61
+
} process;
62
+
63
+
64
+
intmain() {
65
+
66
+
// Number of processes
67
+
intnumber=3;
68
+
69
+
// Size of the array which will show the order of execution of the processes
70
+
process*arr;
71
+
arr=malloc(number*sizeof(process));
72
+
73
+
// Initialize for our example
74
+
arr[0].process_id=1;
75
+
arr[0].arrival_time=0;
76
+
arr[0].burst_time=7;
77
+
78
+
arr[1].process_id=2;
79
+
arr[1].arrival_time=2;
80
+
arr[1].burst_time=3;
81
+
82
+
arr[2].process_id=3;
83
+
arr[2].arrival_time=4;
84
+
arr[2].burst_time=8;
85
+
86
+
//Sorting the struct by arrival time
87
+
processtemp;
88
+
for(inti=0; i<number; i++)
89
+
{
90
+
for(intj=0; j<number; j++)
91
+
{
92
+
//if shorter swap
93
+
if(arr[i].arrival_time<arr[j].arrival_time)
94
+
{
95
+
temp=arr[j];
96
+
arr[j] =arr[i];
97
+
arr[i] =temp;
98
+
}
99
+
}
100
+
}
101
+
102
+
//Variables for index,time and minimum burst time
103
+
intk=1,time=0,min;
104
+
105
+
//Sorting to create a correct priority
106
+
for (inti=0; i<number; i++)
107
+
{
108
+
109
+
time=time+arr[i].burst_time; //Add arrival time to time
110
+
min=arr[k].burst_time; //Set minimum burst time
111
+
112
+
for (intj=k; j<number; j++)
113
+
{
114
+
//If time more than or equal to arrival time
115
+
//And burst time less than or equal to minimum
116
+
//Then swap
117
+
if (time >= arr[j].arrival_time&&arr[j].burst_time <= min)
0 commit comments