22
Module R2 Module R2 Overview Overview

Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different

Embed Size (px)

Citation preview

Module R2Module R2OverviewOverview

Process queuesProcess queues

As processes enter the system and transition As processes enter the system and transition from state to state, they are stored queues.from state to state, they are stored queues.

There may be many different types of queues: There may be many different types of queues: Job queue: jobs waiting to be loaded as Job queue: jobs waiting to be loaded as

processes.processes. State queues: queues for each process State queues: queues for each process

state.state. Device queues: processes waiting for access Device queues: processes waiting for access

to a particular device.to a particular device.

Executing ProcessesExecuting Processes

Process scheduling and dispatching involves:Process scheduling and dispatching involves:

Creating processes as new jobs are admitted to Creating processes as new jobs are admitted to the system. the system.

Maintaining various queues, and moving Maintaining various queues, and moving processes from one queue to another.processes from one queue to another.

Using some algorithm to select a process from Using some algorithm to select a process from the ready queue, and loading and giving control the ready queue, and loading and giving control to the selected process.to the selected process.

R2: Implement the control R2: Implement the control structures to:structures to:

Implement 2 abstract data typesImplement 2 abstract data types PCBsPCBs Doubly linked queuesDoubly linked queues

Operations Operations Create, initialize, and destroy PCB’sCreate, initialize, and destroy PCB’s Insert and remove PCB’s from queuesInsert and remove PCB’s from queues

It will NOT load and dispatch processesIt will NOT load and dispatch processes

2 layers to R22 layers to R2

Internal layerInternal layer Defining abstract data types and the Defining abstract data types and the

operations for objects of those typesoperations for objects of those types These functions will be invoked, by These functions will be invoked, by

operations implemented at the user operations implemented at the user interface levelinterface level

User interface layerUser interface layer Add new commands to COMHAN to allow Add new commands to COMHAN to allow

the testing of your internal operationsthe testing of your internal operations

Process QueuesProcess Queues

Sample structureSample structureTypedef struct queue {Typedef struct queue {

PCB *front;PCB *front;PCB *rear;PCB *rear;

} Queue;} Queue; We suggest you implement a separate We suggest you implement a separate

queue for each possible process state: queue for each possible process state: ready, suspended ready, blocked, ready, suspended ready, blocked, andand suspended blocked.suspended blocked.

Each queue is a doubly linked list!!!!!

Ready queue

Front

Rear

Process10

Previous

Next = NULL

Process3

Previous

Next

Process5

Previous

Next

Proess1

Previous=Null

Next

Priority = 50 Priority = 10Priority = 50Priority=120

We implement the queues as a doubly linked list We implement the queues as a doubly linked list because:because:

Occasionally, a process to be terminated will Occasionally, a process to be terminated will be in the middle of the queue, it is more be in the middle of the queue, it is more efficient to locate and “unlink it”, than to use efficient to locate and “unlink it”, than to use traditional queue operationstraditional queue operations

Also, the ready queue is maintained in Also, the ready queue is maintained in “priority” order, so new processes may be “priority” order, so new processes may be enqueued into the middle of the queue!enqueued into the middle of the queue!

Queue OperationsQueue Operations

Find PCB: Find PCB: Searches for a specific PCB by process Searches for a specific PCB by process

name.name. It will “search” one or more queuesIt will “search” one or more queues If the process is not found a “NULL” If the process is not found a “NULL”

pointer will be returned.pointer will be returned. If found the function will return a pointer to If found the function will return a pointer to

the PCBthe PCB

Insert PCBInsert PCB Traditional FIFO insertionTraditional FIFO insertion

For the blocked, suspended blocked, and For the blocked, suspended blocked, and suspended ready queue, we will implement the suspended ready queue, we will implement the traditional FIFO enqueue.traditional FIFO enqueue.

New processes are added at the END of the New processes are added at the END of the queue.queue.

Priority insertionPriority insertion For the READY queue, we will implement a For the READY queue, we will implement a

“priority” insertion algorithm“priority” insertion algorithm New processes are inserted into the queue IN New processes are inserted into the queue IN

FRONT of all processes with a lower priorityFRONT of all processes with a lower priority

Ready queue

Front

Rear

Process10

Previous

Next = NULL

Process3

Previous

Next

Process1

Previous=Null

Next

Process5

Previous

Next

Priority = 50 Priority = 10Priority = 50Priority=120

Process20

Previous

Next

Priority = 50

Remove PCBRemove PCB Receives a pointer to the PCBReceives a pointer to the PCB A pointer to a queueA pointer to a queue UNLINKS the PCB from the queueUNLINKS the PCB from the queue

May be called after a “find PCB” request May be called after a “find PCB” request remove a specific PCB from a queue.remove a specific PCB from a queue.

You may want to also implement a You may want to also implement a traditional “dequeue” operation.traditional “dequeue” operation.

Process Control Blocks (PCBs)Process Control Blocks (PCBs) Suggested structureSuggested structure

