Skip to content

Commit 7a6aff9

Browse files
committed
added four basic scheduling algorithm
1 parent 80bd9d9 commit 7a6aff9

13 files changed

+1979
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,38 @@ py -3.11 -m pip install numpy
1818
```
1919

2020
## Code for Academic Course CIT-322 (Operating System Sessional)
21-
#### `**********************`
21+
22+
23+
<hr/>
24+
25+
## `Mahabub Sir Part`
26+
<hr/>
27+
28+
### First Come First Serve (FCFS) (With Different Arrival Time)
29+
[jupyter File](./first_come_first_serve.ipynb) <br/>
30+
[Python File](./same_python_file/first_come_first_serve.py)
31+
### Shortest Job First Non-Preemptive (SJF) (With Different Arrival Time)
32+
[jupyter File](./shortest_job_first_non_preemptive.ipynb) <br/>
33+
[Python File](./same_python_file/shortest_job_first_non_preemptive.py)
34+
### Shortest Job First Preemptive (SRT) (With Different Arrival Time)
35+
[jupyter File](./shortest_job_first_preemptive.ipynb) <br/>
36+
[Python File](./same_python_file/shortest_job_first_preemptive.py)
37+
### Priority Primptive (With Different Arrival Time)
38+
[jupyter File](./priority_primptive.ipynb) <br/>
39+
[Python File](./same_python_file/priority_primptive.py)
40+
### Priority Non-Primptive (With Different Arrival Time)
41+
[jupyter File](./priority_non_primptive.ipynb) <br/>
42+
[Python File](./same_python_file/priority_non_primptive.py)
43+
### Round Robin (With Different Arrival Time)
44+
[jupyter File](./round_robin.ipynb) <br/>
45+
[Python File](./same_python_file/round_robin.py)
46+
### Sample
47+
[jupyter File](./first_come_first_serve.ipynb) <br/>
48+
[Python File](./same_python_file/first_come_first_serve.py) &nbsp;&nbsp; / &nbsp;&nbsp; [Python File Alternative](./same_python_file/peterson_solution_alternative.py)
49+
50+
<hr/><hr/>
51+
52+
## `Masud Sir Part`
2253
<hr/>
2354

2455
### Lab - 1 == Peterson Solution (Producer Consumer Problem)
@@ -47,4 +78,4 @@ py -3.11 -m pip install numpy
4778

4879

4980

50-
<hr/><hr/>
81+
<hr/>

first_come_first_serve.ipynb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def fcfs_scheduling(processes):\n",
10+
" # Sort the processes based on their arrival time (ascending order)\n",
11+
" # This ensures that processes with earlier arrival times are scheduled first.\n",
12+
" processes.sort(key=lambda p: p[1])\n",
13+
"\n",
14+
" # Initialize the starting time of the first process as its arrival time\n",
15+
" current_time = processes[0][1]\n",
16+
"\n",
17+
" # Initialize variables to keep track of total waiting time and turnaround time\n",
18+
" waiting_time_total = 0\n",
19+
" turnaround_time_total = 0\n",
20+
"\n",
21+
" # Print the table header for the process details\n",
22+
" print(\"Process ID\\tArrival Time\\tBurst Time\\tWaiting Time\\tTurnaround Time\")\n",
23+
"\n",
24+
" # Iterate through each process in the sorted order\n",
25+
" for process in processes:\n",
26+
" # Unpack the process details (process ID, arrival time, and burst time)\n",
27+
" pid, arrival_time, burst_time = process\n",
28+
"\n",
29+
" # Calculate the waiting time for the current process, ensuring it is non-negative\n",
30+
" waiting_time = max(0, current_time - arrival_time)\n",
31+
"\n",
32+
" # Calculate the turnaround time for the current process\n",
33+
" turnaround_time = waiting_time + burst_time\n",
34+
"\n",
35+
" # Update the total waiting time and turnaround time\n",
36+
" waiting_time_total += waiting_time\n",
37+
" turnaround_time_total += turnaround_time\n",
38+
"\n",
39+
" # Print the details for the current process\n",
40+
" print(f\"{pid}\\t\\t{arrival_time}\\t\\t{burst_time}\\t\\t{waiting_time}\\t\\t{turnaround_time}\")\n",
41+
"\n",
42+
" # Update the current time to the completion time of the current process\n",
43+
" current_time += burst_time\n",
44+
"\n",
45+
" # Calculate the average waiting time and turnaround time for all processes\n",
46+
" n = len(processes)\n",
47+
" avg_waiting_time = waiting_time_total / n\n",
48+
" avg_turnaround_time = turnaround_time_total / n\n",
49+
"\n",
50+
" # Print the average waiting time and average turnaround time\n",
51+
" print(f\"\\nAverage Waiting Time: {avg_waiting_time:.2f}\")\n",
52+
" print(f\"Average Turnaround Time: {avg_turnaround_time:.2f}\")\n",
53+
"\n",
54+
"if __name__ == \"__main__\":\n",
55+
" # Example usage with user input for the number of processes\n",
56+
" n = int(input(\"Enter the number of processes: \"))\n",
57+
" processes = []\n",
58+
"\n",
59+
" # Prompt the user to enter arrival time and burst time for each process\n",
60+
" print(\"\\nEnter Process Arrival Time and Burst Time\")\n",
61+
" for i in range(n):\n",
62+
" arrival_time = float(input(f\"P[{i+1}] Arrival Time: \"))\n",
63+
" burst_time = float(input(f\"P[{i+1}] Burst Time: \"))\n",
64+
" processes.append((i+1, arrival_time, burst_time))\n",
65+
"\n",
66+
" # Call the FCFS scheduling function with the list of processes\n",
67+
" fcfs_scheduling(processes)\n"
68+
]
69+
}
70+
],
71+
"metadata": {
72+
"kernelspec": {
73+
"display_name": "Python 3",
74+
"language": "python",
75+
"name": "python3"
76+
},
77+
"language_info": {
78+
"codemirror_mode": {
79+
"name": "ipython",
80+
"version": 3
81+
},
82+
"file_extension": ".py",
83+
"mimetype": "text/x-python",
84+
"name": "python",
85+
"nbconvert_exporter": "python",
86+
"pygments_lexer": "ipython3",
87+
"version": "3.11.0"
88+
},
89+
"orig_nbformat": 4
90+
},
91+
"nbformat": 4,
92+
"nbformat_minor": 2
93+
}

priority_non_primptive.ipynb

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Works only if arrival time and burst time is in integer\n",
8+
"### highest priority == higher number "
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"metadata": {},
15+
"outputs": [],
16+
"source": [
17+
"# https://cppsecrets.com/users/1108979711510497121461151049710464115111109971051219746101100117/Python-Priority-Scheduling-Non-Pre-emptive-Algorithm-with-Different-Arrival-Time.php\n",
18+
"\n",
19+
"\n",
20+
"class Priority:\n",
21+
"\n",
22+
" def processData(self, no_of_processes):\n",
23+
" process_data = []\n",
24+
" for i in range(no_of_processes):\n",
25+
" temporary = []\n",
26+
" process_id = int(input(\"Enter Process ID: \"))\n",
27+
"\n",
28+
" arrival_time = int(input(f\"Enter Arrival Time for Process {process_id}: \"))\n",
29+
"\n",
30+
" burst_time = int(input(f\"Enter Burst Time for Process {process_id}: \"))\n",
31+
"\n",
32+
" priority = int(input(f\"Enter Priority for Process {process_id}: \"))\n",
33+
"\n",
34+
" temporary.extend([process_id, arrival_time, burst_time, priority, 0])\n",
35+
" '''\n",
36+
" '0' is the state of the process. 0 means not executed and 1 means execution complete\n",
37+
" '''\n",
38+
" process_data.append(temporary)\n",
39+
" Priority.schedulingProcess(self, process_data)\n",
40+
"\n",
41+
"\n",
42+
" def schedulingProcess(self, process_data):\n",
43+
" start_time = []\n",
44+
" exit_time = []\n",
45+
" s_time = 0\n",
46+
" process_data.sort(key=lambda x: x[1])\n",
47+
" '''\n",
48+
" Sort processes according to the Arrival Time\n",
49+
" '''\n",
50+
" for i in range(len(process_data)):\n",
51+
" ready_queue = []\n",
52+
" temp = []\n",
53+
" normal_queue = []\n",
54+
" for j in range(len(process_data)):\n",
55+
" if (process_data[j][1] <= s_time) and (process_data[j][4] == 0):\n",
56+
" temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])\n",
57+
" ready_queue.append(temp)\n",
58+
" temp = []\n",
59+
" elif process_data[j][4] == 0:\n",
60+
" temp.extend([process_data[j][0], process_data[j][1], process_data[j][2], process_data[j][3]])\n",
61+
" normal_queue.append(temp)\n",
62+
" temp = []\n",
63+
" if len(ready_queue) != 0:\n",
64+
" ready_queue.sort(key=lambda x: x[3], reverse=True)\n",
65+
" '''\n",
66+
" Sort the processes according to the Priority, considering Higher the Value, Higher the Priority\n",
67+
" '''\n",
68+
" start_time.append(s_time)\n",
69+
" s_time = s_time + ready_queue[0][2]\n",
70+
" e_time = s_time\n",
71+
" exit_time.append(e_time)\n",
72+
" for k in range(len(process_data)):\n",
73+
" if process_data[k][0] == ready_queue[0][0]:\n",
74+
" break\n",
75+
" process_data[k][4] = 1\n",
76+
" process_data[k].append(e_time)\n",
77+
" elif len(ready_queue) == 0:\n",
78+
" if s_time < normal_queue[0][1]:\n",
79+
" s_time = normal_queue[0][1]\n",
80+
" start_time.append(s_time)\n",
81+
" s_time = s_time + normal_queue[0][2]\n",
82+
" e_time = s_time\n",
83+
" exit_time.append(e_time)\n",
84+
" for k in range(len(process_data)):\n",
85+
" if process_data[k][0] == normal_queue[0][0]:\n",
86+
" break\n",
87+
" process_data[k][4] = 1\n",
88+
" process_data[k].append(e_time)\n",
89+
" t_time = Priority.calculateTurnaroundTime(self, process_data)\n",
90+
" w_time = Priority.calculateWaitingTime(self, process_data)\n",
91+
" Priority.printData(self, process_data, t_time, w_time)\n",
92+
"\n",
93+
"\n",
94+
" def calculateTurnaroundTime(self, process_data):\n",
95+
" total_turnaround_time = 0\n",
96+
" for i in range(len(process_data)):\n",
97+
" turnaround_time = process_data[i][5] - process_data[i][1]\n",
98+
" '''\n",
99+
" turnaround_time = completion_time - arrival_time\n",
100+
" '''\n",
101+
" total_turnaround_time = total_turnaround_time + turnaround_time\n",
102+
" process_data[i].append(turnaround_time)\n",
103+
" average_turnaround_time = total_turnaround_time / len(process_data)\n",
104+
" '''\n",
105+
" average_turnaround_time = total_turnaround_time / no_of_processes\n",
106+
" '''\n",
107+
" return average_turnaround_time\n",
108+
"\n",
109+
"\n",
110+
" def calculateWaitingTime(self, process_data):\n",
111+
" total_waiting_time = 0\n",
112+
" for i in range(len(process_data)):\n",
113+
" waiting_time = process_data[i][6] - process_data[i][2]\n",
114+
" '''\n",
115+
" waiting_time = turnaround_time - burst_time\n",
116+
" '''\n",
117+
" total_waiting_time = total_waiting_time + waiting_time\n",
118+
" process_data[i].append(waiting_time)\n",
119+
" average_waiting_time = total_waiting_time / len(process_data)\n",
120+
" '''\n",
121+
" average_waiting_time = total_waiting_time / no_of_processes\n",
122+
" '''\n",
123+
" return average_waiting_time\n",
124+
"\n",
125+
"\n",
126+
" def printData(self, process_data, average_turnaround_time, average_waiting_time):\n",
127+
" process_data.sort(key=lambda x: x[0])\n",
128+
" '''\n",
129+
" Sort processes according to the Process ID\n",
130+
" '''\n",
131+
" print(\"Process_ID Arrival_Time Burst_Time Priority Completed Completion_Time Turnaround_Time Waiting_Time\")\n",
132+
" for i in range(len(process_data)):\n",
133+
" for j in range(len(process_data[i])):\n",
134+
" print(process_data[i][j], end=\"\\t\\t\")\n",
135+
" print()\n",
136+
" print(f'Average Turnaround Time: {average_turnaround_time}')\n",
137+
"\n",
138+
" print(f'Average Waiting Time: {average_waiting_time}')\n",
139+
"\n",
140+
"\n",
141+
"if __name__ == \"__main__\":\n",
142+
" no_of_processes = int(input(\"Enter number of processes: \"))\n",
143+
" priority = Priority()\n",
144+
" priority.processData(no_of_processes)\n",
145+
"\n",
146+
"### highest priority == higher number \n",
147+
"\n",
148+
"\n",
149+
"'''\n",
150+
"5\n",
151+
"1\n",
152+
"0\n",
153+
"2\n",
154+
"2\n",
155+
"2\n",
156+
"0\n",
157+
"1\n",
158+
"1\n",
159+
"3\n",
160+
"0\n",
161+
"8\n",
162+
"4\n",
163+
"4\n",
164+
"0\n",
165+
"4\n",
166+
"2\n",
167+
"5\n",
168+
"0\n",
169+
"5\n",
170+
"3\n",
171+
"\n",
172+
"\n",
173+
"\n",
174+
"\n",
175+
"'''\n",
176+
"\n"
177+
]
178+
}
179+
],
180+
"metadata": {
181+
"kernelspec": {
182+
"display_name": "Python 3",
183+
"language": "python",
184+
"name": "python3"
185+
},
186+
"language_info": {
187+
"codemirror_mode": {
188+
"name": "ipython",
189+
"version": 3
190+
},
191+
"file_extension": ".py",
192+
"mimetype": "text/x-python",
193+
"name": "python",
194+
"nbconvert_exporter": "python",
195+
"pygments_lexer": "ipython3",
196+
"version": "3.11.0"
197+
},
198+
"orig_nbformat": 4
199+
},
200+
"nbformat": 4,
201+
"nbformat_minor": 2
202+
}

0 commit comments

Comments
 (0)