Skip to content

Commit 918833a

Browse files
committed
added fcfs_with_concurent_process
1 parent 30316ca commit 918833a

File tree

3 files changed

+204
-1
lines changed

3 files changed

+204
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ py -3.11 -m pip install numpy
4343
### Round Robin (With Different Arrival Time)
4444
[jupyter File](./round_robin.ipynb) <br/>
4545
[Python File](./same_python_file/round_robin.py)
46-
### Sample
46+
### FCFS With Concurent Process
47+
[jupyter File](./fcfs_with_concurent_process.ipynb) <br/>
48+
[Python File](./same_python_file/fcfs_with_concurent_process.py)
49+
### sample
4750
[jupyter File](./first_come_first_serve.ipynb) <br/>
4851
[Python File](./same_python_file/first_come_first_serve.py) &nbsp;&nbsp; / &nbsp;&nbsp; [Python File Alternative](./same_python_file/peterson_solution_alternative.py)
4952

fcfs_with_concurent_process.ipynb

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

0 commit comments

Comments
 (0)