Advertisement
minh_tran_782

FCFS_Scheduling

Mar 13th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 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.     struct qitem_t* front = in_queue.head;
  22.     int cnt = 0;
  23.     while (front != NULL) {
  24.         front = front->next;
  25.         ++cnt;
  26.     }
  27.     front = in_queue.head;
  28.     int size = 0;  // bien dem
  29.     struct pcb_t * arr = (struct pcb_t*)malloc(sizeof(struct pcb_t) * cnt);
  30.     while (front != NULL) {
  31.         arr[size] = *(front->data);
  32.         front = front->next;
  33.         ++size;
  34.     }  
  35.     //////////////SORT
  36.     int flag = 0;
  37.     for (int i = 0; i < cnt-1; i++)
  38.     {
  39.         flag = 0;
  40.     for (int j = 0; j < cnt-i-1; j++)
  41.         {
  42.             if (arr[j].arrival_time > arr[j+1].arrival_time)
  43.             {
  44.             struct pcb_t temp;
  45.             temp = arr[j];
  46.             arr[j] = arr[j+1];
  47.             arr[j+1] = temp;
  48.             flag = 1;  
  49.             }
  50.         }
  51.     if (flag == 0) break;      
  52.     }
  53.     for (int i = 0; i < cnt; ++i)
  54.     {
  55.         int start = timestamp;
  56.         int id = arr[i].pid;
  57.         int exec_time  = arr[i].burst_time;
  58.             usleep(exec_time * TIME_UNIT);
  59.             /* Update the timestamp */
  60.             timestamp += exec_time;
  61.             printf("%2d-%2d: Execute %d\n", start, timestamp, id);
  62.     }
  63.     ////////////////////END SORT
  64. }
  65.  
  66. /* Read the list of process to be executed from stdin */
  67. void load_task() {
  68.     int num_proc = 0;
  69.     scanf("%d %d\n", &timeslot, &num_proc);
  70.     int i;
  71.     for (i = 0; i < num_proc; i++) {
  72.         struct pcb_t * proc = (struct pcb_t *)malloc(sizeof(struct pcb_t));
  73.         scanf("%d %d\n", &proc->arrival_time, &proc->burst_time);
  74.         proc->pid = i;
  75.         en_queue(&in_queue, proc);
  76.     }
  77. }
  78. int main() {
  79.     pthread_t cpu_id;   // CPU ID
  80.  
  81.     /* Initialize queues */
  82.     initialize_queue(&in_queue);
  83.     initialize_queue(&ready_queue);
  84.  
  85.     /* Read a list of jobs to be run */
  86.     load_task();
  87.  
  88.     /* Start cpu */
  89.     pthread_create(&cpu_id, NULL, cpu, NULL);
  90.  
  91.     pthread_join(cpu_id, NULL);
  92.  
  93.     pthread_exit(NULL);
  94.  
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement