0% found this document useful (0 votes)
48 views17 pages

FCFS and SJF CPU Scheduling Algorithms

This document contains solutions to operating system practical questions. It includes implementations of FCFS CPU scheduling, SJF CPU scheduling, disk scheduling, memory management algorithms like first fit, best fit and worst fit, shared resource problems using threads, bankers algorithm and page replacement using LRU policy.

Uploaded by

Raj Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views17 pages

FCFS and SJF CPU Scheduling Algorithms

This document contains solutions to operating system practical questions. It includes implementations of FCFS CPU scheduling, SJF CPU scheduling, disk scheduling, memory management algorithms like first fit, best fit and worst fit, shared resource problems using threads, bankers algorithm and page replacement using LRU policy.

Uploaded by

Raj Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Raj ghosh

D053

40315200041

Operating system practical

[Link] implement fcfs cpu scheduling algorithm

Ans.

#Wt=waiting time

#Cbc=cpu burst cycle

#Tat=turn around time

#Avgtime=average time

def findingWt(interpret, num, cbc, wt):

wt[0] = 0

for i in range(1, num ):

wt[i] = cbc[i - 1] + wt[i - 1]

def findingTat(interpret, num, cbc, wt, tat):

for i in range(num):

tat[i] = cbc[i] + wt[i]

def findingavgTime( interpret, num, cbc):

wt = [0] * num

tat = [0] * num

total_wt = 0

total_tat = 0

findingWt(interpret, num, cbc, wt)

findingTat(interpret, num, cbc, wt, tat)


print( "interpreting Cpu burst cycle" + " Waiting time "
+ " Turn around time")

for i in range(num):

total_wt = total_wt + wt[i]

total_tat = total_tat + tat[i]

print(" " + str(i + 1) + "\t\t" +

str(cbc[i]) + "\t " +

str(wt[i]) + "\t\t " +

str(tat[i]))

print( "Average waiting time = "+ str(total_wt / num))

print("Average turn around time = "+ str(total_tat /


num))

if __name__ =="__main__":

interpret = [ 1, 2, 3, 4, 5]

num = len(interpret)

cbc = [2, 8, 12, 10, 6]

findingavgTime(interpret, num, cbc )

output:
[Link] implement sfj cpu scduling program

Ans.

#avg=average

#bt=burst time

#wt=watch time

#tat=turn around time

#pro=processes

max_processes = 12

num=int(input("Enter the number of processes: "))

pro = []

bt = []

wt = [0]*max_processes

tat = [0]*max_processes

avg_tat = 0

avg_wt = 0
for i in range(num):

[Link](input("Enter the Process Id: "))

[Link](int(input("Enter the Cpu Burst Cycle: ")))

for i in range(num):

for j in range(num-i-1):

if bt[j]>bt[j+1]:

bt[j], bt[j+1] = bt[j+1], bt[j]

pro[j] , p[j+1] = p[j+1], p[j]

for i in range(num):

wt[i] = 0

tat[i] = 0

for j in range(i):

wt[i] = wt[i] + bt[j]

tat[i] = wt[i] + bt[i]

avg_tat = avg_tat + tat[i]

avg_wt = avg_wt + wt[i]

print("Process \t\t burst time \t\t wt \t\t tat")

print()

for i in range(num):

print(" ", pro[i], "\t\t\t\t", bt[i], "\t\t", wt[i], "\t\t", tat[i])

print()

print("Average Tat: ", avg_tat/num)

print("Average Wt: ", avg_wt/num)

output:
[Link] implement disc scheduling algorithm

size = 7;

def FCFS(array, head):

seek_count = 0;

distance, current_track = 0, 0;

for i in range(size):

currrent_track = array[i];

distance = abs(current_track - head);

seek_count += distance;

head = current_track;

print("No. of seek operations performed = ",seek_count);

print("Seek Sequence will be");

for i in range(size):

print(array[i]);

if __name__ == '__main__':

array = [ 56, 45, 42, 10 ,112 , 71, 97, 45 ];


head = 50;

FCFS(array, head);

Output:

Implementation of First Fit


A.
def first_fit(block_size, m, process_size, num):
alloc = [-1] * num
for i in range(num):
for j in range(m):
if block_size[j] >= process_size[i]:
alloc[i] = j
block_size[j] -= process_size[i]
break
print(" Process Number Process Size Block Number")
for i in range(num):
print(" ", i + 1, " ", process_size[i],
" ", end = " ")
if alloc[i] != -1:
print(alloc[i] + 1)
else:
print("Not Allocated")
if __name__ == '__main__':
block_size = [15, 12, 10, 10, 25]
process_size = [8, 7, 5, 1, 6, 2]
m = len(block_size)
num = len(process_size)

first_fit(block_size, m, process_size, num)


