0% found this document useful (0 votes)
2 views20 pages

OS Programs Remaining

The document provides implementations for three memory allocation methods (First Fit, Best Fit, and Worst Fit) and three page replacement algorithms (FIFO, LRU, LFU) in C programming. Each section includes code examples, input prompts, and expected output formats. The document also briefly discusses the paging technique of memory management.

Uploaded by

Devabn Nirmal
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)
2 views20 pages

OS Programs Remaining

The document provides implementations for three memory allocation methods (First Fit, Best Fit, and Worst Fit) and three page replacement algorithms (FIFO, LRU, LFU) in C programming. Each section includes code examples, input prompts, and expected output formats. The document also briefly discusses the paging technique of memory management.

Uploaded by

Devabn Nirmal
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/ 20

Implement the following memory allocation methods for fixed partition

a) First fit b) Worst fit c) Best fit


First Fit Program
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp; bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

Output:
Memory Management Scheme - First Fit
Enter the number of blocks:3
Enter the number of files:2

Enter the size of the blocks:-


Block 1:5
Block 2:2
Block 3:7
Enter the size of the files :-
File 1:1
File 2:4
File_no: File_size : Block_no: Block_size: Fragement
1 1 1 5 4
2 4 3 7 3

Best Fit:
#include<stdio.h>
#define max 25

void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme – Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
Output:

Memory Management Scheme – Best Fit


Enter the number of blocks:3
Enter the number of files:2

Enter the size of the blocks:-


Block 1:5
Block 2:2
Block 3:7
Enter the size of the files :-
File 1:1
File 2:4

File No File Size Block No Block Size Fragment


1 1 2 2 1
2 4 1 5 1

Worst Fit:
#include <stdio.h>

void implimentWorstFit(int blockSize[], int blocks, int processSize[], int processes)


{
int fragment[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 = 1; i <= processes; i++){
allocation[i] = -1;
}

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


occupied[i] = 0;
}

// pick each process and find suitable blocks


// according to its size ad assign to it
for (int i=1; i <= processes; i++)
{
int indexPlaced = -1;
for(int j = 1; 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 accommodate 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


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

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


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

}
}

// Driver code
int main()
{
int blockSize[20], processSize[20], blocks, processes, i, j;
printf("\nEnter the number of blocks:");
scanf("%d",&blocks);
printf("Enter the number of files:");
scanf("%d",&processes);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=blocks;i++)
{
printf("Block %d:",i);
scanf("%d",&blockSize[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=processes;i++)
{
printf("File %d:",i);
scanf("%d",&processSize[i]);
}
implimentWorstFit(blockSize, blocks, processSize, processes);

return 0;
}

Output:
Enter the size of the blocks:-
Block 1:5
Block 2:2
Block 3:7
Enter the size of the files :-
File 1:1
File 2:4

Process No Process Size Block No Fragment


1 1 3 6
2 4 1 1
Simulate the following page replacement algorithms
a) FIFO b) LRU c) LFU
a) FIFO
#include <stdio.h>

int main() {
int frames, pages[100], temp[100];
int totalPages, i, j, k, pageFaults = 0, pos = 0, flag;

printf("Enter number of frames: ");


scanf("%d", &frames);

printf("Enter number of pages: ");


scanf("%d", &totalPages);

printf("Enter the page reference string: ");


for (i = 0; i < totalPages; i++) {
scanf("%d", &pages[i]);
}

for (i = 0; i < frames; i++) {


temp[i] = -1; // initialize frames as empty
}

printf("\nPage\tFrames\n");
for (i = 0; i < totalPages; i++) {
flag = 0;

// Check if page is already in frame


for (j = 0; j < frames; j++) {
if (temp[j] == pages[i]) {
flag = 1;
break;
}
}

// If page not found in frame (page fault)


if (flag == 0) {
temp[pos] = pages[i];
pos = (pos + 1) % frames;
pageFaults++;

// Print the frames


printf("%d\t", pages[i]);
for (k = 0; k < frames; k++) {
if (temp[k] != -1)
printf("%d ", temp[k]);
else
printf("- ");
}
printf("\n");
} else {
// If page hit, just print
printf("%d\tHit\n", pages[i]);
}
}

printf("\nTotal Page Faults = %d\n", pageFaults);


return 0;
}
Output:
Output:
Enter number of frames: 3
Enter number of pages: 11
Enter the page reference string: 2 3 2 1 5 2 4 5 2 3 2

Page Frames
2 2--
3 23-
2 Hit
1 231
5 531
2 521
4 524
5 Hit
2 Hit
3 324
2 Hit

Total Page Faults = 7


b) LRU
#include <stdio.h>

#define MAX_FRAMES 10
#define MAX_PAGES 100

