Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. // create the linked list structure of the memory block
  5. struct MemoryBlock {
  6. int beginAddr;
  7. int endAddr;
  8. int hole;
  9. int Pro_ID;
  10. struct MemoryBlock* next;
  11. };
  12. typedef struct MemoryBlock node;
  13.  
  14. // declare the starting pointer of the linked list as NULL
  15. node* start = NULL;
  16.  
  17. //initialize and return a free memory block/node
  18. node* createNode(int beginAddr, int endAddr)
  19. { //takes the starting address and the ending address as input
  20. node* temp = (node*)malloc(sizeof(node)); //allocate memory using malloc() function
  21. temp->beginAddr = beginAddr;
  22. temp->endAddr = endAddr;
  23. temp->hole = 1; //hole
  24. temp->Pro_ID = -1; //means there's no process going on
  25. return temp; //returns the
  26. }
  27.  
  28. //if the size of the memory block/node >= the size of the process, a process is assigned to the block
  29. void addProcess(int size, int pid) {
  30. node* ptr = start;
  31. while(ptr != NULL) {
  32. if ((ptr->endAddr - ptr->beginAddr) >= size && ptr->hole==1)
  33. {
  34. node* new_node = createNode(ptr->beginAddr + size, ptr->endAddr);
  35. ptr->endAddr = (ptr->beginAddr + size-1);
  36. ptr->hole = 0; //Memory block is occupied
  37. ptr->Pro_ID = pid; //the Process id of the Process occupied
  38. new_node->next = ptr->next;
  39. ptr->next = new_node; // points to the next node/block
  40. break;
  41. }
  42. ptr = ptr->next;
  43. }
  44. }
  45.  
  46. //aadds the created node to the list
  47. void addNode(node* new_node) {
  48. if (start == NULL) {
  49. start = new_node;
  50. }
  51. else
  52. {
  53. node* ptr = start;
  54. while (ptr->next != NULL) {
  55. ptr = ptr->next;
  56. }
  57. ptr->next = new_node;
  58. }
  59. }
  60.  
  61. int deleteProcess(node *start, int del_pid)
  62. {
  63. node* prePtr = start;
  64. node* ptr = start;
  65. if(del_pid<0)
  66. {
  67. printf("Invalid Process ID!\n");
  68. return 0;
  69. }
  70.  
  71. while(ptr != NULL)
  72. {
  73. if (ptr->Pro_ID == del_pid)
  74. {
  75. ptr->hole = 1;
  76. ptr->Pro_ID = -1;
  77. if(ptr==prePtr)
  78. {
  79. return 1;
  80. }
  81. if(ptr->next == NULL)
  82. {
  83. return 1;
  84. }
  85.  
  86. return 1;
  87. }
  88. prePtr = ptr;
  89. ptr = ptr->next;
  90. }
  91. printf("No match for Process ID %d\n", del_pid);
  92. return 0;
  93.  
  94. }
  95.  
  96. //Snapshot of the current memory blocks
  97. void printSnapshot(node *start){
  98. node* temp = start;
  99. int size;
  100. printf("Process ID\tSize\tBegin Address\t\tEnd Address\tHole\n");
  101. printf("-----------------------------------------------------------------------\n");
  102. printf("OS\t\t"); //ProcessID of the node
  103. printf("400\t\t");
  104. printf("0\t\t"); //Starting address of the node
  105. printf("399\t\t");
  106. printf("0\t\n");
  107. while(temp != NULL) //while the node is not empty
  108. {
  109. printf("%d\t\t", temp->Pro_ID); //ProcessID of the node
  110. printf("%d\t\t", temp->endAddr - (temp->beginAddr - 1)); //Size of the node
  111. printf("%d\t\t", temp->beginAddr);
  112. printf("%d\t\t", temp->endAddr);
  113. printf("%d\t\t\n", temp->hole);
  114.  
  115. temp = temp->next;
  116. }
  117. }
  118.  
  119.  
  120. int main(){
  121. int choice;
  122. int pid, size;
  123. int del_pid;
  124.  
  125. node* tmp=createNode(400, 2560); //Initialize memory block
  126. addNode(tmp);
  127.  
  128. //printSnapshot(start);
  129. addProcess(600,1);
  130. //printSnapshot(start);
  131. addProcess(200,2);
  132. printSnapshot(start);
  133. //deleteProcess(start,1);
  134. //printSnapshot(start);
  135. //addProcess(700,3);
  136. //printSnapshot(start);
  137. //deleteProcess(start,1);
  138. //printSnapshot(start);
  139. printf("\n\n");
  140.  
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement