Skip to content

Commit 9f3ff7e

Browse files
Scheduling Algortihms
1 parent 251e69f commit 9f3ff7e

File tree

3 files changed

+470
-0
lines changed

3 files changed

+470
-0
lines changed

Scheduling Algortihms/fcfs.c

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
3+
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+
typedef struct
50+
{
51+
52+
int process_id;
53+
int arrival_time;
54+
int burst_time;
55+
56+
} process;
57+
58+
59+
int main() {
60+
61+
// Number of processes
62+
int number=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+
process temp;
83+
for(int i=0; i<number; i++)
84+
{
85+
for(int j=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+
int sum_of_burst_time=0;
100+
101+
//Calculate sum of burst time for helping us know the size of A
102+
for (int i=0; i<number; i++)
103+
{
104+
sum_of_burst_time = sum_of_burst_time+arr[i].burst_time;
105+
}
106+
107+
//Create an array to save the processes for gnatt chart
108+
int *A = malloc( sum_of_burst_time * sizeof(int) );
109+
110+
//index for array A
111+
int count=0;
112+
113+
//Save the id in cells of A
114+
for (int i=0; i<number; i++)
115+
{
116+
//Put process id in arr[i].burst_time cells of A
117+
for (int j=0; j<arr[i].burst_time; j++)
118+
{
119+
A[count]=arr[i].process_id; //save id
120+
count++; //increase index
121+
}
122+
123+
}
124+
125+
//Print A (Gnatt chart)
126+
for (int i=0; i<sum_of_burst_time; i++)
127+
{
128+
printf("%d\n",A[i]);
129+
}
130+
131+
return 0;
132+
}

Scheduling Algortihms/sjf.c

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
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+
typedef struct
55+
{
56+
57+
int process_id;
58+
int arrival_time;
59+
int burst_time;
60+
61+
} process;
62+
63+
64+
int main() {
65+
66+
// Number of processes
67+
int number=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+
process temp;
88+
for(int i=0; i<number; i++)
89+
{
90+
for(int j=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+
int k=1,time=0,min;
104+
105+
//Sorting to create a correct priority
106+
for (int i=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 (int j=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)
118+
{
119+
temp = arr[k];
120+
arr[k] = arr[j];
121+
arr[j] = temp;
122+
}
123+
}
124+
k++; //increase index
125+
}
126+
127+
//Variable for the sum of burst time
128+
int sum_of_burst_time=0;
129+
130+
//Calculate sum of burst time
131+
for (int i=0; i<number; i++)
132+
{
133+
sum_of_burst_time = sum_of_burst_time + arr[i].burst_time;
134+
}
135+
136+
//Create an array to save the processesfor gnatt chart
137+
int *A = malloc( sum_of_burst_time * sizeof(int) );
138+
139+
//index for array A
140+
int count=0;
141+
142+
//Save the id in cells of A
143+
for (int i=0; i<number; i++)
144+
{
145+
//Put process id in arr[i].burst_time cells of A
146+
for (int j=0; j<arr[i].burst_time; j++)
147+
{
148+
A[count]=arr[i].process_id;//save id
149+
count++; //increase index
150+
}
151+
152+
}
153+
154+
//Print A (Gnatt chart)
155+
for (int i=0; i<sum_of_burst_time; i++)
156+
{
157+
printf("%d\n",A[i]);
158+
}
159+
160+
return 0;
161+
162+
}

0 commit comments

Comments
 (0)