Advertisement
minh_tran_782

Round Robin Scheduling

Mar 13th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  1.  
  2. #include "queue.h"
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define TIME_UNIT   100 // In microsecond
  9.  
  10. static struct pqueue_t in_queue; // Queue for incomming processes
  11. static struct pqueue_t ready_queue; // Queue for ready processes
  12.  
  13. static int load_done = 0;
  14.  
  15. static int timeslot;    // The maximum amount of time a process is allowed
  16.             // to be run on CPU before being swapped out
  17.  
  18. // Emulate the CPU
  19. void * cpu(void * arg) {
  20.     int timestamp = 0;
  21.     /* Keep running until we have loaded all process from the input file
  22.      * and there is no process in ready queue */
  23.        
  24.     while (!load_done || !empty(&ready_queue)) {
  25.         /* Pickup the first process from the queue */
  26.         struct pcb_t * proc = de_queue(&ready_queue);
  27.         if (proc == NULL) {
  28.             /* If there is no process in the queue then we
  29.              * wait until the next time slice */
  30.             timestamp++;
  31.             usleep(TIME_UNIT);
  32.         }else{
  33.        
  34.             /* Execute the process */
  35.             int start = timestamp;  // Save timestamp
  36.             int id = proc->pid; // and PID for tracking
  37.             /* Decide the amount of time that CPU will spend
  38.              * on the process and write it to 'exec_time'.
  39.              * It should not exeed 'timeslot'.
  40.             */
  41.             int exec_time = 0;
  42.  
  43.             // TODO: Calculate exec_time from process's PCB
  44.            
  45.             // YOUR CODE HERE
  46.         pthread_mutex_lock(&ready_queue.lock);
  47.             if (proc->burst_time > timeslot)
  48.             {
  49.                 exec_time = timeslot; //
  50.                 proc->burst_time -= timeslot;
  51.             }
  52.             else {
  53.                 exec_time = proc->burst_time;
  54.                 proc->burst_time = 0;
  55.             }  
  56.             pthread_mutex_unlock(&ready_queue.lock);    
  57.            
  58.             /* Emulate the execution of the process by using
  59.              * 'usleep()' function */
  60.              
  61.             usleep(exec_time * TIME_UNIT); 
  62.             /* Update the timestamp */
  63.             timestamp += exec_time;
  64.             // TODO: Check if the process has terminated (i.e. its
  65.             // burst time is zero. If so, free its PCB. Otherwise,
  66.             // put its PCB back to the queue.
  67.            
  68.             // YOUR CODE HERE
  69.            
  70.             if (proc->burst_time == 0)
  71.             {
  72.                 free(proc);  
  73.                 proc = NULL;
  74.             }
  75.             else
  76.             {
  77.                 en_queue(&ready_queue, proc);
  78.             }
  79.            
  80.             /* Track runtime status */
  81.             printf("%2d-%2d: Execute %d\n", start, timestamp, id);
  82.         }
  83.     }
  84. }
  85.  
  86. // Emulate the loader
  87. void * loader(void * arg) {
  88.     int timestamp = 0;
  89.     /* Keep loading new process until the in_queue is empty*/
  90.     while (!empty(&in_queue)) {
  91.         struct pcb_t * proc = de_queue(&in_queue);
  92.         /* Loader sleeps until the next process available */
  93.         int wastetime = proc->arrival_time - timestamp;
  94.         usleep(wastetime * TIME_UNIT);
  95.         /* Update timestamp and put the new process to ready queue */
  96.         timestamp += wastetime;
  97.         en_queue(&ready_queue, proc);
  98.     }
  99.     /* We have no process to load */
  100.     load_done = 1;
  101. }
  102.  
  103. /* Read the list of process to be executed from stdin */
  104. void load_task() {
  105.     int num_proc = 0;
  106.     scanf("%d %d\n", &timeslot, &num_proc);
  107.     int i;
  108.      
  109.     for (i = 0; i < num_proc; i++) {
  110.         struct pcb_t * proc = (struct pcb_t *)malloc(sizeof(struct pcb_t));
  111.         scanf("%d %d\n", &proc->arrival_time, &proc->burst_time);
  112.         proc->pid = i;
  113.         en_queue(&in_queue, proc);
  114.     }
  115.      
  116. }
  117.  
  118. int main() {
  119.    
  120.     pthread_t cpu_id;   // CPU ID
  121.     pthread_t loader_id;    // LOADER ID
  122.  
  123.     /* Initialize queues */
  124.     initialize_queue(&in_queue);
  125.     initialize_queue(&ready_queue);
  126.  
  127.     /* Read a list of jobs to be run */
  128.     load_task();
  129.     /* Start cpu */
  130.     pthread_create(&cpu_id, NULL, cpu, NULL);
  131.     /* Start loader */
  132.     pthread_create(&loader_id, NULL, loader, NULL);
  133.  
  134.     /* Wait for cpu and loader */
  135.     pthread_join(cpu_id, NULL);
  136.     pthread_join(loader_id, NULL);
  137.  
  138.     pthread_exit(NULL);
  139.  
  140. }
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement