OS Mem Alloc
OS Mem Alloc
OS Mem Alloc
#include<stdio.h>
#include<stdlib.h>
struct mem
{
int block_no;
int size;
struct process *occ_proc;
struct mem *next;
}*start = NULL;
struct process
{
int proc_no;
struct mem *block;
int proc_size;
struct process *next;
}*p_start = NULL;
int i;
typedef struct process process;
typedef struct mem mem;
void Display();
void Menu();
void Allocate();
void Sort(int);
void Swap(mem*, mem*);
int main()
{
int block_size[] = {100, 500, 200, 300, 600};
m->block_no = i + 1;
if(start != m)
prev->next = m;
prev = m;
}
prev->next = NULL;
Menu();
return 0;
}
void Display()
{
process *temp = p_start;
printf("\nProcess No\t\tProcess size\t\tBlock No\t\tRemaining
space\n");
while(temp)
{
if(!temp->block)
printf("Unallocated\n");
else
printf("%d\t\t\t%d\n", temp->block->block_no, temp->block-
>size);
temp = temp->next;
}
void Menu()
{
char choice;
int n;
int occupied_block[10];
do
{
printf("\n******************************\nInput number of
processes : ");
scanf("%d", &n);
process *prevp = NULL;
for(i = 0; i < n ; ++i)
{
printf("\nInput process size for P%d : ", i + 1);
int s;
process *p = (process*)malloc(sizeof(process));
if(i == 0)
p_start = p;
scanf("%d",&p->proc_size);
p->block = NULL;
p->proc_no = i + 1;
if(p_start != p)
prevp->next = p;
prevp = p;
}
prevp->next = NULL;
printf("\nSelect option : \n1. First fit\n2. Best fit\n3.
Worst fit\n4. Exit\n");
scanf(" %c", &choice);
switch(choice)
{
case '2':
Sort(1);
break;
case '3':
Sort(0);
break;
case '4':
return;
}
Allocate();
Display();
}while(1);
void Allocate()
{
process *temp1 = p_start;
mem *temp2;
while(temp1)
{
temp2 = start;
while(temp2)
{
if(temp2->size >= temp1->proc_size && !temp2->occ_proc)
{
temp2->size -= temp1->proc_size;
temp2->occ_proc = temp1;
temp1->block = temp2;
break;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}
}
}
void Sort(int asc)
{
mem *m_temp1 = start;
mem *min_block = NULL;
while(m_temp1->next)
{
min_block = m_temp1;
m_temp2 = m_temp2->next;
}
Swap(min_block, m_temp1);
m_temp1 = m_temp1->next;
}
}