Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- // Define the maximum number of program
- #define NB_PROC 10
- // Define a program with runtime arguments
- struct program_desc
- {
- char *file;
- char *args[5];
- };
- // Program table
- struct program_desc programs[] = {
- {"firefox", {"http://google.fr", NULL}},
- {"gedit", {NULL}}
- };
- // Process
- struct process_entry
- {
- pid_t pid; /* Process PID */
- struct timeval time_sched; /* Time spent in the scheduler */
- struct timeval time_exec; /* Total time allocated on the processor */
- char *file; /* Name of the process executable */
- int exit_status; /* Error code returned by the process */
- };
- // Function prototypes
- static pid_t process_create(struct program_desc program);
- static void process_add(pid_t pid, char* file);
- static void process_remove(struct process_entry *entry);
- static int process_exited(struct process_entry *entry);
- static void scheduler_run(void);
- // Blobal variables
- struct process_entry list[NB_PROC]; // Initialization of the queue at 10 maximum processes
- int listSize = 0; // Initializing the size of the list to 0
- // Main function
- int main (int argc, char **argv)
- {
- printf("Starting the scheduler\n");
- process_create(programs[0]);
- process_create(programs[1]);
- scheduler_run();
- return 0;
- }
- // Execute a program in "program_desc" as a child process and add it to the process list
- static pid_t process_create(struct program_desc program)
- {
- pid_t pid = fork();
- switch (pid)
- {
- // Case of the error:
- // Return error code
- case -1:
- perror("fork error");
- return EXIT_FAILURE;
- break;
- // Case of the child process:
- // Running the corresponding program
- case 0:
- execvp(program.file, program.args);
- break;
- // Case of the father process:
- // Add to the list
- default:
- process_add(pid, program.file);
- break;
- }
- }
- // Adding a process to the list
- static void process_add(pid_t pid, char* file)
- {
- // New process structure
- // The data is in arguments
- struct process_entry p = {pid, *file};
- // Adding the new process structure to our list
- // at position [listSize] because it is not busy yet and is at the end
- list[listSize] = p;
- // int which represents the size of our list is increased (global variable)
- listSize++;
- }
- // Remove a process from the list
- static void process_remove(struct process_entry *entry)
- {
- int i;
- // Shift all entries in the list one step
- for(i=0 ; i < NB_PROC-1 ; i++)
- {
- entry[i] = entry[i+1];
- }
- // Delete the process from the list
- struct process_entry empty;
- entry[NB_PROC-1] = empty;
- // int which represents the size of our list is decreased (global variable)
- listSize--;
- }
- // Check if a process is still alive
- // Return the state of the process: running or terminated
- static int process_exited(struct process_entry *entry)
- {
- int statut = 1;
- // Return the process information without waiting for it to finish
- // Return the child's pid if the state has changed, 0 if no changes and -1 if error
- waitpid(entry->pid, &statut, WNOHANG);
- // Refreshe the exit value of the process with the retrieved value
- entry->exit_status = statut;
- return statut;
- }
- // Code of the scheduler
- static void scheduler_run(void)
- {
- int proceed = 1;
- int i;
- while (proceed == 1)
- {
- for(i=0 ; i <= listSize ; i++)
- {
- // Verify process state
- int ret = process_exited(&list[i]);
- // If process blocked or terminated
- if (ret == 0)
- // Remove the process from the list
- process_remove(&list[i]);
- }
- // If more processes in the waiting list
- if (listSize == 0)
- // Stop the loop
- proceed = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement