Linked List for Dynamic Partitioning


A linked list is made up of nodes, each of which has a data element and a pointer (or reference) to the node after it in the list. Each node in dynamic partitioning represents a memory block that can be assigned to a process. The linked list initially reflects the whole memory block that is accessible. In this article, we will explore the Linked List for Dynamic Partitioning, what Dynamic Partitioning is in Memory Management, and also the implementation of Linked List in Dynamic Partitioning.

Dynamic Partitioning in Memory Management

Computer systems employ the memory management approach known as "dynamic partitioning," which divides memory into segments of varying sizes to support numerous processes at once. Dynamic partitioning minimizes memory resource wastage by giving each process only the amount of memory it needs.

The following is how the dynamic partitioning method operates −

  • At first, the entire amount of memory that is accessible is thought of as a single huge block.

  • The system looks for a free block of memory that is big enough to hold the requested memory whenever a process requests memory. Different algorithms, including First Fit, Best Fit, and Worst Fit, might be used to do the search.

  • An appropriate block is then assigned to the requesting process after being discovered.

  • The remaining portion of the block is returned to the free memory pool if the allocated block is further than the needed quantum of memory.

  • The memory that was allocated is deallocated after a process completes, and the freed memory is returned to the free memory pool.

Fragmentation, or the division of memory into little free blocks that are worthless owing to their size, is a problem with the dynamic partitioning technique. Internal and external fragmentation are the two types of fragmentation.

When a memory block allotted to a process is greater than the memory that was requested, internal fragmentation results. In this scenario, the leftover chunk of the block is returned to the free memory pool, but because of its size, it cannot be used by other processes, resulting in memory waste.

It becomes challenging to allocate a larger block of memory to a process when there exist numerous tiny, non-contiguous free blocks of memory. This condition is known as external fragmentation. The solution to this problem is to combine nearby free memory blocks into a single, larger block, however doing so can be computationally expensive.

Dynamic partitioning is a flexible and effective memory management method that allocates only the necessary amount of RAM to each operation, minimizing memory resource waste. However, it has fragmentation problems that may affect the system's performance.

Implementation of Linked List for Dynamic Partitioning

Making a data structure that can effectively allocate and deallocate memory blocks to processes is necessary for the creation of a linked list for dynamic partitioning. Here is an illustration of how to implement a linked list in C for dynamic partitioning −

typedef struct node {
   int size; 
   // size of the memory block
   int free; 
   // flag to indicate whether the block is free or not
   struct node* next; 
   // pointer to the next node in the linked list
} Node;

Node* head = NULL; 
// pointer to the head of the linked list

// Function to initialize the linked list
void initialize(int size) {
   head = (Node*)malloc(sizeof(Node)); 
   // create the head node
   head->size = size - sizeof(Node);
   head->free = 1;
   head->next = NULL;
}

// Function to allocate memory to a process
void* allocate(int size) {
   Node* current = head;
   while (current != NULL) {
      if (current->free && current->size >= size) {
         int remaining_size = current->size - size;
         if (remaining_size >= sizeof(Node)) {
            Node* new_node = (Node*)((char*)current + sizeof(Node) + size);
            new_node->size = remaining_size - sizeof(Node);
            new_node->free = 1;
            new_node->next = current->next;
            current->size = size;
            current->free = 0;
            current->next = new_node;
         } else {
            current->free = 0;
         }
         return (void*)((char*)current + sizeof(Node));
      }
      current = current->next;
   }
   return NULL;
}

// Function to deallocate memory from a process
void deallocate(void* ptr) {
   Node* current = head;
   while (current != NULL) {
      if ((void*)((char*)current + sizeof(Node)) == ptr) {
         current->free = 1;
         if (current->next != NULL && current->next->free == 1) {
            current->size += sizeof(Node) + current->next->size;
            current->next = current->next->next;
         }
         if (current != head && current->free == 1 && current->size <= sizeof(Node)) {
            Node* prev = head;
            while (prev->next != current) {
               prev = prev->next;
            }
            prev->next = current->next;
            free(current);
         }
         break;
      }
      current = current->next;
   }
}

Here, a memory block in a linked list is represented by a Node struct.

  • The size refers to the block's size.

  • Free indicates if the block is free or allocated to a process.

  • Next is a pointer to the node after it in the list.

The allocate function allots memory to a process by looking for a free memory block that can handle the desired size. The initialize function initializes the linked list using the available memory size. The block is divided into two blocks if it is greater than the required size, with the second block being added to the linked list as a free block.

The deallocate function releases memory from a process. If possible, also merges nearby free memory blocks into a single, bigger block.

Conclusion

Computer systems can benefit from the usage of a linked list for dynamic partitioning. Converging nearby free memory blocks, enables effective allocation and deallocation of memory blocks to processes and helps lessen memory fragmentation. Making a data structure with a size, a flag to show whether the block is free or allocated, and a pointer to the subsequent node in the linked list is required to implement a linked list for dynamic partitioning. Memory can be allotted to processes by initializing the linked list with the amount of available memory and looking for free memory blocks that can support the requested size.

Updated on: 03-May-2023

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements