Advertisement
Marko35S

Untitled

Jun 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. /*
  2. * SList.cpp
  3. *
  4. * Created on: Jun 16, 2018
  5. * Author: OS1
  6. */
  7. #include "PCB.h"
  8. #include "SList.h"
  9. #include "SCHEDULE.H"
  10. //global list
  11. SleepList* SleepList::sleepingList = NULL;
  12.  
  13. //constructor
  14. SleepList::SleepList(){
  15. first = NULL;
  16. last = NULL;
  17. }
  18.  
  19. //destructor
  20. SleepList::~SleepList(){
  21. Node* currentNode = this->first; // initialize current node to first
  22. while (currentNode){
  23.  
  24. Node* nextNode = currentNode->next; // get next node
  25. delete currentNode; // delete current
  26. currentNode = nextNode; // set current to "old" next
  27.  
  28. }
  29. this->first = NULL;
  30. this->last = NULL;
  31. }
  32.  
  33.  
  34. void SleepList::insertFirst(PCB *newPCB, int sleepTime){
  35. //error
  36. if(newPCB == NULL)
  37. return;
  38.  
  39. //if there is no first node
  40. if(first == NULL){
  41. first = new Node(newPCB, sleepTime);
  42. last = first;
  43. return;
  44. }
  45.  
  46.  
  47. Node *temp = new Node(newPCB, sleepTime);
  48. //out of memory
  49. if(temp == NULL)
  50. return;
  51.  
  52. temp->next = first;
  53. first = temp;
  54.  
  55. return;
  56.  
  57. }
  58. void SleepList::insertLast(PCB *newPCB, int sleepTime){
  59. //error
  60. if(newPCB == NULL)
  61. return;
  62.  
  63. //if there is no last node
  64. if(last == NULL){
  65. last = new Node(newPCB, sleepTime);
  66. first = last;
  67. return;
  68. }
  69.  
  70. Node* temp = new Node(newPCB, sleepTime);
  71.  
  72. //out of memory
  73. if(temp == NULL)
  74. return;
  75.  
  76. last->next = temp;
  77. last = temp;
  78. }
  79. void SleepList::insertSorted(PCB *newPCB, int sleepTime){
  80. //error
  81. if(newPCB == NULL)
  82. return;
  83. //empty list case
  84. if(first == NULL){
  85. first = new Node(newPCB, sleepTime);
  86. last = first;
  87. return;
  88. }
  89.  
  90. //first edge case
  91. if(sleepTime <= first->sleepTime){
  92. insertFirst(newPCB, sleepTime);
  93. return;
  94. }
  95.  
  96. //second edge case
  97. if(sleepTime >= last->sleepTime){
  98. insertLast(newPCB, sleepTime);
  99. return;
  100. }
  101.  
  102. //'mid' case
  103. Node *curr, *prev;
  104. curr = first;
  105. prev = NULL;
  106.  
  107. while(curr != NULL && curr->sleepTime <= sleepTime){
  108. prev = curr;
  109. curr = curr->next;
  110. }
  111.  
  112. //some error occured
  113. if(curr == NULL || prev == NULL){
  114. return;
  115. }
  116.  
  117.  
  118. //now curr points to the first node with a greater sleep time
  119. Node *temp = new Node(newPCB, sleepTime);
  120. prev->next = temp;
  121. temp->next = curr;
  122.  
  123. }
  124.  
  125. void SleepList::decrementAll(){
  126.  
  127. if(first == NULL){
  128. return;
  129. }
  130.  
  131. Node *temp = first;
  132.  
  133. while(temp != NULL){
  134.  
  135. temp->sleepTime -= 1;
  136. temp = temp->next;
  137. }
  138.  
  139. }
  140.  
  141. void SleepList::simulateSleep() {
  142.  
  143. //there are no threads asleep
  144. if (first == NULL)
  145. return;
  146.  
  147.  
  148. //we are reducing remaining times to all the sleeping threads
  149. decrementAll();
  150.  
  151. while (first != 0 && first->sleepTime == 0) {
  152.  
  153. //we are removing the first element from the list
  154. Node *temp = first;
  155. first = first->next;
  156.  
  157. //we are taking the PCB and puttiing it in the scheduler
  158. PCB* wakeUp = temp->pcb;
  159. wakeUp->state = READY;
  160. Scheduler::put(wakeUp);
  161.  
  162. delete temp;
  163.  
  164. }
  165.  
  166. //if the list is empty restart the pointers
  167. if(first == NULL){
  168. last = NULL;
  169. }
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement