Description: FF is a Memory Management in Operating System
Algorithm in which we put the new Process in the first place found between two
processes. In this c code for Memory Management in OS, we are provided with a
file which contains the processes which we need to load in the memory. And then
we will be provided with a second file which will contain new processes and we need
to implement the First Fit Algorithm for Operating System Memory Management.
When we load the first file,
after loading it we need to print sizes of all the holes in the memory, e.g.
After Process 1: 300KB
After Process 2: 20KB
After Process 5: 500KB
In this way we need to
print all available holes. And when the new process fits in some hole, then we
need to print that which holes is filled and prints again the available holes.
Add two more variables
in the PCB.
struct pcb{
int process_id;
int total_time_for_execution;
int process_arrival_time;
pcb *next;
}
File1.txt
Total Memory
ProcessID Offset Length
Example:
1000
1 20 50
2 100 20
3 150 100
File2.txt
ProcessID Length
4 450
5 65
6 58
and so on. First file is loaded when the program
starts. And then First Fit Algorithm Memory Management in OS is used to insert
the process in the memory which is in the second file. If some Process doesn’t
fit into the memory, then an error message is printed that this process is
dropped.
First Fit Algorithm
#include<stdlib.h>
#include <stdio.h>
#include<string.h>
struct Process{
int ProcessID;
int start_offset;
int end_offset;
int length;
};
#define BUFFER_SIZE 1000
void Print(struct Process Buffer[],int n){
int i;
for(i=0;i<n;i++){
printf("Process ID: %d\t",Buffer[i].ProcessID);
printf("Start Offset: %d \t",Buffer[i].start_offset);
printf("Length: %d\t",Buffer[i].length);
printf("End Offset: %d\n",Buffer[i].end_offset);
}
printf("\n\n");
}
void Sort(struct Process Buffer[],int n){
int a,b;
struct Process temp;
for (a = 0; a < n-1 ; a++){
for (b = 0; b < n -1-a; b++){
if(Buffer[b].start_offset>Buffer[b + 1].start_offset){
temp=Buffer[b];
Buffer[b]=Buffer[b + 1];
Buffer[b + 1]=temp;
}
}
}
}
int main(int argc,char *argv[]){
char File_1[25],File_2[25],ch;
int i,j,no,total_memory,p_id,p_size,total_processes,t;
strcpy(File_1,argv[1]);
strcpy(File_2,argv[2]);
struct Process Buffer[BUFFER_SIZE];
struct Process p;
FILE* file1=fopen(File_1,"r");
fscanf(file1,"%d",&total_memory);
printf("\t\t-->Total Memory: %d\n\n",total_memory);
fscanf(file1,"%d",&p.ProcessID);
for(total_processes=0;!feof(file1);total_processes++){
fscanf(file1,"%d",&p.start_offset);
fscanf(file1,"%d",&p.length);
p.end_offset=p.start_offset+p.length-1;
Buffer[total_processes]=p;
fscanf(file1,"%d",&p.ProcessID);
}
fclose(file1);
Sort(Buffer,total_processes);
printf("->Processes already residing in the
Memory\n");
Print(Buffer,total_processes);
//////////////////////////////////////////////////////////
FILE* file2=fopen(File_2,"r");
fscanf(file2,"%d",&p.ProcessID);
while(!feof(file1)){
fscanf(file2,"%d",&p.length);
t=0;
for(i=0;Buffer[i].start_offset==t;i++)
t=Buffer[i].end_offset+1;
for(i;i<total_processes;i++){
if(p.length<=Buffer[i].start_offset-t){
p.start_offset=t;
break;
}
else
t=Buffer[i].end_offset+1;
}
if(i==total_processes&&(t+p.length<1000))
p.start_offset=t;
else if(t+p.length>=1000){
printf("->Process with ID#%d and Length=%d can't be
inserted (not enough space found)",p.ProcessID,p.length);
fscanf(file2,"%d",&p.ProcessID);
continue;
}
p.end_offset=p.start_offset+p.length-1;
Buffer[total_processes++]=p;
Sort(Buffer,total_processes);
printf("->Process with ID#%d and Length=%d is stored
on Offset#%d\n",p.ProcessID,p.length,p.start_offset);
Print(Buffer,total_processes);
fscanf(file2,"%d",&p.ProcessID);
}
return 0;
}
Best Fit Algorithm
#include<stdlib.h>
#include <stdio.h>
#include<string.h>
#include<stdbool.h>
struct Process{
int ProcessID;
int start_offset;
int end_offset;
int length;
};
#define BUFFER_SIZE 1000
void Print(struct Process Buffer[],int n){
int i;
for(i=0;i<n;i++){
printf("Process ID: %d\t",Buffer[i].ProcessID);
printf("Start Offset: %d \t",Buffer[i].start_offset);
printf("Length: %d\t",Buffer[i].length);
printf("End Offset: %d\n",Buffer[i].end_offset);
}
printf("\n\n");
}
void Sort(struct Process Buffer[],int n){
int a,b;
struct Process temp;
for (a = 0; a < n-1 ; a++){
for (b = 0; b < n -1-a; b++){
if(Buffer[b].start_offset>Buffer[b + 1].start_offset){
temp=Buffer[b];
Buffer[b]=Buffer[b + 1];
Buffer[b + 1]=temp;
}
}
}
}
int main(int argc,char *argv[]){
char File_1[25],File_2[25],ch;
int i,j,no,total_memory,total_processes,off,space,t;
bool
first=true;
strcpy(File_1,argv[1]);
strcpy(File_2,argv[2]);
struct Process Buffer[BUFFER_SIZE];
struct Process p;
FILE* file1=fopen(File_1,"r");
fscanf(file1,"%d",&total_memory);
printf("Total: %d\n",total_memory);
fscanf(file1,"%d",&p.ProcessID);
for(total_processes=0;!feof(file1);total_processes++){
fscanf(file1,"%d",&p.start_offset);
fscanf(file1,"%d",&p.length);
p.end_offset=p.start_offset+p.length-1;
Buffer[total_processes]=p;
fscanf(file1,"%d",&p.ProcessID);
}
fclose(file1);
Sort(Buffer,total_processes);
printf("->Processes already residing in the
Memory\n\n");
Print(Buffer,total_processes);
///////////////////////////////////////////////////////////
FILE* file2=fopen(File_2,"r");
fscanf(file2,"%d",&p.ProcessID);
while(!feof(file1)){
first=true;
fscanf(file2,"%d",&p.length);
t=0;
for(i=0;Buffer[i].start_offset==t;i++)
t=Buffer[i].end_offset+1;
off=-1;
for(i=0;i<total_processes;i++){
if(p.length<=Buffer[i].start_offset-t){
if(first){
space=Buffer[i].start_offset-t;
first=false;
off=t;
}
else if(Buffer[i].start_offset-t<space){
space=Buffer[i].start_offset-t;
off=t;
}
}
t=Buffer[i].end_offset+1;
} if(total_memory-t>=p.length&&space>total_memory-t)
off=t;
else if(off==-1){
printf("->Process with ID#%d and Length=%d can't be
inserted (not enough space found)\n\n",p.ProcessID,p.length);
fscanf(file2,"%d",&p.ProcessID);
continue;
}
p.start_offset=off;
p.end_offset=p.start_offset+p.length-1;
Buffer[total_processes++]=p;
Sort(Buffer,total_processes);
printf("\t\t-->Process with ID#%d and Length=%d is stored
on Offset#%d\n",p.ProcessID,p.length,p.start_offset);
Print(Buffer,total_processes);
fscanf(file2,"%d",&p.ProcessID);
}
return 0;
}
No comments
Post a Comment