Vous êtes sur la page 1sur 7

Programming Assignment #1, CSci 530,

Winter-Mini 2014/2015
Derek Harter
2014-12-15

Contents
1 Dates:

2 Objectives:

3 Description:

Dates:
Assigned: Thursday December 18, 2014
Due:
Sunday December 21, 2014 (before Midnight)

Objectives:
Explore the Process state models from an implementation point of

view.

Use C/C++ data structures to implement a process control block and

round robin scheduling queues.

Description:

In this assignment you will simulate a Three-State process model (ready,


running and blocked) and a simple process control block structure as discussed in Chapter 3. Your program will read input and directives from a
1

le. The input describes a time sequence of events that occur. These are the
full set of events you will simulate:
Event
cpu
new
done
wait X

Description
The processor runs for 1 time step the currently running process
A new process is created
The currently running process has nished
The currently running process has done an I/O operation and
is waiting on event X
event X Event X has occurred, all processes waiting on that event should
be made ready.
The input le will simply be a list of events that occur in the system, in
the order they are to occur. For example:
----- events-example.sim -------new
cpu
cpu
wait 1
cpu
event 1
cpu
cpu
done
----------------------------------

Your task is to read in the events, and simulate the creation and execution
of processes in the system as they move through the various three-states of
their process life cycle. You need to
Dene a simple process control block (PCB) to hold information about

all processes currently running in your system. The PCB can be a


simple C struct or a C++ class. At a minimum you need to have
a eld for the process identier, the process state (Ready, Running
or Blocked). You need to also keep track of the time step that the
process entered the system, and the number of steps the process has
been running.

You will need a ready and blocked queue of some kind. You can use a

simple C array to hold your queues, though you will need to implement
2

some simple queuing functions in that case. You may also use the C++
Standard Template Library (STL) vector and/or queue data structures
to implement your queues.
You will need to implement a simple dispatcher function. Whenever a

cpu event occurs, and no process is currently running, you should select
the next Ready process from your ready queue and start it running on
the processor.

You need to also implement a simple time slicing mechanism. You

should dene a time quantum of 5 time steps for your system (but this
should be a parameter, so that you can change the time quantum for
dierent simulations). At the end of a cpu cycle, you should check if the
currently running process has executed for its full time quantum. In
that case, the currently running proces should timeout, and be returned
to the end of the Ready queue.

new events should cause a new process to be created (including creating

its PCB and lling it in). New events will be placed on the ready
queue after being created. You should assign each new event a process
identier. The process identier should be a simple integer value, and
you should start numbering processes from 1 .

For a done event, if a process is currently running it should then be

released. It should be removed from the cpu, and not placed back on
the ready or blocked queue.

A wait event simulates the currently running process performing some

I/O operation. If a wait occurs, the currently running process should


become blocked and put on the blocked queue. You also need an entry
in the PCB so you know what event the process is waiting for. The
wait event is followed by an integer number, which is an indication of
the type of event the process has requested.

Likewise the event directive simulates the nishing of some I/O op-

eration. When an event occurs, you should scan your blocked queue
and make any jobs ready that were waiting on that event. The integer
value following an event indicates the type of event that just occurred.

You have been given some example event sequences (simulation-01.sim,


simulation-02.sim, etc.) along with the expected output for those sequence
of events (simulation-01.res, simulation-02.res, etc.). The output of your
3

program should be sent to standard output. The output correct output for
the simulation-01.sim simulation is:
Time: 1
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=1,
Ready Queue:
EMPTY
Blocked Queue:
EMPTY
Time: 2
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=2,
Ready Queue:
EMPTY
Blocked Queue:
EMPTY
Time: 3
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=3,
Ready Queue:
EMPTY
Blocked Queue:
EMPTY
Time: 4
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=4,
Ready Queue:
pid=2, state=READY, start=4, slice=0,
Blocked Queue:
EMPTY
Time: 5
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=5,
Ready Queue:
pid=2, state=READY, start=4, slice=0,

Blocked Queue:
EMPTY
Time: 6
CPU (currently running):
pid=2, state=RUNNING, start=4, slice=1,
Ready Queue:
pid=1, state=READY, start=1, slice=5,
Blocked Queue:
EMPTY
Time: 7
CPU (currently running):
pid=2, state=RUNNING, start=4, slice=2,
Ready Queue:
pid=1, state=READY, start=1, slice=5,
Blocked Queue:
EMPTY
Time: 8
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=1,
Ready Queue:
EMPTY
Blocked Queue:
pid=2, state=BLOCKED, start=4, slice=2, event=1
Time: 9
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=2,
Ready Queue:
EMPTY
Blocked Queue:
pid=2, state=BLOCKED, start=4, slice=2, event=1
Time: 10
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=3,
Ready Queue:
pid=2, state=READY, start=4, slice=2,

Blocked Queue:
EMPTY
Time: 11
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=4,
Ready Queue:
pid=2, state=READY, start=4, slice=2,
Blocked Queue:
EMPTY
Time: 12
CPU (currently running):
pid=1, state=RUNNING, start=1, slice=5,
Ready Queue:
pid=2, state=READY, start=4, slice=2,
Blocked Queue:
EMPTY
Time: 13
CPU (currently running):
pid=2, state=RUNNING, start=4, slice=1,
Ready Queue:
pid=1, state=READY, start=1, slice=5,
Blocked Queue:
EMPTY
Time: 14
CPU (currently running):
pid=2, state=RUNNING, start=4, slice=2,
Ready Queue:
pid=1, state=READY, start=1, slice=5,
Blocked Queue:
EMPTY

Your output to standard out should look exactly the same as this output
(i.e. if I do a di and your program is generating the correct output, then
there will be no dierence between the output your program generates and
the above output format). The output is generated by displaying the system
state after each `cpu' cycle executes. Basically we print out the system time.
6

Then we show which process (if any) is currently running on the CPU (or
say it is IDLE if no process is running). Then we display the queue of
processes currently on the Ready and Blocked queues. Note that the queues
are displayed in order. The top of the output corresponds to the head of the
queue. Thus when a new process is dispatched, the next one selected should
be the rst process listed from the ready queue in the previous system cycle.
Also you are required to conform to the coding guidelines for this course,
and we will be returning programs without looking at them if they do not
meet minimal standards or fail to compile and run on a standard ANSI C
compiler. Please look in eCollege at the bottom of the course left sidebar,
for the programming assignment style and execution guidelines.
I have given you some template code (p1-start.cpp) to get you started
The code is meant to be run from the command line, thus from a shell or
dos prompt you would do something like:
$ p1-start simulation-01.sim

i.e. the program expects a single parameter on the command line, which
should be the name of a le that holds the events to be simulated, one on
each line. If you need to test your program and can't gure out how to invoke
running it from the command line, you can change the line in `p1-start.cpp'
to explicitly run a particular simulation le, like this:
runSimulation("simulation-01.sim")

However, you need to make sure that your program correctly works using
the command line invocation, as shown in `p1-start.cpp`. Failure to run from
the command line as required and shown will result in points being deducted
from your submission, and possibly in programs being returned with a 0
grade.
The runSimulation() in `p1-start.cpp1 holds example code and indicates
locations where you need to write your own functions to implement the
simulation. You can use this as a starting point to implement your solution.

Vous aimerez peut-être aussi