Advertisement
x1n53n

Untitled

Nov 20th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4.  
  5. // Define the maximum number of program
  6. #define NB_PROC 10
  7.  
  8. // Define a program with runtime arguments
  9. struct program_desc
  10. {
  11.     char *file;
  12.     char *args[5];
  13. };
  14.  
  15. // Program table
  16. struct program_desc programs[] = {
  17.     {"firefox", {"http://google.fr", NULL}},
  18.     {"gedit", {NULL}}
  19. };
  20.  
  21. // Process
  22. struct process_entry
  23. {
  24.     pid_t pid;          /* Process PID */
  25.     struct timeval time_sched;  /* Time spent in the scheduler */
  26.     struct timeval time_exec;   /* Total time allocated on the processor */
  27.     char *file;         /* Name of the process executable */
  28.     int exit_status;        /* Error code returned by the process */
  29. };
  30.  
  31. // Function prototypes
  32. static pid_t process_create(struct program_desc program);
  33. static void process_add(pid_t pid, char* file);
  34. static void process_remove(struct process_entry *entry);
  35. static int process_exited(struct process_entry *entry);
  36. static void scheduler_run(void);
  37.  
  38.  
  39. // Blobal variables
  40. struct process_entry list[NB_PROC]; // Initialization of the queue at 10 maximum processes
  41. int listSize = 0; // Initializing the size of the list to 0
  42.  
  43.  
  44. // Main function
  45. int main (int argc, char **argv)
  46. {
  47.     printf("Starting the scheduler\n");
  48.     process_create(programs[0]);
  49.     process_create(programs[1]);
  50.     scheduler_run();
  51.     return 0;
  52. }
  53.  
  54. // Execute a program in "program_desc" as a child process and add it to the process list
  55. static pid_t process_create(struct program_desc program)
  56. {
  57.     pid_t pid = fork();
  58.    
  59.     switch (pid)
  60.     {
  61.         // Case of the error:
  62.         // Return error code
  63.         case -1:
  64.             perror("fork error");
  65.             return EXIT_FAILURE;
  66.             break;
  67.  
  68.         // Case of the child process:
  69.         // Running the corresponding program
  70.         case 0:
  71.             execvp(program.file, program.args);
  72.             break;
  73.  
  74.         // Case of the father process:
  75.         // Add to the list
  76.         default:
  77.             process_add(pid, program.file);
  78.             break;
  79.     }
  80. }
  81.  
  82. // Adding a process to the list
  83. static void process_add(pid_t pid, char* file)
  84. {
  85.     // New process structure
  86.     // The data is in arguments
  87.     struct process_entry p = {pid, *file};
  88.     // Adding the new process structure to our list
  89.     // at position [listSize] because it is not busy yet and is at the end
  90.     list[listSize] = p;
  91.     // int which represents the size of our list is increased (global variable)
  92.     listSize++;
  93. }
  94.  
  95. // Remove a process from the list
  96. static void process_remove(struct process_entry *entry)
  97. {
  98.     int i;
  99.     // Shift all entries in the list one step
  100.     for(i=0 ; i < NB_PROC-1 ; i++)
  101.     {
  102.         entry[i] = entry[i+1];
  103.     }
  104.     // Delete the process from the list
  105.     struct process_entry empty;
  106.     entry[NB_PROC-1] = empty;
  107.     // int which represents the size of our list is decreased (global variable)
  108.     listSize--;
  109. }
  110.  
  111. // Check if a process is still alive
  112. // Return the state of the process: running or terminated
  113. static int process_exited(struct process_entry *entry)
  114. {
  115.     int statut = 1;
  116.     // Return the process information without waiting for it to finish
  117.     // Return the child's pid if the state has changed, 0 if no changes and -1 if error
  118.     waitpid(entry->pid, &statut, WNOHANG);
  119.     // Refreshe the exit value of the process with the retrieved value
  120.     entry->exit_status = statut;
  121.     return statut;
  122. }
  123.  
  124. // Code of the scheduler
  125. static void scheduler_run(void)
  126. {
  127.     int proceed = 1;
  128.     int i;
  129.     while (proceed == 1)
  130.     {
  131.         for(i=0 ; i <= listSize ; i++)
  132.         {
  133.             // Verify process state
  134.             int ret = process_exited(&list[i]);
  135.             // If process blocked or terminated
  136.             if (ret == 0)
  137.                 // Remove the process from the list
  138.                 process_remove(&list[i]);
  139.         }
  140.         // If more processes in the waiting list
  141.         if (listSize == 0)
  142.             // Stop the loop
  143.             proceed = 0;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement