Program to simulate Free Space Management

Share
  1. Using Bitmap

A sequence or collection of bits, where each bit stands for a disk block, is known as a bitmap or bit vector. The bit has two possible values: 0 and 1: 0 means the block is allocated, and 1 means the block is free.

#include<stdio.h>
#include<stdlib.h>
int main(){
    int start_add, length;
    int status[50];

    printf("Enter start address : ");
    scanf("%d",&start_add);
    printf("Enter the length:");
    scanf("%d",&length);

    //constructing bitmaps, assuming memory address exactly divisible by 3 and 4 is already allocated
    for(int i=0;i<length;i++){
        if((start_add+i)%3==0 || (start_add+i)%4==0){
            status[i] = 0;
        }else{
            status[i] = 1;
        }
    }

    printf("Bitmap [ Assuming memory address exactly divisible by 3 and 4 is already allocated] \n");
    //printing bitmap
    for(int i=0;i<length;i++){
        printf("%d",status[i]);
    }
    printf("\n");
    return 0;
}

OUTPUT

2. Using Linked List

Link List It is an alternative strategy for managing open space. In this method, all the free blocks are linked together, and the first free block’s pointer is kept in the cache. As a result, a pointer will link each of the disks’ free blocks together.

#include<stdio.h>
#include<stdlib.h>

struct freeSpace{
    int address;
    struct freeSpace* next;
};

struct freeSpace* addNode(struct freeSpace **head, int addr){
    struct freeSpace* temp;
    temp = (struct freeSpace*)malloc(sizeof(struct freeSpace*));
    temp->address = addr;
    temp->next = NULL;
    if(head == NULL){
        head = temp;
    }else{
        struct freeSpace *lastNode = head;
        while(lastNode->next != NULL)
        {
            lastNode = lastNode->next;
        }
        lastNode->next = temp;
    }
}
int main(){
    struct freeSpace *head = NULL;
    struct freeSpace *temp = head;
    int start_add, length;
    printf("Enter start address : ");
    scanf("%d",&start_add);
    printf("Enter the length:");
    scanf("%d",&length);
    //constructing linked list, assuming memory address exactly divisible by 2 and 5 is free
    for(int i=0;i<length;i++){
        if((start_add+i)%2==0||(start_add+i)%5==0){
            addNode(&head,(start_add+i));
        }
    }

    printf("Linked List [Assuming memory address exactly divisible by 2 and 5 is free] \n");
    //printing bitmap
    while (temp->next!=NULL){
        printf("%d -> ",temp->address);
        temp=temp->next;
    }
    printf("NULL \n");
    return 0;
}

OUTPUT

Source code repository: https://github.com/CodewithSudeep/operating-system-c-program

Other C Programs: https://codewithsudeep.com/category/c-program/

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: