Advertisement
EBobkunov

ex2 w6 scheduler.c

Oct 16th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <string.h>
  6. #include <sys/wait.h>
  7. #include <sys/time.h>
  8. #include <limits.h>
  9.  
  10. #define PS_MAX 10
  11.  
  12. typedef struct {
  13.     int idx;
  14.     int at, bt, rt, wt, ct, tat, burst;
  15. } ProcessData;
  16.  
  17. int running_process = -1;
  18. unsigned total_time;
  19. ProcessData data[PS_MAX];
  20. pid_t ps[PS_MAX];
  21. unsigned data_size;
  22.  
  23. void read_file(FILE* file) {
  24.     int idx, at, bt;
  25.     int count = 0;
  26.  
  27.     while (fscanf(file, "%d %d %d", &idx, &at, &bt) == 3) {
  28.         data[count].idx = idx;
  29.         data[count].at = at;
  30.         data[count].bt = bt;
  31.         data[count].rt = bt;
  32.         data[count].burst = bt;
  33.         data[count].wt = 0;
  34.         data[count].ct = 0;
  35.         data[count].tat = 0;
  36.         count++;
  37.     }
  38.  
  39.     data_size = count;
  40.     memset(ps, 0, sizeof(ps)); // Initialize ps array to zeros
  41. }
  42.  
  43. void resume(pid_t process) {
  44.     if (process >= 0 && process < PS_MAX && ps[process] > 0) {
  45.         kill(ps[process], SIGCONT);
  46.     }
  47. }
  48.  
  49. void suspend(pid_t process) {
  50.     if (process >= 0 && process < PS_MAX && ps[process] > 0) {
  51.         kill(ps[process], SIGTSTP);
  52.     }
  53. }
  54.  
  55. void terminate(pid_t process) {
  56.     if (process >= 0 && process < PS_MAX && ps[process] > 0) {
  57.         kill(ps[process], SIGTERM);
  58.         waitpid(ps[process], NULL, 0);
  59.         ps[process] = 0;
  60.     }
  61. }
  62.  
  63. void create_process(int new_process) {
  64.     if (running_process != -1) {
  65.         // Stop the running process
  66.         suspend(running_process);
  67.     }
  68.  
  69.     pid_t child_pid = fork();
  70.     if (child_pid == -1) {
  71.         perror("Fork failed");
  72.         exit(EXIT_FAILURE);
  73.     }
  74.  
  75.     if (child_pid == 0) {
  76.         // Child process: Execute the worker program
  77.         char process_idx_str[2];
  78.         sprintf(process_idx_str, "%d", new_process);
  79.  
  80.         char* worker_args[] = {"./worker", process_idx_str, NULL};
  81.  
  82.         execvp(worker_args[0], worker_args);
  83.         perror("Exec failed");
  84.         exit(EXIT_FAILURE);
  85.     } else {
  86.         // Parent process: Update the state
  87.         ps[new_process] = child_pid;
  88.         running_process = new_process;
  89.         printf("Scheduler: Starting Process %d (Remaining Time: %d)\n", new_process, data[new_process].burst);
  90.     }
  91. }
  92.  
  93. ProcessData find_next_process() {
  94.     int location = -1;
  95.     int min_arrival_time = INT_MAX;
  96.  
  97.     for (int i = 0; i < data_size; i++) {
  98.         if (data[i].burst > 0 && data[i].at < min_arrival_time) {
  99.             location = i;
  100.             min_arrival_time = data[i].at;
  101.         }
  102.     }
  103.  
  104.     if (location == -1) {
  105.         // No process has arrived yet, increment total_time and try again
  106.         printf("Scheduler: Runtime: %u seconds.\nProcess %d: has not arrived yet.\n", total_time, location);
  107.         total_time++;
  108.         return find_next_process();
  109.     } else {
  110.         return data[location];
  111.     }
  112. }
  113.  
  114. void report() {
  115.     printf("Simulation results.....\n");
  116.     int sum_wt = 0;
  117.     int sum_tat = 0;
  118.  
  119.     for (int i = 0; i < data_size; i++) {
  120.         printf("process %d: \n", i);
  121.         printf("    at=%d\n", data[i].at);
  122.         printf("    bt=%d\n", data[i].bt);
  123.         printf("    ct=%d\n", data[i].ct);
  124.         printf("    wt=%d\n", data[i].wt);
  125.         printf("    tat=%d\n", data[i].tat);
  126.         printf("    rt=%d\n", data[i].rt);
  127.         sum_wt += data[i].wt;
  128.         sum_tat += data[i].tat;
  129.     }
  130.  
  131.     printf("data size = %d\n", data_size);
  132.     float avg_wt = (float)sum_wt / data_size;
  133.     float avg_tat = (float)sum_tat / data_size;
  134.     printf("Average results for this run:\n");
  135.     printf("    avg_wt=%f\n", avg_wt);
  136.     printf("    avg_tat=%f\n", avg_tat);
  137. }
  138.  
  139. void check_burst() {
  140.     for (int i = 0; i < data_size; i++) {
  141.         if (data[i].burst > 0) {
  142.             return;
  143.         }
  144.     }
  145.  
  146.     // report simulation results
  147.     report();
  148.  
  149.     // terminate the scheduler
  150.     exit(EXIT_SUCCESS);
  151. }
  152.  
  153. void schedule_handler(int signum) {
  154.     total_time++;
  155.  
  156.     if (running_process != -1) {
  157.         data[running_process].burst--;
  158.  
  159.         printf("Scheduler: Runtime: %u seconds\n", total_time);
  160.         printf("Process %d is running with %d seconds left\n", running_process, data[running_process].burst);
  161.  
  162.         if (data[running_process].burst == 0) {
  163.             printf("Scheduler: Terminating Process %d (Remaining Time: %d)\n", running_process, data[running_process].burst);
  164.             terminate(running_process);
  165.             data[running_process].ct = total_time;
  166.             data[running_process].tat = data[running_process].ct - data[running_process].at;
  167.             data[running_process].wt = data[running_process].tat - data[running_process].bt;
  168.         }
  169.     }
  170.  
  171.     check_burst();
  172.  
  173.     ProcessData next_process = find_next_process();
  174.  
  175.     if (next_process.idx != running_process) {
  176.         if (running_process != -1) {
  177.             printf("Scheduler: Stopping Process %d (Remaining Time: %d)\n", running_process, data[running_process].burst);
  178.             suspend(running_process);
  179.         }
  180.  
  181.         running_process = next_process.idx;
  182.  
  183.         if (ps[running_process] == 0) {
  184.             create_process(running_process);
  185.             data[running_process].rt = total_time;
  186.         } else {
  187.             printf("Scheduler: Resuming Process %d (Remaining Time: %d)\n", running_process, data[running_process].burst);
  188.             resume(running_process);
  189.         }
  190.     }
  191. }
  192.  
  193. int main(int argc, char* argv[]) {
  194.     if (argc != 2) {
  195.         fprintf(stderr, "Usage: %s <data_file>\n", argv[0]);
  196.         exit(EXIT_FAILURE);
  197.     }
  198.  
  199.     // Read the data file
  200.     FILE* in_file = fopen(argv[1], "r");
  201.     if (in_file == NULL) {
  202.         printf("File is not found or cannot open it!\n");
  203.         exit(EXIT_FAILURE);
  204.     } else {
  205.         read_file(in_file);
  206.     }
  207.  
  208.     // Set a timer
  209.     struct itimerval timer;
  210.     timer.it_value.tv_sec = 1;
  211.     timer.it_value.tv_usec = 0;
  212.     timer.it_interval.tv_sec = 1;
  213.     timer.it_interval.tv_usec = 0;
  214.     setitimer(ITIMER_REAL, &timer, NULL);
  215.  
  216.     // Register the handler for SIGALRM signal
  217.     signal(SIGALRM, schedule_handler);
  218.  
  219.     while (1); // Infinite loop to keep the scheduler running
  220. }
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement