## Assignment 2: CPU Scheudling (80points + 10 bonus points)

Objectives
This assignment has the following objectives.
(1) Enhance the understanding of CPU scheduling;
(2) Learn to implement basic steps of classical schedulers;
(3) Practice and improve programming and coding skills;
Overview
This assignment is related to process and CPU scheduling. Basically, you will imple- ment four classical schedulers, which take the parameters of two processes. Each scheduler will generate its Process Gantt Chart that showing the state of each process, with the format of strings. Use the letter ``R'' to represent the running state, the letter ``w'' for the waiting state and the letter ``r'' for the ready state. In the end, you should also calculate the waiting time and CPU utilization for each scheduler.

Problem Description
Your program should take seven(7) integers as command line parameters as follows:
./assign2 q x1 y1 z1 x2 y2 z2
Here, q is the quantum value for the Round-Robin scheduler, which can be omitted for other schedulers; (x1, y1, z1) represents the first process that has x1 units of CPU burst at first, then followed by an I/O burst of y1 units and then a second CPU burst of z1 units. Similarly, the second process is represented by (x2, y2, z2). Suppose that both processes arrive at time 0, but process 1 arrives in the ready queue just before Process 2. Also, we assume that there are no other processes in the system.

The main program starts by checking the number of parameters. If the number is not correct (not EIGHT), you should print an error message and exit. Otherwise, you should print your name on a line as follows:
Assignment 2 program was written by YOUR NAME , then followed by the 7 input values in the same line.
After that, the main program should call the four scheduler functions one by one (FCFS, SJF, SRJF, RR). For each scheduler function, it will the following 5 lines:
(1) a blank line to separate
(2) a line to indicate the scheduler function (one out of ``FCFS, SJF, SRJF, RR''), such as ``The scheduler is FCFS''.
(3) the two scheduling strings for two processes (each string on its own line).
(4) in the end, you will print the last line with four numeric values. Here, the first two values are integers that indicate the waiting time for the first and second processes separately, which can be computed by counting the number of times ``r'' occur in the first and second scheduling string respectively. The third value is the average waiting time (i.e., average of these two integers). The last one indicates the CPU utilization rate. For the CPU utilization, you can compute it by dividing the total number of ``R'' in the two strings with the length of the longer string.

The four scheduler functions (and any other needed functions) should be put in the ``pslibrary.c'' file. The prototypes (or signatures) of these functions should be in ``pslibrary.h'' and your main program will be located in ``assign2.c'' file (with ``#include "pslibrary.h"'').

Part 1: FCFS Scheduler
The first scheduler function is for First-Come First-Serve (FCFS). It will ignore the first input parameter (e.g. quantum) and has the following prototype: fcfs(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2);

Here, the real process schedule strings will be returned via the first two parameters, which you should supply before the function invocation. As described above, the first process is represented by (x1, y1, z1) and the second by (x2, y2, z2).
Tips: you can use a state machine design. A state machine has a state (the values of the variables) which changes over time. At each time step, the state changes to a new state. The new state depends only on the old one and the input values. Here, the state of the system is the state of the two processes, including information about all of their remaining CPU and I/O bursts. Use the example code as a prototype for your fcfs function. Insert the appropriate code so that it behaves like the fcfs algorithm. It should usually produce the same strings as your Assignment 0. The protoype may handle some cases that are not related to fcfs, such as a quantum. You may delete the unncessary code.
Add the code to test this scheduler in assign2.c by calling this function.

Example: for the input parameters: 0 4 2 7 3 6 5, the output from fcfs() should be:
The scheduler is fcfs:
RRRRwwrRRRRRRR
rrrrRRRwwwwwwrRRRRR
1 5 3.0 1.00000

Part 2: SJF
Implement the SJF algorithm (non-preemptive). Again, ignore the first input parameter. Do this by putting the function:
sjf(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2);
in pslibrary.c. Change the type of scheduler to SJF instead.
Add the code to test this scheduler in assign2.c by calling this function.

Part 3: SRJF (preemptive)
Implement the SRJF algorithm. That is, write the function:
psjf(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2);
in pslibrary.c. Change the type of scheduler to SRJF instead.
Add the code to test this scheduler in assign2.c by calling this function.

Part 4: RR
Implement the Round Robin algorithm.
That is, write the function:
rr(char *s1, char *s2, int q, int x1, int y1, int z1, int x2, int y2, int z2);
in pslibrary.c. Change the type of scheduler to RR instead.
In this case the first input parameter is the quantum and the others will be as before.
Add the code to test this scheduler in assign2.c by calling this function.
Test your program with inputs you have developed. Convince yourself that the program is running correctly.

Please test your schedulers one by one using the following intputs. You should write all outputs/results into a file (called as Result.txt) and include it with your assignment. Without this file (Result.txt), you can get only 50% at most.
Test 1: 3 8 7 3 6 3 2
Test 2: 3 8 7 3 6 7 2
Test 3: 4 8 7 3 6 1 2
Test 4: 3 3 3 4 2 1 2
Test 5: 3 3 2 3 2 1 2
Test 6: 3 5 2 5 4 1 1
Test 7: 3 2 3 4 1 1 5
Test 8: 3 2 3 4 1 4 1
Test 9: 3 2 3 4 5 6 2
Test 10: 3 1 1 4 1 3 1

Note on breaking ties:
Processes can enter the ready queue when they first arrive, when their quantum expires, or when their I/O completes. When the RR algorithm must choose the oldest process from the ready queue it needs a way to break ties. For our algorithms, we break a tie by having process 1 run before process 2.

Part 5: Final Comprehensive Test (with the bonus of 10 points).
For this part, we will get the program runall.c. First, you will try to understand the source code. Then, you can compile it with your pslibrary and run the program. It should take less than a minute to complete its execution. This will generate 4 files, each of about 40 megabytes and having the extension ".all". Make sure you have no other files with this extension in your directory. Then you will execute the following commands:
ls -l *.all
wc *.all
md5sum *.all
In the end, you should write the output of md5sum into your "Result.txt". Since these files (*.all) will have 2,000,001 lines and 2,000,003 words, you should delete these large files, which you should not add them to the submission package.
Submission and Evaluation Guideline:
You will submit them through the blackboard, with the following parts.
1. Source Code in "Source" directory: the source code should include assign2.c, pslibrary.c and pslibrary.h (and any other source files you have if you also include your ``makefile''). Your program needs to have proper comments (e.g., explanations for functions and variables etc.).
2. Result.txt: as we discussed above, without this file, you will get at most 50% points, even if everything is correct. You should put the status in the header of the file (before any printing results), if you haven't finished all algrorithms. Otherwise, you don't need to put the status. Please also note in the beginning, whether you have tried the bonus part or not. We will grade this assignment based on the correctness of results.
Please put all files into a directory called as ``assign2-abc123'', where abc123 should be replaced with your own unique UTSA ID. Then please compress all files into a file and uploade it to the blackboard. The "source" directory should include the source code that is compilable and runnable. If you have do not have the source code, you cannot get any points.
Note: not following any of these rules will lose 5 points immediately (without Result.txt, you will lose 50% of your final score), even all results are correct.