0% found this document useful (0 votes)
12 views13 pages

Os Lab (2) 3

The document provides C programs to simulate various memory allocation techniques (Worst Fit, Best Fit, First Fit), page replacement algorithms (FIFO, LRU), and file organization techniques (Single Level Directory, Two Level Directory). Each section includes code implementations that demonstrate how to allocate memory, handle page faults, and manage files within directories. The programs are structured with functions to perform specific tasks related to memory and file management.

Uploaded by

jayadev.nishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Os Lab (2) 3

The document provides C programs to simulate various memory allocation techniques (Worst Fit, Best Fit, First Fit), page replacement algorithms (FIFO, LRU), and file organization techniques (Single Level Directory, Two Level Directory). Each section includes code implementations that demonstrate how to allocate memory, handle page faults, and manage files within directories. The programs are structured with functions to perform specific tasks related to memory and file management.

Uploaded by

jayadev.nishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

6.

Develop a C program to simulate the following contiguous memory allocation


Techniques: a) Worst fit b) Best fit c) First fit.

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

// initially assigning -1 to all allocation indexes


// means nothing is allocated currently for(int i
= 0; i < processes; i++){
allocation[i] = -1;
}

for(int i = 0; i < blocks; i++){


occupied[i] = 0;
}
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i=0; i < processes; i++)
{
int indexPlaced = -1;
for(int j = 0; j < blocks; j++)
{
// if not occupied and block size is large enough
if(blockSize[j] >= processSize[i] && !occupied[j])
{
// place it at the first block fit to accomodate process
if (indexPlaced == -1)
indexPlaced = j;

// if any future block is larger than the current block where


// process is placed, change the block and thus indexPlaced
else if (blockSize[indexPlaced] < blockSize[j])
indexPlaced = j;
}
}
// If we were successfully able to find block for the process
if (indexPlaced != -1)
{
// allocate this block j to process p[i]
allocation[i] = indexPlaced;

// make the status of the block as occupied


occupied[indexPlaced] = 1;

// Reduce available memory for the block


blockSize[indexPlaced] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < processes; i++)
{ printf("%d \t\t\t %d \t\t\t", i+1,
processSize[i]); if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1); else
printf("Not Allocated\n");
}}

// 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]);

implimentWorstFit(blockSize, blocks, processSize, processes);

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

// initially assigning -1 to all allocation indexes


// means nothing is allocated currently for(int i
= 0; i < proccesses; i++){
allocation[i] = -1;
}

for(int i = 0; i < blocks; i++){


occupied[i] = 0;
}
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i = 0; i < proccesses; i++)
{
int indexPlaced = -1; for
(int j = 0; j < blocks; j++) {
if (blockSize[j] >= processSize[i]
&& !occupied[j])
{
// place it at the first block fit to accomodate process
if (indexPlaced == -1)
indexPlaced = j;

// if any future block is smalller than the current block where


// process is placed, change the block and thus indexPlaced
// this reduces the wastage thus best fit
else if (blockSize[j] < blockSize[indexPlaced])
indexPlaced = j;
}
}

// If we were successfully able to find block for the process


if (indexPlaced != -1)
{
// allocate this block j to process p[i]
allocation[i] = indexPlaced;

// make the status of the block as occupied


occupied[indexPlaced] = 1;
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < proccesses; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1); else
printf("Not Allocated\n");
}}

// 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]);

implimentBestFit(blockSize, blocks, processSize, proccesses);

return 0 ;
}

c) First fit.

#include <stdio.h> void implimentFirstFit(int blockSize[], int blocks, int


processSize[], int processes)
{
// This will store the block id of the allocated block to a process
int allocate[processes];
int occupied[blocks];

// initially assigning -1 to all allocation indexes


// means nothing is allocated currently
for(int i = 0; i < processes; i++)
{
allocate[i] = -1;
}

for(int i = 0; i < blocks; i++){


occupied[i] = 0;
}

// take each process one by one and find


// first block that can accomodate it
for (int i = 0; i < processes; i++)
{
for (int j = 0; j < blocks; j++)
{
if (!occupied[j] && blockSize[j] >= processSize[i])
{
// allocate block j to p[i] process
allocate[i] = j;
occupied[j] = 1;

break;
}
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < processes; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocate[i] != -1)
printf("%d\n",allocate[i] + 1); else
printf("Not Allocated\n");
}
}

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]);

implimentFirstFit(blockSize, m, processSize, n);


}
7. Develop a C program to simulate page replacement algorithms:
a) FIFO b) LRU

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>

int checkHit(int incomingPage, int queue[], int occupied){

for(int i = 0; i < occupied; i++){


if(incomingPage == queue[i])
return 1;
}

return 0;
}

void printFrame(int queue[], int occupied)


{
for(int i = 0; i < occupied; i++)
printf("%d\t\t\t",queue[i]);
}

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;

printf("Page\t Frame1 \t\t Frame2 \t\t Frame3\n");

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


{
printf("%d: \t\t",incomingStream[i]);
// what if currently in frame 7
// next item that appears also 7
// didnt write condition for HIT

if(checkHit(incomingStream[i], queue, occupied)){


printFrame(queue, occupied);
}

// filling when frame(s) is/are empty


else if(occupied < frames){
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;

printFrame(queue, occupied);
}
else{

int max = INT_MIN;


int index;
// get LRU distance for each item in frame
for (int j = 0; j < frames; j++)
{
distance[j] = 0;
// traverse in reverse direction to find
// at what distance frame item occurred last
for(int k = i - 1; k >= 0; k--)
{
++distance[j];

if(queue[j] == incomingStream[k])
break;
}

// find frame item with max distance for LRU


// also notes the index of frame item in queue
// which appears furthest(max distance)
if(distance[j] > max){
max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied);
pagefault++;
}

printf("\n");
}

printf("Page Fault: %d",pagefault);

return 0;
}

8. Simulate following File Organization Techniques

a) Single level directory

#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()
;
}

b) Two level directory

#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()
;
}

9. Develop a C program to simulate the Linked file allocation strategies.

#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();
}

10. Develop a C program to simulate SCAN disk scheduling algorithm


#include<stdio.h>
#include<math.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;

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

You might also like