You are on page 1of 40

OPERATING SYSTEM

(CSE 2005)
………………………………………………………………………………………………
TITLE : LAB ASSESSMENT 5

DATE : 1-DEC-21

SUBMITTED BY: -
……………………………………………………………………………………………………………………………………………… ……………………………

NAME: DEVA DHARSHAN D

FACULTY: MRS. SANTHI H

YEAR : 2-ND YEAR(BTECH)

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: -

• Main function starts


• Declare all[10],allo[10]
• For I =0 to 10
• All[i] and allo[i]<- 1,
• End for
• Get blocks
• For I = 0 to blocks
• Get b[i]
• End for
• Get process
• For I = 0 to process
• Get p[i]
• End for
• Display ( 1 for First fit, 2 for Best fit, 3 for Worst fit)
• If input==1
• for i=0 to process
• for j=0 to blocks
• if (bs[j]>=ps[i])
• all[i]<-j,bs[j]<-bs[j]-ps[i]
• break
• End if
• End for
• End for
• End if
• If input==2
• Low<-999
• for i=0 to process
• for j=0 to blocks
• if bs[j]>=ps[i]
• Temp<-bs[j]-ps[i]
• if (low>temp)
• Low<-temp, all[i]=j
• End if
• End if
• End for
• Low<-999, allo[all[i]]<-1
• bs[all[i]]<-bs[all[i]]-ps[i]
• End for
• End if
• If input==3
• low=-1;
• for i=0 to process
• for j=0 to blocks
• if bs[j] >= ps[i]
• Temp<-bs[j]-ps[i]
• if (low<temp)
• Low<-temp, all[i]<-j
• End if
• End if
• End for
• Low<-low-1
• allo[all[i]]<-1 and bs[all[i]]<-bs[all[i]]-ps[i];
• End for
• End if
• for I = 0 to process
• Display(i+1,ps[i])
• if (all[i]!=-1)
• Display(all[i]+1)
• End if
• else
• Display(Not allocated)
• End else
• End for
• for I = 0 to blocks
• if bs[i]==b[i]
• e<-e+bs[i]
• End if
• else
• f<-f+bs[i]
• End else
• End for
• Display(e,f,e+f)
• Main function ends
SOURCE CODE: -
#include<stdio.h>
int main()
{
int n1,n2,bs[10],ps[10],all[10],allo[10],c=-1,b[10],e=0,f=0,co=0,low,temp;
for (int i=0;i<10;i++)
{
all[i]=-1;
allo[i]=-1;
}
printf("Enter The Total No of Blocks :");
scanf("%d",&n1);
for (int i=0;i<n1;i++)
{
printf("Enter The Size of %d :",(i+1));
scanf("%d",&bs[i]);
b[i]=bs[i];
}
printf("Enter The Total No of Process :");
scanf("%d",&n2);
for (int i=0;i<n2;i++)
{
printf("Enter The size of %d :",(i+1));
scanf("%d",&ps[i]);
}
printf(" Menu\n");
printf("Enter 1 if Firstfit");
printf("\nEnter 2 if BestFit");
printf("\nEnter 3 if WorstFit");
printf("\nEnter the value:");
scanf("%d",&co);
if (co==1)
{
printf("\n---------------FirstFit---------------\n");

for (int i=0;i<n2;i++)


{
for (int j=0;j<n1;j++)
{
if (bs[j]>=ps[i])
{
all[i]=j;
bs[j]=bs[j]-ps[i];
break;
}
}
}

}
else if (co==2)
{
low=99999;
printf("\n---------------BestFit---------------\n");

for (int i=0;i<n2;i++)


{
for (int j=0;j<n1;j++)
{
if ((bs[j]>=ps[i]))
{
temp=bs[j]-ps[i];
if (low>temp)
{
low=temp;
all[i]=j;
}
}
}
low=99999;
allo[all[i]]=1;
bs[all[i]]=bs[all[i]]-ps[i];
}

}
else if (co==3)
{
low=-1;
printf("\n---------------WorstFit---------------\n");

for (int i=0;i<n2;i++)


{
for (int j=0;j<n1;j++)
{
if ((bs[j]>=ps[i]))
{
temp=bs[j]-ps[i];
if (low<temp)
{
low=temp;
all[i]=j;
}
}
}
low=-1;
allo[all[i]]=1;
bs[all[i]]=bs[all[i]]-ps[i];
}

}
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;
}

}
}

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


{
if(temp[j] == -1)
{
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0)
{
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
{
if (frames[j]!=-1)
printf("%d\t", frames[j]);
}
}

}
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: -

• Function FCFS(arr[], head)


• int seek_c=0;
• for I = 0 to n
• Curr<-arr[i],dist<-abs(curr-head)
• seek_c<-seek_c+dist
• Head<-curr
• End for
• Display(seek_c)
• for I = 0 to n
• Display(arr[i])
• End for
• End FCFS()
• Function SSTF(arr[], head)
• while (c!=n)
• min=999
• For I = 0 to n
• dist=abs(arr[i]-head);
• if dist<min
• Curr<-I,min<-dist
• End if
• End for
• seek_c<-seek_c+min
• Head<-arr[curr],seek_seq[c]<-arr[curr]
• arr[curr]<-1000,c<-c+1
• End while
• Display(seek_c)
• For I = 0 to n
• Display(seek_seq[i])
• End for
• End SSTF()
• Function SCAN(arr[],head)
• for I = 0 to n
• for j = 0 to n
• if arr[j]<arr[i]
• Swap(arr[i], arr[j])
• End if
• End for
• End for
• for I = 0 to n
• if arr[i]<head
• if arr[i+1]>head
• declare z<-i+1
• End if
• End if
• End for
• for I = z to n
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• head<-arr[i], seek_seq[c] <-arr[i],c<-c+1
• End for
• seek_c<-seek_c+(u-head), head<-u
• for i=z-1 to 0
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• Head<-arr[i],seek_seq[c]<-arr[i],c<-c+1
• End for
• Display(seek_c)
• For I = 0 to n
• Display(seek_seq[i])
• End for
• End SCAN()
• Function CSCAN(arr[],head)
• for I = 0 to n
• for j = 0 to n
• if arr[j]<arr[i]
• Swap(arr[i], arr[j])
• End if
• End for
• End for
• for I = 0 to n
• if arr[i]<head
• if arr[i+1]>head
• declare z<-i+1
• End if
• End if
• End for
• for I = z to n
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• head<-arr[i], seek_seq[c] <-arr[i],c<-c+1
• End for
• dist=abs(head-u),seek_c=dist+seek_c
• seek_c<-seek_c+(u-l),head<-l
• for I = 0 to z
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• Head<-arr[i],seek_seq[c]<-arr[i],c<-c+1
• End for
• Display(seek_c)
• for I = 0 to n
• Display(seek_c)
• Display(seek_seq[i])
• End for
• End CSCAN()
• Function LOOK(arr[], head)
• for I = 0 to n
• for j = 0 to n
• if arr[j]<arr[i]
• Swap(arr[i], arr[j])
• End if
• End for
• End for
• for I = 0 to n
• if arr[i]<head
• if arr[i+1]>head
• declare z<-i+1
• End if
• End if
• End for
• for I = z to n
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• head<-arr[i], seek_seq[c] <-arr[i],c<-c+1
• End for
• for i=z-1 to 0
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• Head<-arr[i],seek_seq[c]<-arr[i],c<-c+1
• End for
• Display(seek_c)
• For I = 0 to n
• Display(seek_seq[i])
• End for
• End LOOK()
• Function CLOOK(arr[],head)
• for I = 0 to n
• for j = 0 to n
• if arr[j]<arr[i]
• Swap(arr[i], arr[j])
• End if
• End for
• End for
• for I = 0 to n
• if arr[i]<head
• if arr[i+1]>head
• declare z<-i+1
• End if
• End if
• End for
• for I = z to n
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• head<-arr[i], seek_seq[c] <-arr[i],c<-c+1
• End for
• for I = 0 to z
• Dist<-abs(head-arr[i]),seek_c<-seek_c+dist
• Head<-arr[i],seek_seq[c]<-arr[i],c<-c+1
• End for
• Display(seek_c)
• for I = 0 to n
• Display(seek_seq[i])
• End for
• End CLOOK()
• Main function starts
• Get sequences,n
• For I = 0 to n
• Get arr[i]
• End for
• Get head
• Get LowerBound
• Get Upperbound
• Display( 1 for FCFS, 2 for SSTF, 3 for SCAN, 4 for CSCAN, 5 for LOOK, 6 for CLOOK)
• Get option
• Switch option:
• Case 1
• Function call FCFS
• Case 2
• Function call SSTF
• Case 3
• Function call SCAN
• Case 4
• Function call CSCAN
• Case 5
• Function call LOOK
• Case 6
• Function call CLOOK
• End switch
• Main function ends
SOURCE CODE: -
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n,l,u;
void fcfs(int arr[],int head)
{
int seek_c=0;
int dist,curr;
for (int i=0;i<n;i++)
{
curr=arr[i];
dist=abs(curr-head);
seek_c=seek_c+dist;
head=curr;
}
printf("The Total Value of Seek Count is :%d \n",seek_c);
printf("\nThe sequence is");
for (int i=0;i<n;i++)
{
printf(" %d",arr[i]);
}
printf("\n");
}
void sstf(int arr[],int head)
{
int seek_c=0,dist;
int seek_seq[n],curr,c=0;

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: -

You might also like