Presentation is loading. Please wait.

Presentation is loading. Please wait.

PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time.

Similar presentations


Presentation on theme: "PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time."— Presentation transcript:

1 PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time and importance Process Queue Insert Process into Process Queue Remove Process of highest priority

2 PS3-Slides Using an Abstract Data Type Application program Module Implementation Module Interface Specification Process Manager Priority Queue Abstract Data Type

3 PS3-Slides Priority Queue Abstract Data Type Interface Specification 1) Application must define WHAT is stored. PQ ADT does not really need it (if “generic”). typedef struct { int id; /* assigned automatically and sequentially from 1 */ int size; /* memory requirements for process */ int time; /* time requirement for process */ int importance; /* insignificant = 0; average = 1; urgent = 2 */ int priority; } Process; 2) Functions the Priority Queue needs from the Application: bool Order(GenericPtr x, GenericPtr y);

4 PS3-Slides Priority Queue Abstract Data Type Interface Specification 3) Public Functions Available to Application from Priority Queue: void PQInitialize(member_type type, PriorityQueue *PQ, bool (* Order)(GenericPtr, GenericPtr)) bool PQEmpty(PriorityQueue *); bool PQFull(PriorityQueue *); int PQSize(PriorityQueue *); void PQInsert(GenericPtr, PriorityQueue *); GenericPtr PQRemove(PriorityQueue *);

5 PS3-Slides Communicating Between Application Program and Module /* Application Program File */ #include … #include ”Application.h" /* definitions of items and structures used in the application program, and prototypes of application functions the module needs */ #include ”ModuleInterface.h" /* public data and function prototypes in the module */ void ApplicationFunction1(void) { … } … int main(void){ … }

6 PS3-Slides Communicating Between Application Program and Module /* Module File */ #include … #include "ModuleInterface.h" /* see previous comments */ void ModuleFunction1(void){ … } …

7 PS3-Slides Application program Module Implementation 1 Module Interface Specification Application program Module Implementation 2 Module Interface Specification Ideally, these are identical These are different Changing Module Implementations

8 // Common Type definitions: // To be cast as needed - user or default… typedef void * GenericPtr; // Priority Queue Types supported (could be) enum member_type {Char, Int, Long, Float, Double, Proc}; // The Priority Queue Header: typed, order function // Notice that Content will point to the actual // implementation - invisible from outside. typedef struct{ member_type type; bool (* order)(GenericPtr, GenericPtr); int Count; GenericPtr Content; } PriorityQueue; PS3-Slides

9 Priority Queue Implemented with a Sorted Linked List Count Content NIPtr Link NIPtr Link NIPtr Link The Priority Queue typedef struct PQNodeTag { GenericPtrNodeItemPtr; struct PQNodeTag *Link; } PQListNode; type order Process typedef struct { int id; int size; int time; int importance; int priority; } Process;

10 PS3-Slides Priority Queue Implemented with Unsorted Array The Priority Queue ((GenericPtr *)(PQ->Content))[0] ((GenericPtr *)(PQ->Content))[1] ((GenericPtr *)(PQ->Content))[2] ((GenericPtr *)(PQ->Content))[3] ((GenericPtr *)(PQ->Content))[Count-1] ((GenericPtr *)(PQ->Content))[MAXCOUNT-1] Process PQ->Content = malloc(MAXCOUNT*sizeof(GenericPtr)); Items in order of INSERTION. Count Content type order

11 Process Management using a Priority Queue This program uses a Priority Queue to manage processes. The list can hold a maximum of 50 jobs. Using Sorted Linked-list Implementation of Priority Queue. MAKE SURE YOUR APPLICATION PROGRAM USES '#include PQImpl1.h'. Use the H command anytime to list valid commands. Valid commands are: A - Add a process to the Priority Queue R - Remove the highest priority process from the Priority Queue C - Show the number of processes in the Priority Queue H - List valid commands Q - Quit Command> a New process job: Size: 4 Time: 12 Importance (insignificant=0, average=1, or urgent=2): 1 Job id = 1 size = 4 time = 12 importance = 1 priority = 4 PS3-Slides

12 Command> a New process: Size: 2 Time: 8 Importance (insignificant=0, average=1, or urgent=2): 1 Job id = 2 size = 2 time = 8 importance = 1 priority = 2 Command> a New process: Size: 7 Time: 22 Importance (insignificant=0, average=1, or urgent=2): 1 Job id = 3 size = 7 time = 22 importance = 1 priority = 7 Command> c Number of jobs in Process Queue is 3 Command> r Highest priority process removed: Job id = 3 size = 7 time = 22 importance = 1 priority = 7 Command>

13 PS3-Slides What are we trading? The “regular” PS3 implementation requires a lot of information flowing back and forth, and allows only one type of Priority Queue to exist within a program. It also requires recompilation of both Application Program AND library module every time one switches from one implementation to another. It is relatively “type safe”, since the compiler can figure out statically whether you are trying to insert or retrieve objects of the wrong type.

14 This (“generic”) implementation requires no information flowing from the application program to the library module, and allows multiple types of Priority Queues to exist within a program. It does not require recompilation of either Application Program or module to change from one implementation to another: linking of object code files suffices (unless we want to extend the family of types we associate with the Priority Queue). All type safety is lost: only careful discipline on the part of the programmer will avoid disasters - almost no help will be available from the compiler. It is also somewhat more complex to implement. PS3-Slides


Download ppt "PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time."

Similar presentations


Ads by Google