Advertisement
moldovexc

suu

May 29th, 2023
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ucontext.h>
  4. #include <signal.h>
  5. #include <sys/time.h>
  6.  
  7. typedef struct Task Task;
  8. struct Task {
  9.     ucontext_t context;
  10.     char taskChar;
  11.     Task *next;
  12. };
  13.  
  14. Task taskQueue;
  15. Task *runningTask;
  16.  
  17. void enqueueTask(Task *newTask) {
  18.     newTask->next = NULL;
  19.  
  20.     if (taskQueue.next == NULL) {
  21.         taskQueue.next = newTask;
  22.     } else {
  23.         Task *lastTask = taskQueue.next;
  24.         while (lastTask->next != NULL) {
  25.             lastTask = lastTask->next;
  26.         }
  27.         lastTask->next = newTask;
  28.     }
  29. }
  30.  
  31. Task* dequeueTask() {
  32.     if (taskQueue.next == NULL) {
  33.         return NULL;
  34.     }
  35.  
  36.     Task *nextTask = taskQueue.next;
  37.     taskQueue.next = nextTask->next;
  38.     nextTask->next = NULL;
  39.  
  40.     return nextTask;
  41. }
  42.  
  43. void handleSignal(int signum) {
  44.     Task *nextTask = dequeueTask();
  45.     if (nextTask != NULL) {
  46.         enqueueTask(runningTask);
  47.         runningTask = nextTask;
  48.         swapcontext(&taskQueue.context, &runningTask->context);
  49.     }
  50. }
  51.  
  52. void initializeTimer() {
  53.     struct sigaction sa;
  54.     sa.sa_handler = handleSignal;
  55.     sigemptyset(&sa.sa_mask);
  56.     sa.sa_flags = 0;
  57.     sigaction(SIGALRM, &sa, NULL);
  58.  
  59.     // Set up the timer
  60.     struct itimerval timer;
  61.     timer.it_interval.tv_sec = 1;       // 1 second interval
  62.     timer.it_interval.tv_usec = 0;
  63.     timer.it_value = timer.it_interval;
  64.     setitimer(ITIMER_REAL, &timer, NULL);
  65. }
  66.  
  67. void* initializeLibrary() {
  68.     taskQueue.next = NULL;
  69.     runningTask = NULL;
  70.     return NULL;
  71. }
  72.  
  73. void taskFunction() {
  74.     while (1) {
  75.         printf("%c", runningTask->taskChar);  // Print the character associated with the task
  76.         swapcontext(&runningTask->context, &taskQueue.context);
  77.     }
  78. }
  79.  
  80. int createTask(void (*startRoutine)(), char taskChar) {
  81.     Task *newTask = (Task*) malloc(sizeof(Task));
  82.     if (newTask == NULL) {
  83.         return -1;
  84.     }
  85.  
  86.     getcontext(&newTask->context);
  87.     newTask->context.uc_stack.ss_sp = malloc(SIGSTKSZ);
  88.     newTask->context.uc_stack.ss_size = SIGSTKSZ;
  89.     newTask->context.uc_link = &taskQueue.context;
  90.  
  91.     newTask->taskChar = taskChar;
  92.  
  93.     makecontext(&newTask->context, startRoutine, 0);
  94.  
  95.     enqueueTask(newTask);
  96.  
  97.     return 0;
  98. }
  99.  
  100. int main(int argc, char *argv[]) {
  101.     initializeLibrary();
  102.    
  103.     if (argc != 3) {
  104.         printf("The correct usage is: %s <firstTask> <secondTask>\n", argv[0]);
  105.         return 1;
  106.     }
  107.  
  108.     if (createTask(taskFunction, '+') != 0) {
  109.         printf("ERROR WHILE CREATING TASK -> [firstTask]\n");
  110.         return 1;
  111.     }
  112.  
  113.     if (createTask(taskFunction, '-') != 0) {
  114.         printf("ERROR WHILE CREATING TASK -> [secondTask]\n");
  115.         return 1;
  116.     }
  117.  
  118.     runningTask = dequeueTask();
  119.     initializeTimer();
  120.  
  121.     while (1) {
  122.         if (runningTask == NULL) {
  123.             printf("\nAll tasks completed!\n");
  124.             exit(0);
  125.         }
  126.  
  127.         Task *nextTask = dequeueTask();
  128.         if (nextTask != NULL) {
  129.             enqueueTask(runningTask);
  130.             runningTask = nextTask;
  131.         }
  132.  
  133.         swapcontext(&taskQueue.context, &runningTask->context);
  134.     }
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement