Os Lab (2) 3
Os Lab (2) 3
a) Worst fit
#include <stdio.h>
void implimentWorstFit(int blockSize[], int blocks, int processSize[], int processes)
{
// This will store the block id of the allocated block to a process
int allocation[processes];
int occupied[blocks];
// Driver code
int main() {
int blockSize[] = {100, 50, 30, 120, 35}; int
processSize[] = {40, 10, 30, 60}; int blocks =
sizeof(blockSize)/sizeof(blockSize[0]); int processes =
sizeof(processSize)/sizeof(processSize[0]);
return 0;
}
b) Best fit
#include <stdio.h>
void implimentBestFit(int blockSize[], int blocks, int processSize[], int proccesses)
{
// This will store the block id of the allocated block to a process
int allocation[proccesses];
int occupied[blocks];
// Driver code
int main() {
int blockSize[] = {100, 50, 30, 120, 35}; int
processSize[] = {40, 10, 30, 60}; int blocks =
sizeof(blockSize)/sizeof(blockSize[0]);
int proccesses = sizeof(processSize)/sizeof(processSize[0]);
return 0 ;
}
c) First fit.
break;
}
}
}
void main()
{
int blockSize[] = {30, 5, 10}; int processSize[]
= {10, 6, 9}; int m =
sizeof(blockSize)/sizeof(blockSize[0]); int n =
sizeof(processSize)/sizeof(processSize[0]);
FIFO:
#include<stdio.h>
int main()
{
//int incomingStream[] = {4,1,2,4,5};
int incomingStream[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf(" Incoming \t Frame 1 \t\t Frame 2 \t\t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
LRU:
#include<stdio.h>
#include<limits.h>
return 0;
}
int main()
{
int incomingStream[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
// int incomingStream[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1};
// int incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3, 6, 1, 2, 4, 3};
// int incomingStream[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3};
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printFrame(queue, occupied);
}
else{
if(queue[j] == incomingStream[k])
break;
}
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct {
char dname[10],fname[10][10];
int fcnt; }dir; void main()
{ int i,ch;
char f[30];
//clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1) {
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter
your choice -- "); scanf("%d",&ch); switch(ch) {
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]); dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f); for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]); break;
}
}
if(i==dir.fcnt) printf("File
%s not found",f);
else dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f); for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break; } } if(i==dir.fcnt)
printf("File %s not found",f);
break; case 4: if(dir.fcnt==0)
printf("\n Directory
Empty"); else {
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++) printf("\t%s",dir.fname[i]);
} break;
default: exit(0);
}}
getch()
;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct {
char dname[10],fname[10][10];
int fcnt; }dir[10]; void main() {
int i,ch,dcnt,k; char f[30],
d[30];
//clrscr();
dcnt=0; while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch); switch(ch) {
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0; dcnt++;
printf("Directory created"); break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d); for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt])
; dir[i].fcnt++; printf("File
created"); break; } if(i==dcnt)
printf("Directory %s not found",d); break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d); for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]); goto
jmp;
}}
printf("File %s not found",f); goto
jmp;
}}
printf("Directory %s not found",d); jmp
: break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d); for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f); goto
jmp1;
}}
printf("File %s not found",f); goto
jmp1;
}}
printf("Directory %s not found",d);
jmp1: break; case 5: if(dcnt==0)
printf("\nNo Directory's "); else {
printf("\nDirectory\tFiles"); for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++) printf("\t%s",dir[i].fname[k]);
} } break;
default:exit(0);
}}
getch()
;
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h
> void main() {
int f[50], p,i, st, len, j, c, k, a;
//clrscr(); for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: "); scanf("%d",&p);
printf("Enter blocks already allocated: "); for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1; }
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len; if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{ if(f[j]==0)
{ f[j]=1; printf("%d--------
>%d\n",j,f[j]);
} else
{
printf("%d Block is already allocated \n",j); k++;
}
}}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x; else exit(0); getch();
}
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}