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

Os 2

The document describes the implementation of round robin scheduling. It discusses the theory behind round robin scheduling where each process gets a time quantum or time slice to execute before being preempted. The code provided implements round robin scheduling by taking input for the number of processes, their arrival times and burst times. It then runs a loop allocating each process a time slot to execute before switching to the next process. The turnaround times and waiting times are calculated and printed.

Uploaded by

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

Os 2

The document describes the implementation of round robin scheduling. It discusses the theory behind round robin scheduling where each process gets a time quantum or time slice to execute before being preempted. The code provided implements round robin scheduling by taking input for the number of processes, their arrival times and burst times. It then runs a loop allocating each process a time slot to execute before switching to the next process. The turnaround times and waiting times are calculated and printed.

Uploaded by

Pragya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

QUTPUT: -

tecZ-user@ip-172-31-42-186 ~1^ gCC sjf.C -0 sjÍ


[ec2-user@ip-172-31-42-186 -1$ /sjf
Bnter number of process 5
Enter Burst Tine:
pl:4
p2:3
p3:7
p4:1
p5:2
Turnaround Time
Process Burst Time Waiting Time
p4 1
p5 2 1
3 6
p2 10
p1 4 6
10 17
p3
Average Waiting Time = 4 000000
Average Turnaround Time7.400000
L(ec2-usereip-172-31-42-186-1s
AIM: - EXPERIMENT -5
Impl
THEORY:
In
emcntation of priority scheduling.
Priority
lower scheduling,
the number, there is a priority number
will be the the higher the prpriority. While, inassigned to cach process. In some systems, the
the others, the higher the number, the higher
the CPU. priority. The Process with the higher priority among the available
There are two types of priority processes is given
scheduling while the other is schheduling algorithm exists. One is Pre-emptive priority
Non Pre-emptive Priority scheduling.

Priority
Scheduling

Preemptive Non Preemptive

The priority number assigned to each of the


doesn't change itself throughout the process, process may or may not vary. If the
it is called static priority, while if it priority number
itself at the regular intervals, it is called keeps changing
dynamic priority.

Priority

Static Dynamic

CODE; -
1. /Function to swap two variables
2. void swap(int *a,int *b)
3. {
4. int temp=*a;
*=*b;

6. *b=tcmp:
7. }
8. int
main()
9.
10. int n:
11.
printf("Enter Number of Proccsscs: ");
12.
13.
scanf("%d",&n);
oLS array for burst time. n for priority and index for process 4
14. int b[n],p[n],index[n];
15. for(int i=0;i<n;i++)
16.
Process %d: ",1+l);
17. Printr("Enter Burst Time andPriority Value for
18. scanf("%d %d",&b[i], &p[i]);
19. index[i]-i+1;
20.

21. for(int i=0;i<n;it+)


22. {
23. int a-p[i],m=i;
and placing it at its desired
position
24. IMFinding out highest priority element
25. for(int j=ij<njt)
26.

27. if(pi]> a)
28. {
a-pil:
29.
Imj
30.

31.

32.
/Swappingprocesses
33. swap(&p[i], &p[m]D;
34.
35. swap(&b[i], &b[m]);
36. swap(&index(i], &index[m]);
37.

38. /Tstores the starting time of process


39. int t-0;
40. I/Printing scheduled process
41. printf("Order of process Execution is\n");
42. for(int i-0;i<n;i++)
43.
44. printf("P%d is execcuted from %d to %d\n",index[i],t,t+b[i]);
45. tt-b[i];
46.

47. printf("\n");
48. printf("Process Id Burst Time Wait Time TurnAround Time\n"):
49. int wait time-0;
50. for(int i=0;i<n;it+)
51. {
52. printf("P%d %d %d
b[i); %d\in",index[i],b[i],wait time, wait time +
53. wait time += b[i];
54.

55. return 0:

56.}
OUTPUT: -
[admin@fedora-]$ vi PS.c
PS
[admin@fedora ]$ gcG PS.C -o
[admin@fedora 1$ /PS
Enter Number of
Processes: 3
Value for Process l: 19 2
Enter Burst Time and Priority
Value for Process 2: 5 9
Enter Burst Tine and Priority
Value for Process 3: 8 1
Enter Burst Time and Priority
is
Order of process Execution
19
Pl is executed from0 to
to 18
P3 is executed from 10
23
P2 is executed from 18 to

Burst Time Wait Time TurnAround Time


Process Id
10
Pl
8 10 18
P3
P2 5 18 23
EXPERIMENT - 6
AIM: -mplementation of Round Robin scheduling.
THEORY: -
Round Robin CPU Scheduling is the most important CPU Scheduling Algorithm which is ever
used in the history of CPU Scheduling Algorithms. Round Robin CPU Scheduling uses Time
Quantum (TQ). The Time Quantum is something which is removed from the Burst Time and lets
the chunk of process to be completed.
Time Sharing is the main emphasis of the algorithm. Each step of this algorithm is carried out
cyclically. The system defines aspecific tÉme slice, known as atime quantum.
First, the processes which are eligible to enter the ready queue enter the ready queue. After entering
the first process in Ready Queue is executed for a Time Quantum chunk of time. After execution
is complete, the process is removed from the ready queue. Even now the process requires some
time to complete its execution, then the process is added to Ready Queue.
The Ready Queue does not hold processes which alrcady present in the Ready Queue. The Ready
Queue is designed in such amanner that it does not hold non unique processes. By holding same
processes Redundancy of the processes increases.
After, the process execution is complete, the Ready Queue does not take the completed process
for holding.

Ready State

Execution for Time No Yes Execute Till


BT<TQ?
Quantum Completion

Tg
Finished

Exeedtion Yes Termination


Finished?

No
Advantages
The Advantages of Round Robin CPU Scheduling are:
1. A fair amount of CPUis allocated to each job.
2. Because it doesn't depend on the burst time, it can truly be implemented in the system.
3. It is not affected by the convoy effect or the starvation problem as occurred in First Come
First Serve CPU Scheduling Algorithm.
Disadvantages
The Disadvantages of Round Robin CPUScheduling are:
1. Low Operating System slicing times will result in decreased CPU output.
2. Round Robin CPUScheduling approach takes
longer to Swap contexts.
3. Time quantumn has a significant impact on
its performance.
4. The procedures cannot have
priorities established.

CODE:
1. #include<stdio.h>

2. int main()
3. {
4.
/Input no of processed
5. int n;
6.
printf("Enter Total Number of Processes:");
7.
scanf("%od", &n);
8. int wait time = 0,
9.
ta_time = 0, arr_time[n], burst_time[n], temp
int x = n; burst time[n];
10. //nput details of processes
11. for(int i= 0; isn; itt)
12.

13.
printf("Enter Details of Process %d \n", i+ 1):
14. printf("Arrival Time: "):
15 scanf("%d", &arr time[il);
16 printf("Burst Time: ");:
17 scanf("%d", &burst_ time[i]);
18 temp_burst_timeli] =burst time[i];
19. }
20. /Input time slot
21. int time slot;
22. printf("Enter Time Slot:"):
23. scanf("%d", &time slot);
24. I/Total indicates total time
25. l/counter indicates which process is executed
26. int total =0, counter =0,i;
27. printf("Process ID Burst Time Turnaround Time
Waitíng Time n"):
28. for(total=0, i=0; x!-0; )
29.

30. l define the conditions


31.
if(temp burst time[i] = time slot && temp burst
32. time[]>0)
33. total =total + temp burst time[i];
34.
temp_burst time[i] =0;
35. counter=l:
36

37. clse if(temp_burst time[]>0)


38

39. temp burst_time[i] =temp burst_time[i] - time slot;


40. total += ime slot;
41.
42 if(temp burst_time[il=-0&& counter==1)
43.

44. X--; //decrement the process no.


45. printf("\nProcess No %d \tlt %d\tltltlt 9%d\t\t\t %d", i+1, burst time[Ü),

46. total-arr time[i], total-arr time[i]-burst_time[]);


47. wait time= wait time+total-arr time[i]-burst time[i];
48. ta time += total -arr time{i];
49. counter =0;
50. }
51. if(i==n-1)
52.

53. i-0;
54.

55. else if(arr time[it1]<=total)


56.

57. it+;
58.

59. else

60.

61. i=0;
62.

63.}
64. float average wait time = wait time * 1.0/ n:
65. float average turnaround time = ta
_time*107n:
66.
printf("nAverage Waiting Time:%f",average_wait_time);
67. printf("nAvg Turnaround Time:%f", average turnaround time):
68. return 0;
69.}
OUTPUT: -
[adminafedora 1$ vi RRS.C
[adminafedora ]$ gCC RRS.C o RRS
[adminafedora $/RRs
Enter Total Number of
Processes!3
Enter Detalls of Process 1
Arrival Time: 0
Burst Time: 10
Enter Details of Process 2
Arrival Time: 1
Burst Time: 8
Enter Details of Process 3
Arrìval Tine:2
Burst Time7
Enter Time slot:5
Process ID Burst Time Turnaround Time Waiting Time
Process No 20 10
ProceSS NO 22 14
Process NO 3 23 16
Average Waiting Time:13.333333
AVg Turnaround Time:21.666666 [admingfedora-1$
EXPERIMENT -7
AIM:-Implementation of LRU page replacement policy.
THEORY:-
The LRUalgorithm keeps track of the order in which pages are
accessed and maintains a list of
pages in memory, ordered from most recently accessed to least recently accessed.
When a new page necds to be loaded into menmory, and there is no free space, the
algorithm scans
the list from the beginning and replaces the page that was accessed least
recently.
In other words, any page that has been inactive for an extended period is more likely to
remain
inactive. As a result, it is preferable to replace that page.
The LRU algorithm works on the principle of locality of reference, which states that
programs
tend to access a small portion of memory at any given time. Therefore, pages that were accessed
recently are more likely to be accessed again soon than pages that were accessed a long time ago.
Advantages of LRU
1. Mininmizes the number of page faults: The main advantage of the LRUalgorithm is that it
optimal in the sense that it minimizes the number of page faults that occur.
2. Optimizes memory usage: The LRU algorithm keeps the pages that are most frequently
accessed in memory, which optimizes memory usage and ensures that frequently used
pages are available quickly.
3. Simple implementation: The LRU algorithm is easy to understand and implement. It
requires only a linked list or a priority queue to keep track of the order in which pages are
accessed.

Disadvantages of LRU
1. Computationally Expensive: It can be computationally expensive to maintain the access
history of each page, especially in systems with a large number of pages.
2. Performance Issue: It may perform poorly in cases where the access patterns of a program
do not follow the principle of locality of reference.
3. Limited scalability: The LRU algorithm may not be scalable for large-scale systems
because it requires maintaining alinked list or priority queue that can become prohibitively
large.

CODE: -
#include <stdio.h>

lluser-defincd function

int findLRU(int time[],int n)


int i, minimum =time[0], pos =0;
for (i =I; i<n; ++i)

if (imeli]<minimum)

minimun =time[i];
pos =i;

return pos:

l/main function

int main)

int no_of frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flagl, flag2, i,
j. pos, faults = 0;
príntf("Enter number of frames: ");
scanf("%d", &no _of_frames);
printf("Enter number of pages: ");
scanf("%d", &no _of pages);

printf("Enter refercnce string: ");


for (i = 0; i< no of pages; tti)

scanf("%d", &pages[i]);

for (i= 0; i< no oframes; ++i)


frames[i]= -1;

pages; ++i)
for (i = 0; i< no of

flagl = flag2 = 0;
++)
for (j = 0;j< no_of_frames;

if (frames[i]==pages[i])

countert+;

time[il= counter;
flagl = flag2 = 1;
break:

if (flagl 0)

for (j = 0; j< no_of frames; +tj)

if (frames[i] -=-1)

countert+;
faults++t;
frames[j]=pages[i]:
time[j]= counter;
flag2 = 1;
break:
if(tlag2=0))

findLRU(time, no of frames):
pos =
Countcr++:

faults+;
frames[pos] = pages[i];
time[pos]= counter;

printf("n");
for (j =0:j< no_of_frames; ++)

printf("%d\t", framesi);

printf("nTotal Page Faults =%d", faults);


return 0:;
OUTPUT: -

fec2-u5er@ip-172-31-23-10]$ ví LRUC
Tec2-user@ip-l72-31-23-10~l$ gcc LRU.C-0 LRU
[ec2-usereip-172-31-23-10~15 /LRU
frames: 3
Enter number of
Of pages: 10
Enter number 9.4379 6 2 1
Bnter reference string: 75

5
5

3
7

9
1
EXPERIMENT 8
replacement policy.
AIM:- Implementation of FIFO page
THEORY:
replacement algoriths
First Out is one of the types of page
FIEO which is also known as First In system
algorithm is used in the paging method for memory management in an operating
TheFIFO algorithm replaces the
decides which existing page needs to be replaced in the queue. FIFO memory.
that main
present for the longest time in the
oldest (First)page which has been
Replacement Algorithm
Advantages of FIFO Page
simplicity.
is commonly known for its
FIFO page replacement algorithm
implement as well as understand.
FIFO algorithm is much easy to
algorithm efficiently.
Small systems can use the FIFO
Algorithm
Disadvantages of FIFO Page Replacement
-Queue== data structure.
additional
FIFOalgorithm in os uses an
frames
from =Belady's anomaly== problem ie when the number of page
It suffers number of
more memory is given to processes, but instead of decreasing, the
increases,
page faults increases.

CODE: -
#include<stdio.h>

int main)

int incomingStream[]={4, 1, 2, 4, 5};


Int pageFaults = 0;
int frames =3:

int m, n, s, pages;

pages = sizeof(incomingStream)/sizeof(incomingStream[0):
printfIncoming \t Frame 1 \t Frame 2 \t Frame 3"):
int temp[frames];
for(1m = 0; m < frames; m++)
temp[m]=-1;

for (m = 0; m < pages; m++)

S=0:

for(n = 0; n < frames; nt+)

if(incomingStrcam[m] =temp[n)

St+;
pageFaults--;

pageFaults++;
if((pageFaults <= frames) && (s = 0)

temp[m] = incomingStream[m;

}
else if(s ==0)

incomingStream[m];
temp[(pageFaults - 1) % frames] =

printf("\n");
printf("%d\t\t)",incomingStream[m]);
n++)
for(n0; n < frames;

if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else

printf(" - \tlt\t");

printf("\nTotal Page Faults:\t%d\n", pageFaults);


return 0;

OUTPUT: -

[ec2-usereip-172-31-23-10 ~]$ vi FIFO.C


[ec2-usereip-172-31-23-10~]$ gcc FIFO.C -o FIFO
[ec2-usereip172-31-23-101^/FIFO
Incoming Erame 1 Erame 2 Erame3
1
2
1
4
1
5 2
1
Total Page Faults.
[ec2-usereip-172-31-23-10 -1
EXPERIMENT-9
AIM: - Implementation of OPTIMAL page replacement policy.
THEORY:

The optimal page replacement algorithm is used to reduce these page faults. It uses the principle
that- "when apage is called by the system and it is not available in the frames, the frame whichn is
not in demand for the longest future time is replaced by the new page".
Advantages Of Optimal Page Replacement Algorithm:
Following are the advantages of the Optimal page replacement algorithm:
Least page fault occurs as this algorithm replaces the page that is not going to be used for
the longest time in the future.
In this algorithm, Belady's Anomaly (the number of page faults increases when we increase
the number of frames in secondary storage) does not occur because this algorithmn uses a
'stack-based algorithm' for page replacement.
Data structure used in this algorithm is easy to understand.
The page that will not be in demand in the future time is replaced by a new
page in the
frames.
Disadvantages Of Optimal Page Replacement Algorithm:
Following are the disadvantages of the optimal page replacement algorithm:
This algorithm is limited to some specific operating systems. All OS
cannot use this.
Detecting error in this algorithm is not so easy.
Sometimes,the page which isused recently is replaced which takes much time
This algorithm is difficult to implement because the
future reference strings.
operating system can't predict the

CODE: -
#include <stdio.h>

uThis function checks if currcnt strca item(key) eXISts in any of the


framesoros
int search(int key, int frame_jtemsI, int frame_0ccupied)

for (intí =0; i<frame_occupicd; it+)


key)
if(frame_items[i]==
return 1;

return 0:

void printOuterStructure(int max frames){


printf("Stream "):
for(int i=0; i< max_ frames; it+)
printf("Frame%d", i+1);
}

voidprintCurrFrames(int item, int frame items/], int frame occupied, int max_frames)?
Il print current reference stream item
printf("n%d \tlt", item);
Il print frame occupants one by one
for(int i = 0; i< max frames; it+){
if(i< frame_occupied)
printf("%d \tlt", frame_items[i]);
else

printf("- \tlt");

I/ This Function helps in finding franme that will not be used


I/ for the longest period oftime in future in ref, str[0 ..refStrLen - 11
int nredict(int ref str[], int
frame_items[1, int reStrLen, int index, int frame occupied)

I/ For each currel occupant in frame item


Il we try ofind the frame item that willnot be referenced in
for the longest in future in the upcoming refercnce string
int result = -1, farthest = index:
for(int i= 0; i<frame_occupied; it++)
int j;
for (j = index; j< refStrLen; jt+)

if (frame_items[i] == ref stril)

if (G>farthest) {
farthest=j;
result = i;

break;

|/If we find a page that is never referenced in future,


/ return it immediately as its the best
if (G == refStrLen)
return i

reference string
|/If none of the frame items appear in
index. Otherwise we return result
|/in the future then we return Oth
return (result -1) ?0 :result;

ref_str[], int refStrLen, int frame_items[], int max frames)


void optimalPage(int

occupicd
thc frames arc
1/ initially nonç of
occupicd= 0;
int fran
printOuterStructurc(max_frames);
/Here we traverse through reference string
land check for miss and hit.
int hits =0;

for (int i= 0; i< refStrLen; i-t+) {


I/ If found already in the frame items : HIT
if (search(ref _str[i], frame_items,
franme_occupied)) {
hits++;

printCurrFrames(ref_str[i], frame items, frame occupied, max_ frames);


continue;

// 1f frames are empty then current reference string item in frame


if (frame_occupied < max_frames){
frame_ items[frame occupied]=ref_str[i]:
frame_occupied++;
printCurrFrames(ref str[i], frame items, frame_occupied, max frames);

}else
i+ 1,frame occupied):
int pos = prcdict(ref str, frame_items, refStrLen,
frame items[pos] = ref_str[i];
printCurrFrames(ref_str[i], frame_items, frame_occupied, max frames):

printf("n\nHits: %d\n", hits);


hits);
printf("Misses: %d", refStrLen -

I/Driver Function

int main()
Il int ret str] =19. 0, 5,
1.0,3.0,4. 1,3.0,3. 1,3}:
int ref _str[] ={7,0, 1,2,0,3. 0, 4. 2.3.0, 3.2. 1,2.0. 1,7.0. 1}:
int refStrLen = sizeof(ref str)
/sizeot(ref str[0):
int max frames = 3:

int frame_ items[max frames;


optimalPage(ref str, retStrLen, frame items, max frames):
return 0;

OUTPUT: -

[adminajayant-baid -1$ vì optimal.c


[admindajayant-baid 1$ gcc optimal.c-o optimal
Tadmin@jayant-baid -]$/optimal
Stream Framel Frame2 Frame3

NNNN

Hits 11
Misses: 9[admin@jayantt-baid]s
EXPERIMENT 10
AIM: -Implementation of FIRST
FIT algorithm for memory
THEORY: - managenie
First-Fit Allocation is a memory allocation technigue used in
tO a proess. n First-Fit, the operating systems to allocate memoy
operating system searches through the list of free blocks of memoy,
starting from the beginning of the list. until it finds a block that is
large enough to
suitable block is found, the operatingaccomnodae
the memory request from the process. Once a
the blok into two parts: the portion that will be system spits
block. allocated to the process, and the remaining ree

Advantages of First-Fit Allocation include its simplicity and efficiency, as the search for a suitable
block oI memory can be performed quickly and easily, Additionally,
First-Fit can also help to
minimize memory fragmentation, as it tends to allocate memory in larger
blockS.
Disadvantages of First-Fit Allocation include poor performance in situations where the memory
1s highly fragmnented, as the search for a suitable block of memory can become
and inefficient. Additionally, First-Fit can also lead to poor memory utilization, astime-consuming
it may allocate
larger blocks of memory than are needed by a process.
Overall, First-Fit Allocation is a widely used memory allocation technique in operating systems,
but its effectiveness may vary depending on the specifics of the system and the workload being
executed.

CODE: -
#include<stdio.h>

void main()

allocation[1 0], i j;
int bsize[10], psize[10], bno, pno, flags[10],
for(i = 0; i< 10; it+)

flags[i] =0;
allocation[i] = -1;

blocks: "):
printf("Enlef no. of
scanf("%d", &bno);
printt"\nEnter size of cach block: "):
for(i =0; i<bno; it+)
scanf" od", &bsize[i):
printf("nEnter no. of processes: "):
scanf("%od", &pno):
printf("\nEnter size of each process: "):
tor(i = 0; i<pno; it)
scant("%d",&psize[i):
for(i=0; i< pno; it+)
llallocation as per first fit
for(j = 0;j< bno; j+)
if
flags[i]=0&& bsizei] > psize[i])
allocation[i] =i;
flags[i] = 1:
break:

lldisplay allocation details


printf("nBlock no.\tsize\t\tprocess no. t\tsize");
for(i= 0; i<bno: it)

printf("n%d\tit%ditlt", i+1, bsize[i]);


if(flags[i]=1)
else
printf("%dtitit%d", allocation[i]tl.psize[allocation[il]):
printf("Not allocated"):

You might also like