Spring2011.CSCI340Homework4 History

Show minor edits - Show changes to output

Changed line 2 from:
'''Due Date''': Monday, Apr. 11, 2011\\
to:
'''Due Date''': Wednesday, Apr. 13, 2011\\
Deleted lines 120-121:
'''NOTE:''' If a process blocks, it uses only 1 second. If it does not block, it uses the complete quantum.
Added lines 121-122:
'''NOTE:''' If a process blocks, it uses only 1 second. If it does not block, it uses the complete quantum.
Deleted lines 130-131:
, based on their time of arrival, and the current simulated time.
Changed line 2 from:
'''Due Date''': Friday, Apr. 8, 2011\\
to:
'''Due Date''': Monday, Apr. 11, 2011\\
Changed line 56 from:
12345 0 5 # seed to the random number generator, the scheduler to use and the time quantum
to:
12345 r 5 # seed the random number generator with 12345, and run the RR scheduler with a 5-sec quantum
Changed lines 123-126 from:
Lastly, you should only read and schedule processes from the input file 1 line at a time. That is, once your code has read the first process from input, don't read in the next process until the current process is ready to be scheduled (i.e. it's arrival time = current time).

Feel free
to modularize your design using meaningful functions.
to:
Lastly, '''you have two options on how to handle arriving processes''':

# Either read and schedule processes from the input file one line at a time. That is, once your code has
read the first process from the input, do not read in the next process until the current process is ready to be scheduled (i.e., its arrival time <= current time).

# Or, read all the processes from the input file and place them in a temporary queue (ordered by time of arrival). Move processes from the temporary to the ready queue when they are ready to be scheduled (i.e., their arrival time <= current time).

, based on their time of arrival, and the current simulated time.

Modularize your design using meaningfully named
functions.
Changed lines 141-142 from:
* '''Priority Scheduling''': The priority for each process will be defined as @1/f@, where @f@ is the fraction of the previous quantum used by that process. For example, assuming a time quantum of @50@, if a process runs for @25@ time units, then its fraction of used time is @25/50 = 1/2@. Hence, the priority of that process becomes @1/(1/2) = 2@
to:
* '''Priority Scheduling''': The priority for each process will be defined as @1/f@, where @f@ is the fraction of the previous quantum used by that process. For example, assuming a time quantum of @50@, if a process runs for @25@ time units, then its fraction of used time is @25/50 = 1/2@. Hence, the priority of that process becomes @1/(1/2) = 2@ ('''Note:''' You should initialize all incoming processes to the highest possible priority, so that every process will run at least once initially.)
Changed lines 28-30 from:
// argc equals the number of arguments received
// argv is an array containing all command line arguments
// argv[0] is set to the name of
the program
to:
/* argc equals the number of arguments received
argv is an array containing all command line arguments
argv[0] is set to the name of the program */
Changed line 77 from:
struct PBR *next; // a pointer to the next PBR in the queue (i.e. linked list)
to:
struct PBR *next; /* a pointer to the next PBR in the queue (i.e. linked list) */
Changed line 108 from:
// map random range to 0 through 99
to:
/* map random range to 0 through 99 */
Changed line 111 from:
// assuming an IO chance of 30%
to:
/* assuming an IO chance of 30% */
Changed line 113 from:
// process blocks and is rescheduled
to:
/* process blocks and is rescheduled */
Changed lines 115-118 from:
// process keeps running

// time is advanced by
1
// ...
to:
/* process keeps running */

/* time is advanced by
1 */
/*
... */
Added lines 7-8:
[--'''NOTE:''' This homework is an extension of [[Spring2011.CSCI340Homework3 | homework 3]]. Verbiage from homework 3 is included here to make this assignment self-contained. The extension involves adding two more scheduling algorithms (Priority and Round Robin). It also slightly changes the format of the input data.--]
Changed lines 56-58 from:
12345 0 5 # seed to the random number generator, the scheduler to use and the time quantum
0 1 4 30
# first process has ID=0, arrives at second 1, will execute in 4 seconds and has a 30 percent chance to block for IO
1 3 2 5 # ...
to:
12345 0 5 # seed to the random number generator, the scheduler to use and the time quantum
0 1 4 30
# process ID is 0, arrives at sec 1, will execute in 4 secs, has 30% chance to block for IO
1 3 2 5 # ...
Changed lines 37-41 from:
* The first value will be used as a seed to the random number generator @@srand()@@ (so results can be reproduced);

* the second value will define which scheduling algorithm should be used (FCFS = @@'f'@@, Priority = @@'p'@@, RR = @@'r'@@); and
to:
* The first value will be used as a seed to the random number generator @@srand()@@ (so results can be reproduced),
* the second value will define which scheduling algorithm should be used (FCFS = @@'f'@@, Priority = @@'p'@@, RR = @@'r'@@), and
Changed lines 43-44 from:
On each additional line, the input file will contain 4 integers describing a single process to be run. The first integer is the process ID, the second integer is the arrival time of the process, the third integer is the required execution time of the process (remember, we are simulating execution), and the forth integer is the probability the process will block for IO.
to:
On each additional line, the input file will contain 4 integers describing a single process to be run:
*
The first integer is the process ID,
*
the second integer is the arrival time of the process,
*
the third integer is the required execution time of the process (remember, we are simulating execution), and
* the forth integer is the probability the process will block for IO.
Changed lines 36-39 from:
The first line of the input file will contain 1 integer, 1 character, and 1 integer values. The first value will be used as a seed to the random number generator @@srand()@@ (so results can be reproduced), the second value will define which scheduling algorithm should be used (FCFS = @@'f'@@, Priority = @@'p'@@, RR = @@'r'@@) and the third will be the quantum/slice value to use for the preemptive algorithms.

You may assume that the first and second values are separated by a single space character only.
to:
The first line of the input file will contain 1 integer, 1 character, and 1 integer values:

*
The first value will be used as a seed to the random number generator @@srand()@@ (so results can be reproduced);

*
the second value will define which scheduling algorithm should be used (FCFS = @@'f'@@, Priority = @@'p'@@, RR = @@'r'@@); and

*
the third will be the quantum (time-slice) value to use for the preemptive algorithms.

You may assume that the first and second values are separated by a single space character.
Changed lines 36-37 from:
The first line of the input file will contain 1 integer, 1 character, and 1 integer values. The first value will be used as a seed to the random number generator @srand()@ (so results can be reproduced), the second value will define which scheduling algorithm should be used (FCFS = @@'f'@@, Priority = @@'p'@@, RR = @@'r'@@) and the third will be the quantum/slice value to use for the preemptive algorithms.
to:
The first line of the input file will contain 1 integer, 1 character, and 1 integer values. The first value will be used as a seed to the random number generator @@srand()@@ (so results can be reproduced), the second value will define which scheduling algorithm should be used (FCFS = @@'f'@@, Priority = @@'p'@@, RR = @@'r'@@) and the third will be the quantum/slice value to use for the preemptive algorithms.
Added lines 1-213:
'''Assigned Date''': Wednesday, Mar. 30, 2011\\
'''Due Date''': Friday, Apr. 8, 2011\\
'''Due Time''': 11:50am

Last modified on {$LastModified} (see [[http://www.cs.cofc.edu/~manaris/?n=Spring2011.CSCI340_Homework4?action=diff&source=n&minor=n | updates]])

This is a '''solo''' assignment. You '''must''' work alone.

!!Purpose

This assignment focuses on Process Management with C, and in particular:

* To gain experience with preemptive scheduling algorithms.
* To gain experience with the simulation of I/O- and CPU-bound processes.

!!Assignment

Create a process management simulation in C that implements a '''First-Come-First-Serve (FCFS)''', '''Priority''', and '''Round-Robin (RR)''' scheduling algorithms.

Read in process information from an ASCII file. The name of the file will be provided as input to your
program, using C command-line arguments.

(:source lang=c tabwidth=3 -trim:)
int main(int argc, char *argv[])
(
// argc equals the number of arguments received
// argv is an array containing all command line arguments
// argv[0] is set to the name of the program
}
(:sourcend:)

For example, see [1].

!!!Input

The first line of the input file will contain 1 integer, 1 character, and 1 integer values. The first value will be used as a seed to the random number generator @srand()@ (so results can be reproduced), the second value will define which scheduling algorithm should be used (FCFS = @@'f'@@, Priority = @@'p'@@, RR = @@'r'@@) and the third will be the quantum/slice value to use for the preemptive algorithms.

You may assume that the first and second values are separated by a single space character only.

On each additional line, the input file will contain 4 integers describing a single process to be run. The first integer is the process ID, the second integer is the arrival time of the process, the third integer is the required execution time of the process (remember, we are simulating execution), and the forth integer is the probability the process will block for IO.

You should ignore anything past the forth integer on each line. The file may contain an arbitrary number of processes.

Example file contents:

(:source lang=c tabwidth=3 -trim:)
12345 0 5 # seed to the random number generator, the scheduler to use and the time quantum
0 1 4 30 # first process has ID=0, arrives at second 1, will execute in 4 seconds and has a 30 percent chance to block for IO
1 3 2 5 # ...
2 6 7 80
(:sourcend:)

You may only assume that processes are sorted by arrival time.

!!!Details

Processes and their related information should be stored in ProcessBlockRecords (PBR) organized as a
queue in order of their arrival time. The PBRs should be represented as C structs, and the queue as a linked list.

Below is an example struct for the PBR. Depending on the scheduling algorithm, your struct may require additional fields (e.g. priority) - be sure to document the rationale for your changes/additions. The same data structure should be used for all three algorithms.

(:source lang=c tabwidth=3 -trim:)
struct PBR
{
int processID;
int processArrivalTime;
int remainingRunTime;
struct PBR *next; // a pointer to the next PBR in the queue (i.e. linked list)
};
(:sourcend:)

To allocate space for the C structs use @@malloc()@@. For example:

(:source lang=c tabwidth=3 -trim:)
struct PBR *head = (struct PBR *)malloc(sizeof(struct PBR));
(:sourcend:)

To deallocate the space use @@free()@@. For example:

(:source lang=c tabwidth=3 -trim:)
free(head);
(:sourcend:)

Your program should deallocate all the space it allocated. A good strategy is to deallocate the PBR of any process that has completed. Also, depending on how you implement your linked list, you may also use a function called @@cleanUp()@@, which takes as argument(s) the program's dynamic data structure(s). It should be called before the program exits.

Instead of maintaining actual time in your program, you should use a counter and treat every increment
made as the passing of 1 second.

In a real system, each process would perform some sort of work while executing, but, for simplicity,
this simulation will omit this detail.

To simulate IO- and CPU-bound processes, every process is provided with a probability of blocking for IO in the input file. For each unit of time a process is running, a check must be made to see if the process should block.

Again, in a real system, a process that blocks for IO would normally be blocked for some duration while the IO occurs. However, for this assignment, we are concerned only with simulating the blocking of each process. So, for this assignment, you can assume that once a process blocks it can be immediately rescheduled.

To check if a process will block, use the C @@rand()@@ function for every unit time that the process is running. For example:

(:source lang=c tabwidth=3 -trim:)
// map random range to 0 through 99
int result = rand() % 100;

// assuming an IO chance of 30%
if (result < 30)
// process blocks and is rescheduled
else
// process keeps running

// time is advanced by 1
// ...
(:sourcend:)

To make sure all executions of your code can be reproduced, make a call to the @srand(seed)@ function near the beginning of your program, where @@seed@@ is provided in the input file.

Lastly, you should only read and schedule processes from the input file 1 line at a time. That is, once your code has read the first process from input, don't read in the next process until the current process is ready to be scheduled (i.e. it's arrival time = current time).

Feel free to modularize your design using meaningful functions.

!!!Scheduling

For this assignment, you will be implementing three separate scheduling algorithms. The algorithm to be used is indicated by the second integer on the first line of the input file.

Implement the following scheduling algorithms:

* '''First Come First Serve (FCFS)''': There will be no time quantum/slice, therefore the implemented algorithm will be non-preemptive.

* '''Priority Scheduling''': The priority for each process will be defined as @1/f@, where @f@ is the fraction of the previous quantum used by that process. For example, assuming a time quantum of @50@, if a process runs for @25@ time units, then its fraction of used time is @25/50 = 1/2@. Hence, the priority of that process becomes @1/(1/2) = 2@

* '''Round Robin''': Using the quantum value provided via the input file, implement the Round Robin algorithm.

!!!Output

Upon completion of the simulation, i.e., when all processes have finished executing, your program should output the following statistics:

(:source lang=c tabwidth=3 -trim:)
average wait time: x

average turnaround time: y

average execution time: z
(:sourcend:)

where @@x@@, @@y@@, @@z@@ are float values.

'''Average wait time:''' Average (across all processes) of elapsed time between a process' arrival time and the moment it is allocated the CPU.

'''Average turnaround time:''' Average (across all processes) of elapsed time between a process' arrival time and its completion.

'''Average execution time:''' Average of all the processes' run times.

!!Documentation

Follow the Golden Rule of Style: "A program should be as easy for a human being to read and understand as it is for a computer to execute." [2]

In general, you should comment any variable, obscure statement, block of code, etc. you create.

Also, you should comment '''why''' something is being done, e.g.,

(:source lang=C tabwidth=3 -trim :)
numStudents += 1; /* we have processed one more student */
(:sourcend:)

as opposed to '''how''' it is done, e.g.,

(:source lang=C tabwidth=3 -trim :)
numStudents += 1; /* increment numStudents by one */
(:sourcend:)

Finally, your code should always include opening comments as follows.

(''NOTE:'' Angle brackets signify information that needs to be filled out. '''Remove''' the angle brackets!)

(:source lang=C tabwidth=3 -trim :)
/*
Author: <Your Name(s)>
Email: <Your email address(es)>
Class: CSCI 340, Section 1
Assignment: HMWK4
Due Date: <The assignment's due date>

Certification of Authenticity <remove one of the following>:

I certify that this lab is entirely my own work.

I certify that this lab is my own work, but I received
some assistance from: <Name(s)>

TASK: <Provide a simple, yet complete description of the task being
performed by this program. It may be several sentences long.>

INPUT: <Describe the input to this program. Be thorough.>

OUTPUT: <Describe the output to this program. Be thorough.>

*/
(:sourcend:)

!!Submissions

You will submit your assignment via the stono @@submit@@ command, as follows:

% submit csci340 hmwk4 processScheduler.c

No other submission mechanism will be accepted (e.g., email).

!!Grading

Your assignment will be graded based on the documentation, formatting, and correctness of your source code. Also the completeness / thoroughness of your work, and how well you followed the homework instructions.

!!Reference

# Cprogramming.com, [[http://www.cprogramming.com/tutorial/c/lesson14.html | Accepting command line arguments]], accessed on-line, March 20, 2011.

# Cooper, D. and Clancy, M. (1985) "Oh! Pascal", 2nd ed., W.W. Norton & Company, New York, p. 42.