280 likes | 407 Views
MY SOLUTION FOR THE 4330/6310 FIRST ASSIGNMENT Summer 2014. Jehan-François Pâris jfparis@uh.edu. Why?. To be able to give better guidance to students To show it can be done. How?. I wrote the program in Python 3 Greatly simplifies list management
E N D
MY SOLUTION FOR THE4330/6310 FIRST ASSIGNMENTSummer 2014 Jehan-François Pârisjfparis@uh.edu
Why? • To be able to give better guidance to students • To show it can be done
How? • I wrote the program in Python 3 • Greatly simplifies list management • Useful refresher for my Python skills before the fall semester • The outcome • Less than 200 lines of code • Eleven functions
C C S Q RQ IN SSD The model We have • One dual-core CPU • One SSD • One input device • Two queues • CPU queue "ready queue" • SSD queue
Major data structures • Process list • To store input data • Event list • Core status • Ready queue • Disk status • Disk queue
INPUT5100 CPU100 CPU20 CPU20 SSD0 CPU80 The process list (I) processlist[0] CPU20 processlist[1]
The process list (II) • Process list • An array of linked lists • One linked list per process • Each list contains all computational steps for a process • Will pop them out while going through the steps • Array index is process ID
5ARRIVAL 0 35CORE 0 100ARRIVAL 1 100ARRIVAL 1 100ARRIVAL 1 5105INPUT 0 The event list (I) At starttime Next Next
The event list (II) • Linked list containing future events • Process arrivals • CPU request completions • SSD request completions • Input request completions • Each entry contains • The event time (sort key) • The event type • The ID of the process
The cores' status • An integer between 0 and 2 • 2 means both cores are free • 0 means both cores are busy • Must queue incoming processes • Will be • Decremented when a process grabs a core • Incremented when a process releases a core
The ready queue • Contains processes waiting for the CPU • Each entry contains • A process ID • A request duration • FIFO queue
The SSD status • An integer between 0 and 1 • 1 means SSD is free • 0 means SSD us busy • Must queue incoming processes • Will be • Decremented when a process grabs the SSD • Incremented when a process releases it
The SSD queue • Contains processes waiting for the SSD • Each entry contains • A process ID • A request duration • FIFO queue
A less critical data structure • The process table • Array of strings • Indexed by process IDs • The possible values: • READY • RUNNING • WAITING
Program organization • Initialization • The main loop • Printing the summary
Initialization • Read the input file • Populate • Event list with event notices for process arrivals • Process list with computational steps of each process
Eventlist INPUT5100 CPU100 CPU20 SSD0 5ARRIVAL 0 100ARRIVAL 1 CPU80 The outcome (I) Process list= Array of lists CPU20
The outcome (I) --- --- pid = 0 pid = 1 Process table "---" stands here for undefined status
The main loop while eventlist is not empty :get next event from event list # it specifies a time, an event, and a process idset clock to time of eventif event == 'ARRIVAL' : arrival(pid)elif … All events are either arrivals or step completions
The arrival event function def arrival(pid) : get first request from processlist[pid] # it specifies a request type and its duration if request == 'CPU': core_request(pid, duration) else : print('PANIC!')
The core request function def core_request(pid, duration) : if a core is free : update number of free cores process_table[pid] = "RUNNING" schedule core completion event at time clock + duration for process pid else : process_table[pid] = "READY" enter process pid and request duration in ready queue
The core completion function def core_completion(pid) : clock = time if ready queue is empty : update number of free coreselse: pick first process anotherpid in ready queue anotherduration is its request duration process_table[anotherpid] = "RUNNING" schedule core completion event at time clock + anotherduration for process anotherpidnext_request(pid)
Other request/completion functions • SSD request and SSD completion functions are very similar to the core request and completion functions • There is only one SSD • Must keep track of SSD service times • Process status will always be "WAITING" • INPUT request and completion functions are trivial
The nextrequest function def nextrequest(pid) : if processlist[pid] is empty : process_table[pid] = 'TERMINATED' else : get next request from processlist[pid] if request == 'CPU' : core_request(pid, duration) elif …
How the functions call each other • Main loop processes an event notice and calls either a process arrival function or arequest completion function • In either case, the function ends with a call to the next request function • This function will call a resource request function • Unless the requested resource is busy, the request function will create an event notice
Main funtion processes an arrival event Arrival function Next step function An example Core request function New core completion event
Core completion Next step function Another example Main function processes an core completion event SSD request function New SSD completion event
Main function processes an SSD completion event SSD completion Next step function A last example Core request function New core completion event