// PCB structure// PCB structuretypedef struct PCB{typedef struct PCB{ struct IOCB iocb;struct IOCB iocb; char pname[20]; char pname[20]; //Process name at least 8 characters//Process name at least 8 characters int pclass; int pclass; //A code identifying the process as an //A code identifying the process as an //application process or a system process//application process or a system process int priority; int priority; // range -128 ~ +127 // range -128 ~ +127 int state; int state; // Running, Ready, Blocked, Suspended Ready, or// Running, Ready, Blocked, Suspended Ready, or

// Suspended blocked// Suspended blocked struct PCB * previous; struct PCB * previous; // pointer of the previous PCB in the queue// pointer of the previous PCB in the queue struct PCB * next; struct PCB * next; // pointer of the next PCB in the queue // pointer of the next PCB in the queue

unsigned char * stack_base; unsigned char * stack_base; // Stack pointer to the base// Stack pointer to the base unsigned char *stack_p; unsigned char *stack_p; //stackptr //stackptr

int memory;int memory; unsigned char *load; unsigned char *load; //pointer to where the process is loaded //pointer to where the process is loaded unsigned char *execution; unsigned char *execution; //pointer to execution addr //pointer to execution addr} PCB;} PCB;

PCB operationsPCB operations

Allocate PCBAllocate PCB

This operation will dynamically allocate a This operation will dynamically allocate a “new” PCB and return a pointer to it.“new” PCB and return a pointer to it.

If the allocation fails, a NULL pointer If the allocation fails, a NULL pointer should be returnedshould be returned

Free PCBFree PCB

Given a pointer to a PCB, this function is Given a pointer to a PCB, this function is responsible for:responsible for:

Freeing all storage contained in the PCBFreeing all storage contained in the PCB Freeing the space associated with the PCBFreeing the space associated with the PCB Setting the incoming pointer to NULLSetting the incoming pointer to NULL

Setup PCBSetup PCB

Initializes the contents of a PCB!!!Initializes the contents of a PCB!!! Receives a pointer to a PCBReceives a pointer to a PCB Input parameters for Name, priority, class, Input parameters for Name, priority, class,

and state (always “ready” for R2)and state (always “ready” for R2) Allocate memory for the stack and Allocate memory for the stack and

initialize the stack pointer (not used in R2)initialize the stack pointer (not used in R2) Allocate a memory area for the process. Allocate a memory area for the process.

(this is not used in R2, so the memory size (this is not used in R2, so the memory size should be set to zero and all related should be set to zero and all related pointers set to NULL!!!!!!)pointers set to NULL!!!!!!)

User Interface operations!User Interface operations!

Create_PCBCreate_PCB Allocates and sets-up a new PCB, it receives Allocates and sets-up a new PCB, it receives

arguments for name, class and priorityarguments for name, class and priority Delete_PCBDelete_PCB

Given a process name, this function will “find”, Given a process name, this function will “find”, “remove”, and “free” a process!“remove”, and “free” a process!

BlockBlock This function moves a process from the ready queue This function moves a process from the ready queue

to the blocked queue (or from one suspended queue to the blocked queue (or from one suspended queue to the other.)to the other.)

It receives the process name as an argumentIt receives the process name as an argument

UnblockUnblock This function moves a process from the This function moves a process from the

blocked queue to the ready queue (or from blocked queue to the ready queue (or from one suspended queue to the other.)one suspended queue to the other.)

It receives the process name as an argumentIt receives the process name as an argument

SuspendSuspend This function moves a process from either the This function moves a process from either the

“ready” or “blocked” queue to the associated “ready” or “blocked” queue to the associated “suspended” queue.“suspended” queue.

It receives the process name as an argumentIt receives the process name as an argument

ResumeResume This function moves a process from either of This function moves a process from either of

“suspended” queues back to either the “suspended” queues back to either the “ready” or the “blocked” queues“ready” or the “blocked” queues

It receives the process name as an argumentIt receives the process name as an argument

Please note: These operations require that Please note: These operations require that we “find” a PCB, “remove” it from one we “find” a PCB, “remove” it from one queue and “insert” it into another!queue and “insert” it into another!

Set PrioritySet Priority This function is used to change the priority of This function is used to change the priority of

a specific process.a specific process. It must “find” the processIt must “find” the process Change the value of it’s priorityChange the value of it’s priority

Show_PCBShow_PCB Displays the contents of a single PCBDisplays the contents of a single PCB Receives the process name as an argument.Receives the process name as an argument.

Show AllShow All This function prints information for ALL PCBs This function prints information for ALL PCBs

in the system!!!!! Displays the contents of in the system!!!!! Displays the contents of EVERY queue!!!EVERY queue!!!

Need to select an attractive display format!!!Need to select an attractive display format!!!

Show ReadyShow Ready This function displays information for all of the This function displays information for all of the

PCB in both the ready queue or the PCB in both the ready queue or the suspended ready queue.suspended ready queue.

Show blockedShow blocked Shows information for all PCBs in either the Shows information for all PCBs in either the

blocked or suspended blocked queues.blocked or suspended blocked queues.