4330/6310 first assignment spring 2015 jehan-françois pâris

61
4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris [email protected]

Upload: madeline-tamsyn-nash

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

AN EXAMPLE

TRANSCRIPT

Page 1: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

4330/6310 FIRST ASSIGNMENTSpring 2015

Jehan-François Pâ[email protected]

Page 2: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The modelWe haveOne quad-core CPUOne diskOne input deviceThree queues

CPU queue "ready queue"

Disk queueDevice queue

RQ

DQ

Disk

CCCC

IQ

Input

Page 3: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

AN EXAMPLE

Page 4: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P0 begins at t = 0

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30

RQ

DQ

Disk

CCCC

IQ

Input

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

Page 5: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P0 gets core until t = 0 + 200 = 200

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30

RQ

DQ

Disk

CCCC

IQ

Input

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

Page 6: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?

RQ

DQ

Disk

CCCC

IQ

Input

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

Page 7: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P1 arrives at t = 100NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 8: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P1 gets core until t = 100 + 30 = 130NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 9: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 10: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P1 waits for input until t = 130 +800 = 930 NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 11: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next? NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 12: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P0 waits for input device NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 13: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 14: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P1 gets core until t = 930 + 40 = 970P0 waits for input until t = 930 + 900 = 1830

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 15: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 16: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P1 terminates at t = 970

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 17: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Your program will display (I)

Process 1 terminates at t = 970Core 0 is IDLECore 1 is IDLECore 2 is IDLECore 3 is IDLEDisk is IDLEAverage number of busy cores:

Ready queue contains: --Disk queue contains: --

Page 18: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Your program will display (II)

Process ID Start time CPU time Status0 0 200 WAITING1 100 70TERMINATED

Page 19: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

How to compute CPU utilization

Keep track of total time for all CPU requests:200 + 30 + 40 = 270 ms

Divide by elapsed time:270/970 = 0.278 (rounded)

Since there are four cores, the maximum CPU utilization is 4.0

Page 20: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 21: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P0 gets core until t = 1830 +10 = 1840

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 22: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 23: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P0 gets disk until t = 1840 + 10 = 1850

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 24: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 25: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P0 gets CPU until t = 1850 + 30 =1880

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 26: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

What's next?

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 27: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

P0 terminates at t = 1880

NEW 0START 0CPU 200INPUT 900CPU 10I/O 10CPU 30NEW 1START 100CPU 30INPUT 800CPU 40

RQ

DQ

Disk

CCCC

IQ

Input

Page 28: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Your program will display (I)

Process 0 terminates at t = 1880Core 0 is IDLECore 1 is IDLECore 2 is IDLECore 3 is IDLEDisk is IDLEAverage number of busy cores: 0.174

Ready queue contains: --Disk queue contains: --

Page 29: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Your program will display (II)

Process ID Start time CPU time Status0 0 240TERMINATED

Page 30: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

How to compute CPU utilization

Keep track of total time for all CPU requests:200 + 10 + 30 + 30 + 40 = 310 ms

Divide by elapsed time:310/1880 = 0.165 (rounded)

Since there are four cores, the maximum CPU utilization is 4.0

Page 31: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

HANDLINGZERO-DELAYDISK ACCESSES

Page 32: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Zero-delay disk accesses

Represent disk requests that can be satisfied without actually accessing the disk:Data can be read from the I/O bufferData are written to the I/O buffer

A process performing a zero-delay I/O request will:Skip the diskStart without further delay its next processing

step

Page 33: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

An example (I)…CPU 5I/O 0CPU 20…

RQ

DQ

Disk

CCCC

IQ

Input

Assume next stepfor red process isI/O 0

Page 34: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

An example (II)…CPU 5I/O 0CPU 20…

RQ

DQ

Disk

CCCC

IQ

Input

Red process willimmediatelyrequest a core(and get one becauseone or more coreswere idle)

Page 35: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

ENGINEERING THE SIMULATION

Page 36: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Simulating time

Absolutely nothing happens to our model between two successive "events"

Events areArrival of a new processStart of a computing stepCompletion of a computing step

We associate an event routine with each event

Page 37: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Arrival event routine

Process first request of process It will always be a CPU request

Page 38: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

CPU request routine

current time is clockrequest time is crt

if a core is free :mark core busy until clock + crt

add crt to corebusytimeselse :

enter process in ready queue

Page 39: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

CPU request completion routine

if ready queue is empty :mark core idle

else:pick first process P' in ready queuecrt' is request time for P'mark core busy until clock + crt'

add crt' to corebusytimes proceed with next request for completing

process

Page 40: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Disk request routine

current time is clockrequest time is drt

if drt == 0: proceed with next process request

if disk is free :mark disk busy until clock + drt

add drt to diskbusytimeselse :

enter process request in disk queue

Page 41: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Disk request completion routine

if disk queue is empty :mark disk idle

else :pick first process request P' in disk queuedrt' is request time for P'mark disk busy until clock + drt'

add drt' to diskbusytimes proceed with next request for completing

process

Page 42: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Input request routine

current time is clockrequest time is irt

if input device is free :mark input device busy until clock + irt

else :enter process request in device queue

Page 43: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Input request completion routine

if input queue is empty :mark input device idle

else :pick first process request P' in

input queueirt' is request time for P'mark disk busy until clock + irt'

proceed with next request for completing process

Page 44: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The simulation scheduler

1. Find next event by looking at: CPU request completion times Disk request completion time time Input request completion time Arrival time of next process

2. Set current time to event time3. Process event routine4. Repeat until all processes are done

Page 45: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Organizing our program (I)

Most steps of simulation involve scheduling future completion events

Associate with each completion event an event noticeTime of eventDevice Process sequence number

Page 46: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Organizing our program (II)

Do the same with process startsTime of eventProcess start Process sequence number

Page 47: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Organizing our program (III)

Process all event notices in chronological orderRelease

disk247

Releasecore250

Newprocess

245

Newprocess

270

Newprocess

310

First notice tobe processed

Page 48: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Organizing our program (IV)

Keep the event list sorted (priority queue)

Releasedisk247

Releasecore250

Newprocess

245

Newprocess

270

Newprocess

310

First notice to be processed is head of the list

Page 49: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Organizing our program (V)

Overall organization of main program

schedule first event # will be a process startwhile event list is not empty :

process next event in listprint simulation results

Page 50: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Organizing our event list

Priority queue Two kinds of entries

Computational steps completion times: Created and inserted "on the fly"

Process arrivals: Created during input phase Already sorted

Page 51: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

An implementation

My main data structures would be:Data tableProcess tableDevice table

Page 52: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The data table

Stores the input data Line indices are used

in process table

Operation Parameter

NEW 5

CPU 10

INPUT 0

CPU 20

NEW 20

CPU 50

… …

Page 53: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The process table (I)

StartTime

First Line

Last Line

CurrentLine

5 0 3 varies

20 4 … …

… … … …

Page 54: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The process table (I)StartTime

First Line

Last Line

CurrentLine

5 0 3 varies

20 4 … …

… … … …

Page 55: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The process table (II)

One line per processLine index is process sequence number!

First column has start time of process First line, last line and current line respectively

identify first line, last line and current line of the process in the input table

Last column is for processes waiting for end of an INPUT step

Page 56: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The device table (I)DeviceDevice Process Completion time

Core 0Core 0 P0 15Core1Core1Core 2Core 2Core 3Core 3 - -DIskDIskInputInput - -

First column is not needed:use row index to specify device

Page 57: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

The device table (II)

One line per device (core, disk and Input) Contains

A busy/free flagA process sequence number if device is busyA completion time

Zero (or a very large value) if device is free

Page 58: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Finding the next event (I)

If you use a priority list for your events, you should start by "seeding" your priority list with the arrival times of all processes

After that, you add successive events to the list each time you can allocate a resource to a process.

Your simulation will end once the list is empty (and you cannot add anything to it).

Page 59: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Finding the next event (II)

If you do not use a priority list for your events, you can find the next event to process by searching the lowest value in The process start times in the process tableCompletion times of INPUT stepsThe completion times in the device table

Page 60: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

A full list implementation

INT…0

INT…5

RUN10

RUN20

… …

Very elegant but harder to debug

Page 61: 4330/6310 FIRST ASSIGNMENT Spring 2015 Jehan-François Pâris

Reading your input

You must use I/O redirectionassign1 < input_file

AdvantagesVery flexibleProgrammer writes her code as if it was read

from standard input No need to mess with fopen(), argc and argcv