output:
B.
Implementation of Best - Fit algorithm
def best_fit(block_size, m, process_size, num):
alloc = [-1] * num
for i in range(num):
Index = -1
for j in range(m):
if block_size[j] >= process_size[i]:
if Index == -1:
Index = j
elif block_size[Index] > block_size[j]:
Index = j
if Index != -1:
alloc[i] = Index
block_size[Index] -= process_size[i]
print("Process Number Process Size Block Number")
for i in range(num):
print(i + 1, " ", process_size[i],
end = "
")
if alloc[i] != -1:
print(alloc[i] + 1)
else:
print("Not Allocated")
if __name__ == '__main__':
block_size = [15, 12, 10, 10, 25]
process_size = [8, 7, 5, 1, 6, 2]
m = len(block_size)
num = len(process_size)

best_fit(block_size, m, process_size, num)


output:

C.
Implementation of worst - Fit algorithm
def worst_fit(block_size, m, process_size, num):
alloc = [-1] * num
for i in range(num):
Index = -1
for j in range(m):
if block_size[j] >= process_size[i]:
if Index == -1:
Index = j
elif block_size[Index] < block_size[j]:
Index = j
if Index != -1:
alloc[i] = Index
block_size[Index] -= process_size[i]
print("Process Number Process Size Block Number")
for i in range(num):
print(i + 1, " ",
process_size[i], end = " ")
if alloc[i] != -1:
print(alloc[i] + 1)
else:
print("Not Allocated")
if __name__ == '__main__':
block_size = [15, 12, 10, 10, 25]
process_size = [8, 7, 5, 1, 6, 2]
m = len(block_size)
num = len(process_size)
worst_fit(block_size, m, process_size, num)
output:
[Link] resource java prg
Ans.
class room
{
public synchronized void givesPresentation(String name) throws
InterruptedException
{
[Link](name + " done with projector arrangement...");
[Link](250);
[Link](name + " starts PPT...");
[Link](500);
[Link](name + " completed......");
[Link](250);
}
}
class Student implements Runnable
{
String name;
room r;
Thread t;
Student(String n,room r)
{
name = n;
t = new Thread(this, n);
this.r = r;
}
public void start()
{
[Link]();
}
public void run()
{
try
{
[Link](name);
}
catch(InterruptedException e)
{
[Link](name + " Error.....");
}
}
}
class SharedResource
{
public static void main(String args[])
{
room r = new room();
Student neha = new Student("Neha's Gang",r);
Student kevin = new Student("kevin",r);
Student Shree = new Student("Shree data",r);
[Link]();
[Link]();
[Link]();
}}
Output:

[Link] algorithm
Ans.
if __name__=="__main__":
num = 3
resource = 1
alloc = [[1, 0, 2 ],[ 0, 3, 1 ],[1, 0, 2 ]]
max = [[4, 1, 2 ],[1, 5, 3 ],[ 1, 2, 3 ]]
avail = [2, 2, 0]
f = [0]*num
result = [0]*num
temp = 0
for k in range(num):
f[k] = 0
need = [[ 0 for i in range(resource)]for i in range(num)]
for i in range(num):
for j in range(resource):
need[i][j] = max[i][j] - alloc[i][j]
y=0
for k in range(3):
for i in range(num):
if (f[i] == 0):
flag = 0
for j in range(resource):
if (need[i][j] > avail[j]):
flag = 1
break
if (flag == 0):
result[temp] = i
temp += 1
for y in range(resource):
avail[y] += alloc[i][y]
f[i] = 1

print("The SAFE Sequence will be:")

for i in range(num - 1):


print(" P", result[i], " ->", sep="", end="")
print(" P", result[num - 1], sep="")
output:

Implementation of page fault


Ans.
from queue import Queue

def pgFaults(pg, num, temp):

s = set()

index = Queue()

pg_faults = 0

for i in range(num):

if (len(s) < temp):


if (pg[i] not in s):

[Link](pg[i])

pg_faults += 1

[Link](pg[i])

else:

if (pg[i] not in s):

value = [Link][0]

[Link]()

[Link](value)

[Link](pg[i])

[Link](pg[i])

pg_faults += 1

return pg_faults

if __name__ == '__main__':

pg = [6, 0, 1, 2, 4, 3, 0, 2, 6, 3, 2, 0, 1, 6]

num = len(pg)

temp = 3

print(pgFaults(pg, num, temp))

output:

Common questions

Powered by AI

Using a synchronized method in a shared resource Java program ensures that only one thread can access the method at a time, preventing concurrency issues like race conditions . This synchronization guarantees that shared data is accessed in a serialized manner, maintaining data consistency and integrity, especially crucial in operations requiring timely and ordered execution such as presentations in a shared room .

The First Fit algorithm allocates the first block that is large enough for a process, ensuring a quick allocation but possibly leading to fragmentation as larger blocks may be split inefficiently . The Best Fit algorithm chooses the smallest available block that fits the process, aiming to reduce fragmentation but may increase overhead due to searching . The Worst Fit algorithm allocates the largest available block, attempting to leave larger blocks for future allocations but may lead to inefficient utilization of memory since larger blocks are filled first .

SJF scheduling is more efficient than FCFS in minimizing the average turnaround time because it prioritizes shorter processes, allowing them to complete faster thus reducing overall waiting and turnaround times . FCFS, however, schedules processes in the order of arrival, which can lead to the 'convoy effect' where shorter jobs wait for longer ones to finish, increasing the average turnaround time .

The Banker's Algorithm ensures system safety by calculating the maximum possible request that each process can make and determining if resources can be allocated without leading to a deadlock . It simulates resource allocation by checking against available resources and the maximum demand of processes to find a safe sequence where all processes can finish without conflicts, thus preventing deadlock situations and securing system stability .

The FCFS disk scheduling algorithm is designed to minimize the seek time by processing requests in the order they arrive, providing simplicity and predictability but potentially leading to long wait times or inefficient head movement across the disk . Conversely, CPU scheduling algorithms like FCFS aim at optimizing CPU usage by reducing average waiting times and turnaround times for processes, focusing more on processor time efficiency rather than minimizing seek time .

First Fit allocation is fast due to its simplicity, as it allocates the first suitable block it finds, but can result in significant external fragmentation as larger blocks may be inefficiently split . Best Fit allocation reduces fragmentation by selecting the smallest possible block that fits, but it is slower because it requires searching the entire list to find this optimal block, which increases the overhead .

You might also like