int findLRU(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 frames[MAX_FRAMES], pages[MAX_PAGES], time[MAX_FRAMES];
int total_pages, total_frames, count = 0, faults = 0;
int i, j, flag1, flag2, pos;

printf("Enter number of frames: ");


scanf("%d", &total_frames);

printf("Enter number of pages: ");


scanf("%d", &total_pages);

printf("Enter the page reference string:\n");


for(i = 0; i < total_pages; ++i) {
scanf("%d", &pages[i]);
}

for(i = 0; i < total_frames; ++i) {


frames[i] = -1;
}

for(i = 0; i < total_pages; ++i) {


flag1 = flag2 = 0;

// Check if page is already in frames


for(j = 0; j < total_frames; ++j) {
if(frames[j] == pages[i]) {
count++;
time[j] = count;
flag1 = flag2 = 1;
break;
}
}

// If page not in frames


if(flag1 == 0) {
for(j = 0; j < total_frames; ++j) {
if(frames[j] == -1) {
count++;
faults++;
frames[j] = pages[i];
time[j] = count;
flag2 = 1;
break;
}
}
}

// Replace LRU page


if(flag2 == 0) {
pos = findLRU(time, total_frames);
count++;
faults++;
frames[pos] = pages[i];
time[pos] = count;
}

// Print current frame status


printf("Frames: ");
for(j = 0; j < total_frames; ++j) {
if(frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
printf("\n");
}

printf("\nTotal Page Faults = %d\n", faults);

return 0;
}
Output:
Enter number of frames: 3
Enter number of pages: 11
Enter the page reference string:
23215245232
Frames: 2 - -
Frames: 2 3 -
Frames: 2 3 -
Frames: 2 3 1
Frames: 2 5 1
Frames: 2 5 1
Frames: 2 5 4
Frames: 2 5 4
Frames: 2 5 4
Frames: 2 5 3
Frames: 2 5 3

Total Page Faults = 6

c) LFU
#include <stdio.h>
#include <limits.h>

#define MAX_FRAMES 10
#define MAX_PAGES 100

// Structure to hold page information


typedef struct {
int page;
int frequency;
int time; // To break ties using FIFO
} Frame;

int main() {
int pages[MAX_PAGES], n, framesCount;
Frame frames[MAX_FRAMES];
int time = 0, faults = 0;

// Input
printf("Enter number of pages: ");
scanf("%d", &n);
printf("Enter the page reference string: ");
for (int i = 0; i < n; i++) {
scanf("%d", &pages[i]);
}
printf("Enter number of frames: ");
scanf("%d", &framesCount);

// Initialize frames
for (int i = 0; i < framesCount; i++) {
frames[i].page = -1;
frames[i].frequency = 0;
frames[i].time = 0;
}

// LFU logic
for (int i = 0; i < n; i++) {
int currentPage = pages[i];
int found = 0;

// Check if page is already in frame


for (int j = 0; j < framesCount; j++) {
if (frames[j].page == currentPage) {
frames[j].frequency++;
frames[j].time = time++;
found = 1;
break;
}
}

// If not found, page fault


if (!found) {
faults++;

int minFreq = INT_MAX, oldestTime = INT_MAX, replaceIndex = -1;

// Find LFU page


for (int j = 0; j < framesCount; j++) {
if (frames[j].page == -1) {
replaceIndex = j;
break;
}
if (frames[j].frequency < minFreq ||
(frames[j].frequency == minFreq && frames[j].time < oldestTime)) {
minFreq = frames[j].frequency;
oldestTime = frames[j].time;
replaceIndex = j;
}
}

// Replace the page


frames[replaceIndex].page = currentPage;
frames[replaceIndex].frequency = 1;
frames[replaceIndex].time = time++;
}

// Print current frame state


printf("After accessing page %d: ", currentPage);
for (int j = 0; j < framesCount; j++) {
if (frames[j].page != -1)
printf("%d ", frames[j].page);
else
printf("- ");
}
printf("\n");
}

printf("\nTotal Page Faults: %d\n", faults);


return 0;
}
INPUT
Enter number of pages: 11
Enter the page reference string: 2 3 2 1 5 2 4 5 2 3 2
Enter number of frames: 3
After accessing page 2: 2 - -
After accessing page 3: 2 3 -
After accessing page 2: 2 3 -
After accessing page 1: 2 3 1
After accessing page 5: 2 5 1
After accessing page 2: 2 5 1
After accessing page 4: 2 5 4
After accessing page 5: 2 5 4
After accessing page 2: 2 5 4
After accessing page 3: 2 5 3
After accessing page 2: 2 5 3

Total Page Faults: 6

7. Simulate Paging Technique of memory management.


#include<stdio.h>
main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];

printf("\nEnter the memory size -- ");


scanf("%d",&ms);

printf("\nEnter the page size -- ");


scanf("%d",&ps);

nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);

printf("\nEnter number of processes -- ");


scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)

printf("\nEnter no. of pages required for p[%d]-- ",i);


scanf("%d",&s[i]);

if(s[i] >rempages)
{

printf("\nMemory is Full");
break;
}
rempages = rempages - s[i];

printf("\nEnter pagetable for p[%d] --- ",i);


for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}

printf("\nEnter Logical Address to find Physical Address ");


printf("\nEnter process no. and pagenumber and offset -- ");

scanf("%d %d %d",&x,&y, &offset);

if(x>np || y>=s[i] || offset>=ps)


printf("\nInvalid Process or Page Number or offset");

else
{
pa=fno[x][y]*ps+offset;
printf("\nThe Physical Address is -- %d",pa);

}
}

Output:
Enter the memory size -- 1000
Enter the page size -- 100
The no. of pages available in memory are -- 10
Enter number of processes -- 3
Enter no. of pages required for p[1]-- 4

Enter pagetable for p[1] --- 8 6 9 5


Enter no. of pages required for p[2]-- 5

Enter pagetable for p[2] --- 1 4 5 7 3


Enter no. of pages required for p[3]-- 5
Memory is Full

Enter Logical Address to find Physical Address


Enter process no. and pagenumber and offset -- 1 2 75

The Physical Address is -- 975

You might also like