Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include "../include/execution_queue.h"
  2. #include "../include/ready_queue.h"
  3. #include "../include/short_scheduler.h"
  4. #include "../include/semaphore.h"
  5. #include <ucontext.h>
  6. #include <stdlib.h>
  7. #define STACK_SIZE 4096
  8.  
  9. /* escalonador de longo prazo vai usar as seguintes filas:
  10.  
  11. - ready queue
  12.  
  13. */
  14. ucontext_t *threadEndContext = NULL;
  15. PFILA2 endedThreads = NULL;
  16.  
  17. int Tid = 0;
  18.  
  19. int newTid()
  20. {
  21. int currentTid = Tid;
  22. Tid++;
  23. return currentTid;
  24. }
  25.  
  26.  
  27. void endThread()
  28. {
  29. getcontext(threadEndContext);
  30. TCB_t *currentThread = removeThreadFromExecutionQueue();
  31. AppendFila2(endedThreads, currentThread);
  32. currentThread->state = 4;
  33. releaseThread(currentThread->tid);
  34. executeNextThread();
  35. }
  36.  
  37. void initLongScheduler()
  38. {
  39. endedThreads = malloc(sizeof(FILA2));
  40. CreateFila2(endedThreads);
  41.  
  42. threadEndContext = malloc(sizeof(ucontext_t));
  43. getcontext(threadEndContext);
  44. threadEndContext->uc_stack.ss_sp = (char*)malloc(SIGSTKSZ);
  45. threadEndContext->uc_stack.ss_size = SIGSTKSZ;
  46. threadEndContext->uc_link = NULL;
  47. makecontext(threadEndContext, endThread, 0);
  48.  
  49. TCB_t *mainThread = malloc(sizeof(TCB_t));
  50. mainThread->tid = newTid();
  51.  
  52. ucontext_t *mainContext;
  53. mainContext = malloc(sizeof(ucontext_t));
  54. getcontext(mainContext);
  55. mainContext->uc_stack.ss_sp = (char*)malloc(SIGSTKSZ);
  56. mainContext->uc_stack.ss_size = SIGSTKSZ;
  57. mainContext->uc_link = threadEndContext;
  58. mainThread->context = *(mainContext);
  59. addThreadToExecutionQueue(mainThread);
  60. }
  61.  
  62. int finishedThread(int tid)
  63. {
  64. if(FirstFila2(endedThreads) == ERROR)
  65. {
  66. return ERROR; //Se a fila não estiver vazia
  67. }
  68. do
  69. {
  70. TCB_t *thread;
  71. thread = (TCB_t*)GetAtIteratorFila2(endedThreads);
  72. if(thread != NULL && thread->tid == tid)
  73. {
  74. return SUCCESS;
  75. }
  76. }while(NextFila2(endedThreads) == 0);
  77. return ERROR;
  78. }
  79.  
  80. int createNewThread(void* (*context)(void*), void *arg)
  81. {
  82. ucontext_t * thisNewContext = malloc(sizeof(ucontext_t));
  83. getcontext(thisNewContext);
  84. thisNewContext->uc_link = threadEndContext;
  85. thisNewContext->uc_stack.ss_sp = (char*)malloc(SIGSTKSZ);
  86. thisNewContext->uc_stack.ss_size = SIGSTKSZ;
  87. makecontext(thisNewContext, (void*)context, 1, arg);
  88.  
  89. TCB_t * newThread = malloc(sizeof(TCB_t));
  90. newThread->context = (*thisNewContext);
  91. newThread->tid = newTid();
  92.  
  93. addThreadToReadyQueue(newThread);
  94. return newThread->tid;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement