0% found this document useful (0 votes)
22 views

Scheduling Codes

_

Uploaded by

Karan Tikoo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Scheduling Codes

_

Uploaded by

Karan Tikoo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Scheduling Codes

FIFO
Input:
# Input the number of processes
n = int(input("Enter number of processes: "))

# Initialize lists to store arrival times, burst times, completion


times, turnaround times, and waiting times
at, bt, ct, tat, wt = [], [], [0] * n, [], []

# Initialize a variable for calculating completion time and


keeping track of the current time
current_time = 0

# Input arrival times and burst times for each process


for i in range(n):
at.append(int(input(f"Enter arrival time for process P{i+1}:
"))) # Append arrival time to the 'at' list
bt.append(int(input(f"Enter burst time for process P{i+1}:
"))) # Append burst time to the 'bt' list

# Sort processes by arrival time (FIFO)


processes = sorted(range(n), key=lambda i: at[i])
# Calculate completion times for each process
for i in processes:
# If the current time is less than the arrival time, the CPU is
idle
if current_time < at[i]:
current_time = at[i] # CPU remains idle until the next
process arrives

current_time += bt[i] # Add the burst time of the


current process to 'current_time'
ct[i] = current_time # Set the completion time for the
current process

# Calculate turnaround time for each process


tat = [ct[i] - at[i] for i in range(n)] # Turnaround time =
Completion time - Arrival time

# Calculate waiting time for each process


wt = [tat[i] - bt[i] for i in range(n)] # Waiting time =
Turnaround time - Burst time

# Print the table


print("____________________________________________
_________________________________________________"
)
print("|Process | Arrival Time | Burst Time | Completion
Time | Turnaround Time | Waiting Time |")

# Print the details for each process


for i in range(n):
print(f"\n P{i+1} {at[i]} {bt[i]} {ct[i]}
{tat[i]} {wt[i]}")

# Calculate and print the average turnaround time


print(f"Average turnaround time: {sum(tat) / n}")

# Calculate and print the average waiting time


print(f"Average waiting time: {sum(wt) / n}")
Output:
PS C:\Users\choud\OneDrive\Desktop\coos> python
revised.py
Enter number of processes: 3
Enter arrival time for process P1: 0
Enter burst time for process P1: 2
Enter arrival time for process P2: 4
Enter burst time for process P2: 3
Enter arrival time for process P3: 6
Enter burst time for process P3: 1
__________________________________________________
___________________________________________
|Process | Arrival Time | Burst Time | Completion Time |
Turnaround Time | Waiting Time |

P1 0 2 2 2 0
P2 4 3 7 3 0

P3 6 1 8 2 1
Average turnaround time: 2.3333333333333335
Average waiting time: 0.3333333333333333
PS C:\Users\choud\OneDrive\Desktop\coos>
SJF
Input:
def sjf_scheduling(processes):
# n is the number of processes
n = len(processes)

# Sort processes based on their arrival time (index 1 in each


tuple)
processes.sort(key=lambda x: x[1]) # Sort by arrival time

# Initialize a list to track whether each process is completed


completed = [False] * n # Initially, no process is completed

# Initialize current_time to track the system clock


current_time = 0

# Initialize waiting_time, turnaround_time, and


completion_time lists to store the respective times for each
process
waiting_time = [0] * n
turnaround_time = [0] * n
completion_time = [0] * n
# Variables to store total waiting and turnaround times for
calculating averages later
total_waiting_time = 0
total_turnaround_time = 0

# Variable to track the number of completed processes


completed_processes = 0

# Loop until all processes are completed


while completed_processes < n:
# Find the process with the smallest burst time that has
arrived
idx = -1 # Store the index of the process with the
smallest burst time
min_burst = float('inf') # Set an initial high value for the
minimum burst time

# Iterate over all processes to find the one with the


smallest burst time that has arrived
for i in range(n):
# Check if the process has arrived and is not completed
if processes[i][1] <= current_time and not
completed[i]:
# If the current process has a smaller burst time,
select it
if processes[i][2] < min_burst:
min_burst = processes[i][2]
idx = i # Store the index of the selected process

# If no process is ready to execute (i.e., no arrived


process with pending burst time), increment the current time
(system is idle)
if idx == -1:
current_time += 1
else:
# Process selected, increment the current time by the
burst time of the selected process
current_time += processes[idx][2]

# Mark the process as completed and store its


completion time
completion_time[idx] = current_time

# Calculate turnaround time (completion time - arrival


time)
turnaround_time[idx] = completion_time[idx] -
processes[idx][1]
# Calculate waiting time (turnaround time - burst time)
waiting_time[idx] = turnaround_time[idx] -
processes[idx][2]

# Mark the process as completed


completed[idx] = True

# Increment the number of completed processes


completed_processes += 1

# Add the waiting and turnaround times to the totals


total_waiting_time += waiting_time[idx]
total_turnaround_time += turnaround_time[idx]

# Print the results in tabular form for each process


print("\nProcess\tArrival\tBurst\tCompletion\tWaiting\
tTurnaround")
for i in range(n):
# Print process ID, arrival time, burst time, completion
time, waiting time, and turnaround time
print(f"{processes[i][0]}\t{processes[i][1]}\t{processes[i]
[2]}\t{completion_time[i]}\t\t{waiting_time[i]}\
t{turnaround_time[i]}")

# Calculate and print the average waiting and turnaround


times
print(f"\nAverage Waiting Time: {total_waiting_time /
n:.2f}")
print(f"Average Turnaround Time:
{total_turnaround_time / n:.2f}")

# Input from the user


num_processes = int(input("Enter the number of processes:
")) # Get number of processes
processes = [] # Initialize an empty list to store processes

# Loop through each process to get its arrival and burst times
from the user
for i in range(num_processes):
arrival_time = int(input(f"Enter arrival time for Process {i +
1}: ")) # Get arrival time
burst_time = int(input(f"Enter burst time for Process {i + 1}:
")) # Get burst time
processes.append((i + 1, arrival_time, burst_time)) # Store
process as a tuple: (process ID, arrival time, burst time)

# Call the scheduling function with the list of processes


sjf_scheduling(processes)
Output:
PS C:\Users\choud\OneDrive\Desktop\coos> python sjf.py
Enter the number of processes: 5
Enter arrival time for Process 1: 0
Enter burst time for Process 1: 5
Enter arrival time for Process 2: 6
Enter burst time for Process 2: 3
Enter arrival time for Process 3: 9
Enter burst time for Process 3: 8
Enter arrival time for Process 4: 11
Enter burst time for Process 4: 4
Enter arrival time for Process 5: 12
Enter burst time for Process 5: 4

Process Arrival Burst Completion Waiting Turnaround


1 0 5 5 0 5
2 6 3 9 0 3
3 9 8 17 0 8
4 11 4 21 6 10
5 12 4 25 9 13

Average Waiting Time: 3.00


Average Turnaround Time: 7.80
PS C:\Users\choud\OneDrive\Desktop\coos>
RR
Input:
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void sortProcesses(int n, string pnames[], int at[], int bt[]) {


int i, j, val1, val2;
string name;
for (i = 1; i < n; i++) {
name = pnames[i];
val1 = at[i];
val2 = bt[i];
j = i - 1;
while ((j >= 0) && (val1 < at[j])) {
pnames[j + 1] = pnames[j];
at[j + 1] = at[j];
bt[j + 1] = bt[j];
j--;
}
pnames[j + 1] = name;
at[j + 1] = val1;
bt[j + 1] = val2;
}
}

int main() {
int n, tq, sum_tat = 0, sum_wt = 0, remain;

cout << "Enter the number of processes: ";


cin >> n;

string processNames[n];
int AT[n], BT[n], RT[n], CT[n], TAT[n], WT[n];

cout << "Enter the value of time quantum: ";


cin >> tq;

for (int i = 0; i < n; i++) {


processNames[i] = "P" + to_string(i + 1);
cout << "Enter the arrival time of " << processNames[i]
<< ": ";
cin >> AT[i];
cout << "Enter the burst time of " << processNames[i] <<
": ";
cin >> BT[i];
}

// Sorting the processes by arrival time


sortProcesses(n, processNames, AT, BT);

remain = n; // Total processes remaining


for (int i = 0; i < n; i++) {
RT[i] = BT[i]; // Remaining time initially is the burst time
}

int time = 0; // Keeps track of current time


int temp = 0; // Temp variable to signal completion of a
process

cout << "\nInitial Process Table (Before Scheduling):\n";


cout << "| Process | Arrival Time | Burst Time |\n";
cout << "|-------------------------------------|\n";
for (int pid = 0; pid < n; pid++) {
cout << "| " << setw(7) << processNames[pid] << " | " <<
setw(12) << AT[pid] << " | " << setw(10) << BT[pid] << " |\n";
}

int i = 0;
while (remain > 0) {
// Ensure the process is ready (has arrived)
if (RT[i] > 0 && AT[i] <= time) {
// If the process can finish within the time quantum
if (RT[i] <= tq && RT[i] > 0) {
time += RT[i]; // Increment time by remaining time
RT[i] = 0; // Process is done
temp = 1; // Mark the process as complete
}
// If the process needs more time than the quantum
else if (RT[i] > 0) {
RT[i] -= tq; // Deduct the quantum from remaining
time
time += tq; // Increment the time by the quantum
}

// If the process is finished


if (RT[i] == 0 && temp == 1) {
remain--; // Decrease the count of remaining
processes
CT[i] = time; // Completion time is the current time
TAT[i] = CT[i] - AT[i]; // Turnaround time =
Completion time - Arrival time
WT[i] = TAT[i] - BT[i]; // Waiting time = Turnaround
time - Burst time
sum_wt += WT[i];
sum_tat += TAT[i];
temp = 0; // Reset the completion flag
}
}

// Check for the next process or CPU idle


if (i == n - 1) {
i = 0; // Reset to first process
} else if (AT[i + 1] <= time) {
i++; // Move to next process if it's arrived
} else {
// If no process is available, CPU is idle, move time
forward
if (remain > 0 && RT[i] == 0) {
time++; // Increment time in case of idle state
i = 0; // Check all processes again from the
beginning
}
}
}

// Output the final table with completion, turnaround, and


waiting times
cout << "\nFinal Process Table (After Scheduling):\n";
cout << "| Process | Arrival Time | Burst Time | Completion
Time | Turnaround Time | Waiting Time |\n";
cout <<
"|-------------------------------------------------------------------------------
----------|\n";
for (int pid = 0; pid < n; pid++) {
cout << "| " << setw(7) << processNames[pid] << " | " <<
setw(12) << AT[pid] << " | " << setw(10) << BT[pid];
cout << " | " << setw(15) << CT[pid] << " | " << setw(15)
<< TAT[pid] << " | " << setw(12) << WT[pid] << " |\n";
}

// Calculate and print the averages


float avg_tat = (float(sum_tat) / n);
float avg_wt = (float(sum_wt) / n);

cout <<
"|-------------------------------------------------------------------------------
----------|\n";
cout << "Average Turnaround Time: " << avg_tat << "
seconds\n";
cout << "Average Waiting Time: " << avg_wt << " seconds\
n";

return 0;
}

Output:
PS C:\Users\choud\OneDrive\Desktop\coos> g++ -o result
rr.cpp
PS C:\Users\choud\OneDrive\Desktop\coos> ./result.exe
Enter the number of processes: 5
Enter the value of time quantum: 3
Enter the arrival time of P1: 0
Enter the burst time of P1: 5
Enter the arrival time of P2: 6
Enter the burst time of P2: 3
Enter the arrival time of P3: 9
Enter the burst time of P3: 8
Enter the arrival time of P4: 11
Enter the burst time of P4: 4
Enter the arrival time of P5: 12
Enter the burst time of P5: 4

Initial Process Table (Before Scheduling):


| Process | Arrival Time | Burst Time |
|-------------------------------------|
| P1 | 0| 5|
| P2 | 6| 3|
| P3 | 9| 8|
| P4 | 11 | 4|
| P5 | 12 | 4|

Final Process Table (After Scheduling):


| Process | Arrival Time | Burst Time | Completion Time |
Turnaround Time | Waiting Time |
|--------------------------------------------------------------------------------
---------|
| P1 | 0| 5| 5| 5| 0|
| P2 | 6| 3| 9| 3| 0|
| P3 | 9| 8| 25 | 16 | 8|
| P4 | 11 | 4| 22 | 11 | 7|
| P5 | 12 | 4| 23 | 11 | 7|
|--------------------------------------------------------------------------------
---------|
Average Turnaround Time: 9.2 seconds
Average Waiting Time: 4.4 seconds
PS C:\Users\choud\OneDrive\Desktop\coos>

You might also like