Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct memBlock{
  5. int pid; //ProcessID
  6. int isFree; //Checks if the node is free
  7. int size; //size of the node
  8. struct memBlock *next; //points to the next node
  9. };typedef struct memBlock memBlock;
  10.  
  11. int free_pid = 00;
  12.  
  13. //Snapshot of the current memory blocks
  14. void snapshot(struct memBlock* n){
  15. int pid;
  16. printf("Process ID\t\tSize\t\tStatus\n");
  17. printf("------------------------------------------------\n");
  18. while(n != NULL){ //while the node is not empty
  19. printf("%d\t\t\t", n->pid); //ProcessID of the node
  20. printf("%d\t\t", n->size); //Size of the node
  21. if(n->isFree == 0) //isFree==0 means the node isn't empty
  22. {
  23. if(pid=1)
  24. {
  25. printf("Operating System\n"); //the node with the ProcessID 1 is allocated for the OS
  26. }
  27. else
  28. {
  29. printf("Occupied\n"); //The node is is occupied with a process
  30. }
  31. }else{
  32. printf("Free\n"); //The node is empty/ A free block of memory
  33. }
  34. n = n->next;
  35. }
  36. }
  37.  
  38. memBlock *newBlock(int new_size){ //Takes in the size of the memory as input
  39. memBlock *memory = (memBlock*)malloc(sizeof(memBlock *)); //Allocating memory for the MemBlock nodes using malloc()
  40. memory->isFree = 1; //memory is empty
  41. memory->pid = free_pid;
  42. memory->size = new_size; //insert the size of the memory
  43. memory->next = NULL; //points to NULL to end the list
  44. return memory;
  45. }
  46.  
  47. //Adding a new process/node to the list
  48. int addProcess(memBlock* node, int new_size, int new_pid){ //Takes the head node, the size of the new node, and the ProcessID that's to be allocated memory
  49. while(node != NULL){
  50. if(node->isFree == 1 && node->size == new_size){
  51. node->isFree = 0; //the node isn't free anymore
  52. node->pid = new_pid;
  53. return 1;
  54. }
  55.  
  56. if(node->isFree == 1 && node->size > new_size){
  57. memBlock *unused = newBlock(node->size - new_size); //takes some extra space from the unused block of memory
  58. unused->next = node->next; //points the unused block of memory as the next node
  59. node->next = unused;
  60. node->isFree = 0;
  61. node->size = new_size; //enter the size of the new node
  62. node->pid = new_pid; //insert the Process ID
  63. return 1;
  64.  
  65. }
  66.  
  67. node = node->next; //points to the next node
  68. }
  69.  
  70. printf("No free space!\n");
  71. return 0;
  72. }
  73.  
  74.  
  75. int deleteProcess(memBlock* node, int del_pid){ //Takes in the head node and the ProcessID that needs to be removed.
  76.  
  77. //Define the previous node which initially it points to the head.
  78. //In the initial state, both node and prenode points to the head. Because thats where a linked list starts.
  79. memBlock* prev_node = node;
  80.  
  81. while(node != NULL){ //Traverse through the list untill you meet the node->next = NULL
  82. //If you find the node that you have to delete, then,
  83. if(node->pid == del_pid){
  84. //First make the node a free node. Set is free to true(1)
  85. node->isFree = 1;
  86. //Then set the pid of the free node to 0. (I used 0 as the pid to mark all the free blocks)
  87. node->pid = free_pid;
  88.  
  89. //If the node and prenode points to the same block, then return 1 because if node = prenode then the given node is the head
  90. if(node == prev_node){
  91. return 1;
  92. }
  93. //If the next node to a given node is NULL then the given node is the end of the list. So again we return 1
  94. if(node->next == NULL){
  95. return 1;
  96. }
  97.  
  98. if(prev_node->isFree == 1){
  99. prev_node->size = prev_node->size + node->size;
  100. prev_node->next = node->next;
  101. free(node);
  102. node = prev_node;
  103. }
  104. //Right merge
  105. if(node->next->isFree == 1){
  106. memBlock* next_node = node->next;
  107. node->size = next_node->size + node->size;
  108. node->next = next_node->next;
  109. node->isFree = 1;
  110. free(next_node);
  111. }
  112. return 1;
  113. }
  114. //Increment the loop
  115. prev_node = node;
  116. node = node->next;
  117. }
  118. printf("No match for Process ID %d\n", del_pid);
  119. return 0;
  120. }
  121.  
  122.  
  123. int main(){
  124. int choice;
  125. int new_size, new_pid;
  126. int del_pid;
  127. memBlock* head = newBlock(2560000); //The total size of the memory
  128. addProcess(head,400000,1); //Allocating 400k bytes for the Operating system as the 1st Process (pid=1)
  129.  
  130. while (1)
  131. {
  132. printf("Memory Management using First-Fit Algorithm\n");
  133. printf("---------------------------------------------\n");
  134. printf("1.Allocate memory for new Process\n");
  135. printf("2.Deallocate memory from an excisting process\n");
  136. printf("3.Print current snapshot\n");
  137. printf("4.Exit\n");
  138. printf("---------------------------------------------\n");
  139. printf("Select an option: ");
  140. scanf("%d", &choice);
  141. printf("\n\n");
  142.  
  143. switch (choice)
  144. {
  145. case 1:
  146. printf("Enter Process ID\n");
  147. scanf("%d", &new_pid);
  148. printf("Enter the size of the memeory block for Process ID %d:\n", new_pid);
  149. scanf("%d", &new_size);
  150. addProcess(head,new_size,new_pid);
  151. printf("Memory allocated for Process %d\n", new_pid);
  152. break;
  153.  
  154. case 2:
  155. printf("\nEnter the Process ID you want to terminate:\n");
  156. scanf("%d", &del_pid);
  157. deleteProcess(head,del_pid);
  158. printf("Process %d terminated", del_pid);
  159. break;
  160.  
  161. case 3:
  162. snapshot(head);
  163. break;
  164.  
  165. case 4:
  166. printf("Exit\n");
  167. exit(0);
  168.  
  169. default:
  170. printf("Error!, invalid choice.\n");
  171. break;
  172. }
  173.  
  174. printf("\n\n");
  175. }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement