Professional Documents
Culture Documents
Operating System: Title: Lab Assessment 5 Date: 1-DEC-21
Operating System: Title: Lab Assessment 5 Date: 1-DEC-21
(CSE 2005)
………………………………………………………………………………………………
TITLE : LAB ASSESSMENT 5
DATE : 1-DEC-21
SUBMITTED BY: -
……………………………………………………………………………………………………………………………………………… ……………………………
REG.NO: 20BCE0282
SLOT: L49+L50
PSEUDO CODE: -
SOURCE CODE: -
OUTPUT: -
1. Implement a menu driven program to allocate memory by applying the following strategies.
a. FIRSTFIT
b. BESTFIT
c. WORSTFIT
PSEUDO CODE: -
}
else if (co==2)
{
low=99999;
printf("\n---------------BestFit---------------\n");
}
else if (co==3)
{
low=-1;
printf("\n---------------WorstFit---------------\n");
}
printf("\nPROCESS NO \t PROCESS SIZE \tBLOCK NO \n");
for (int i=0;i<n2;i++)
{
printf("%d \t\t %d",(i+1),ps[i]);
if (all[i]!=-1)
{
printf("\t\t%d",all[i]+1);
}
else
{
printf("\t\tNot Allocated");
}
printf("\n");
}
for (int i=0;i<n1;i++)
{
if ( bs[i]==b[i])
{
e=e+bs[i];
}
else
{
f=f+bs[i];
}
}
printf("External Fragmentation is: %d",e);
printf("\nInternal fragmentation is : %d",f);
printf("\nTotal Fragmentation is:%d\n",f+e);
}
OUTPUT: -
FIRSTFIT: -
BESTFIT: -
WORSTFIT: -
2. Implement a menu driven program for the following page replacement algorithms.
a. FIFO
b. LRU
c. OPTIMAL
PSEUDO CODE: -
• Function LRU(time[],n)
• For i=1 to n
• If(time[i]<min)
• Min=time[i]
• Pos=i
• End if
• End for
• Display(pos)
• Function ends
• Main function starts
• Get n
• For i=0 to n
• Get p[i]
• End for
• Get frames
• Declare f[frames]
• For I = 0 to n
• Declare f[i]=-1
• end for
• Disp( 1 for FIFO, 2 for LRU, 3 for OPTIMAL)
• If(input==1)
• for(i=0 to n )
• for (j=0 to frames)
• if (pages[i]==frames[j])
• S<-s+1
• Faults<-faults-1
• End if
• End for
• Faults+=1;
• if faults<=no_of_frames and s==0
• frames[(faults-1)% frames]<-pages[i];
• End if
• else if s==0
• f [(faults-1)% frames]<-page[i];
• End if
• for j=0 to frames
• If frames[j]!=-1
• Disp(f[j])
• End for
• End for
• End if
• If(input==2)
• For i = 0 to pages
• For j = 0 to frames
• If frames[j] == pages[i]
• counter++
• time[j] <- counter
• flag1 and flag2 = 1
• End if
• End for
• If flag1 == 0
• For j = 0 to frames
• If f [j] == -1
• counter++,faults++
• frames[j] <- pages[i], time[j] <- counter
• flag2 <- 1
• End if
• End for
• End if
• If flag2 == 0
• pos = function call LRU(time, frames)
• counter++,faults++
• frames[pos] <- page[i],time[pos] <- c
• End if
• For j = 0 to frames
• if frames[j]!=-1
• Disp( frames[j])
• End if
• End for
• End for
• End if
• If input==3
• For i = 0 to pages
• flag1 and flag2 <- 0
• For j = 0 to frames
• If f [j] == pages[i])
• flag1 and flag2 <- 1
• End if
• End for
• If flag1 == 0
• For j = 0 to frames
• if(frames[j] == -1)
• faults++
• f [j] <- page[i], flag2 = 1
• End if
• End for
• End if
• If flag2 == 0
• flag3 <-0
• For j = 0 to frames
• temp[j] <- -1
• For k = i + 1 to n
• If f [j] == page[k]
• temp[j] = k
• End if
• End for
• End for
• For j = 0 to frames
• If temp[j] == -1
• pos <- j,flag3 ,- 1
• End if
• End for
• If flag3 ==0
• max <- temp[0], pos <- 0
• For j = 1 to frames
• If temp[j] > max
• max <- temp[j], pos = j
• End if
• End for
• End if
• f [pos] = page[i],faults++
• End if
• For j = 0 to frames
• if frames[j]! <- -1
• Disp( f [j])
• End for
• End if
• End for
• End if
• q=pagemiss
• w=Total miss
• Q1=q/10
• W1=w/10
• Disp(q,w,q1,w1)
• Main function ends
SOURCE CODE: -
#include<stdio.h>
int LRU(int time[], int n)
{
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i){
if(time[i] < minimum)
{
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10],temp[10],max, pages[30], counter = 0,
time[10], flag1, flag2,flag3, i,j,k,pos, faults = 0,c=0,s;
printf("Enter The Total Number of Pages: ");
scanf("%d", &no_of_pages);
for(i = 0; i < no_of_pages; ++i)
{
printf("Enter The Value of %d:",(i+1));
scanf("%d", &pages[i]);
}
printf("Enter The Total Number of Frames: ");
scanf("%d", &no_of_frames);
for(i = 0; i < no_of_frames; ++i)
{
frames[i] = -1;
}
printf("\nEnter 1 for FIFO");
printf("\nEnter 2 for LRU");
printf("\nEnter 3 for OPTIMAL");
printf("\n\nEnter your choice :") ;
scanf("%d",&c);
if (c==1)
{
printf("----------FIFO----------");
for(int i=0;i<no_of_pages;i++)
{
s=0;
for (int j=0;j<no_of_frames;j++)
{
if (pages[i]==frames[j])
{
s=s+1;
faults=faults-1;
}
}
faults=faults+1;
if ((faults<=no_of_frames)&&(s==0))
{
frames[(faults-1)%no_of_frames]=pages[i];
}
else if(s==0)
{
frames[(faults-1)%no_of_frames]=pages[i];
}
printf("\n");
for (int j=0;j<no_of_frames;j++)
{
if(frames[j]!=-1)
printf("%d\t",frames[j]);
}
}
}
else if (c==2)
{
printf("----------LRU----------");
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i])
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
pos = LRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
{
if (frames[j]!=-1)
printf("%d\t", frames[j]);
}
}
}
else if (c==3)
{
printf("----------OPTIMAL----------");
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i])
{
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
flag3 =0;
for(j = 0; j < no_of_frames; ++j)
{
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k)
{
if(frames[j] == pages[k])
{
temp[j] = k;
break;
}
}
}
}
int miss=faults;
float q=(float)(no_of_pages-miss);
q=q/10;
float w=(float)miss;
w=w/10;
printf("\nThe Total hits are :%d",(no_of_pages-miss));
printf("\nThe Total miss are :%d",miss);
printf("\nThe Hit ratio is :%f",q);
printf("\nThe Miss ratio is :%f",w);
printf("\n");
return 0;
}
OUTPUT: -
FIFO: -
LRU: -
OPTIMAL: -
3. Implement a menu driven program for the following disk scheduling algorithms.
a. FCFS
b. SSTF
c. SCAN
d. C-SCAN
e. LOOK
f. C-LOOK
PSEUDO CODE: -
while (c!=n)
{
int min=999;
for (int i=0;i<n;i++)
{
dist=abs(arr[i]-head);
if (dist<min)
{
curr=i;
min=dist;
}
}
seek_c=seek_c+min;
head=arr[curr];
seek_seq[c]=arr[curr];
arr[curr]=1000;
c=c+1;
}
printf("The Total Value of Seek Count is :%d",seek_c);
printf("\nTHe sequence is");
for (int i=0;i<n;i++)
{
printf(" %d",seek_seq[i]);
}
printf("\n");
}
void scan(int arr[],int head)
{
int seek_c=0,dist,temp;
int curr,z=0,c=0,seek_seq[n];
for (int i=0;i<n;i++)
{
for (int j=i;j<n;j++)
{
if (arr[j]<arr[i])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for (int i=0;i<n-1;i++)
{
if (arr[i]<head)
{
if (arr[i+1]>head)
{
z=i+1;
}
}
}
for (int i=z;i<n;i++)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
seek_c=seek_c+(u-head);
head=u;
for (int i=z-1;i>-1;i--)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
printf("The Total Value of Seek Count is :%d",seek_c);
printf("\nThe sequence is");
for (int i=0;i<n;i++)
{
printf(" %d",seek_seq[i]);
}
printf("\n");
}
void cscan(int arr[],int head)
{
int seek_c=0,dist,temp;
int curr,z=0,c=0,seek_seq[n];
for (int i=0;i<n;i++)
{
for (int j=i;j<n;j++)
{
if (arr[j]<arr[i])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for (int i=0;i<n-1;i++)
{
if (arr[i]<head)
{
if (arr[i+1]>head)
{
z=i+1;
}
}
}
for (int i=z;i<n;i++)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
dist=abs(head-u);
seek_c=dist+seek_c;
seek_c=seek_c+(u-l);
head=l;
for (int i=0;i<z;i++)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
printf("The Total Value of Seek Count is :%d",seek_c);
printf("\nThe sequence is");
for (int i=0;i<n;i++)
{
printf(" %d",seek_seq[i]);
}
printf("\n");
}
void look(int arr[],int head)
{
int seek_c=0,dist,temp;
int curr,z=0,c=0,seek_seq[n];
for (int i=0;i<n;i++)
{
for (int j=i;j<n;j++)
{
if (arr[j]<arr[i])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for (int i=0;i<n-1;i++)
{
if (arr[i]<head)
{
if (arr[i+1]>head)
{
z=i+1;
}
}
}
for (int i=z;i<n;i++)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
for (int i=z-1;i>-1;i--)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
printf("The Total Value of Seek Count is :%d",seek_c);
printf("\nThe sequence is");
for (int i=0;i<n;i++)
{
printf(" %d",seek_seq[i]);
}
printf("\n");
}
void clook(int arr[],int head)
{
int seek_c=0,dist,temp;
int curr,z=0,c=0,seek_seq[n];
for (int i=0;i<n;i++)
{
for (int j=i;j<n;j++)
{
if (arr[j]<arr[i])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for (int i=0;i<n-1;i++)
{
if (arr[i]<head)
{
if (arr[i+1]>head)
{
z=i+1;
}
}
}
for (int i=z;i<n;i++)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
for (int i=0;i<z;i++)
{
dist=abs(head-arr[i]);
seek_c=seek_c+dist;
head=arr[i];
seek_seq[c]=arr[i];
c=c+1;
}
printf("The Total Value of Seek Count is :%d",seek_c);
printf("\nThe sequence is");
for (int i=0;i<n;i++)
{
printf(" %d",seek_seq[i]);
}
printf("\n");
}
int main()
{
printf("Enter The Total Number of Sequences:");
scanf("%d",&n);
int ar[n],co=0;
for (int i=0;i<n;i++)
{
printf("Enter The Value of Sequence %d:",(i+1));
scanf("%d",&ar[i]);
}
int h;
printf("Enter The Value of The Head:");
scanf("%d",&h);
printf("Enter The Value of The Lower Bound:");
scanf("%d",&l);
printf("Enter The value of The Upper Bound:");
scanf("%d",&u);
printf("\n\n MENU");
printf("\nEnter 1 if FCFS");
printf("\nEnter 2 if SSTF");
printf("\nEnter 3 if SCAN");
printf("\nEnter 4 if C-SCAN");
printf("\nEnter 5 if LOOK");
printf("\nEnter 6 if C-LOOK");
printf("\n\nEnter Your choice :");
scanf("%d",&co);
switch (co)
{
case 1:
{
printf("\n---------------FCFS---------------\n");
fcfs(ar,h);
break;
}
case 2:
{
printf("\n---------------SSTF---------------\n");
sstf(ar,h);
break;
}
case 3:
{
printf("\n---------------SCAN---------------\n");
scan(ar,h);
break;
}
case 4:
{
printf("\n---------------C-SCAN---------------\n");
cscan(ar,h);
break;
}
case 5:
{
printf("\n---------------LOOK---------------\n");
look(ar,h);
break;
}
case 6:
{
printf("\n---------------C-LOOK---------------\n");
clook(ar,h);
break;
}
default:
{
printf("\n---------------ERROR---------------\n");
printf("\nEnter the correct choice");
}
}
}
OUTPUT: -
FCFS : -
SSTF: -
SCAN: -
C-SCAN: -
LOOK: -
C-LOOK: -