In: Computer Science
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time of each ”process” scheduled. The first toy scheduling algorithm to write this program for is First Come First Serve. After implementing FCFS, write a toy scheduler for Round Robin scheduling.
Here is code for part A:
prog.c:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/resource.h> #include <sys/wait.h> /** * This consists of a loop that echoes * the PID and the niceness of the * current process once per second. */ void proccessBody() { for(;;) { printf("PID: %d; Niceness: %d;\n", getpid(), getpriority(PRIO_PROCESS, 0)); sleep(1); // waiting for one second } } /** * This creates the n dummy processes. */ int main() { // creating the processes const int n = 5; for(int i = 0; i < n; ++i) { if(fork() == 0) { // inside child process proccessBody(); // calling the process body exit(EXIT_SUCCESS); // child exiting successfully } } // waiting for the child processes to end while(wait(NULL) > 0); return 0; }
|