Os 2
Os 2
Priority
Scheduling
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.
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.
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
Ready State
Tg
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.
53. i-0;
54.
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
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);
scanf("%d", &pages[i]);
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)
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);
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 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;
S=0:
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");
OUTPUT: -
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>
return 0:
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");
if (G>farthest) {
farthest=j;
result = i;
break;
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;
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;
}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):
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:
OUTPUT: -
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: