module r3

22
Module R3 Module R3 Process Scheduling Process Scheduling

Upload: kieran-west

Post on 01-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Module R3. Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require that you read and understand chapters I2, I3, and R3 from your project manual. - PowerPoint PPT Presentation

TRANSCRIPT

Module R3Module R3Process SchedulingProcess Scheduling

Module R3 involves the creation of a simple Module R3 involves the creation of a simple “Round Robin” dispatcher.“Round Robin” dispatcher.

The successful completion of this module will The successful completion of this module will require that you read and understand chapters require that you read and understand chapters I2, I3, and R3 from your project manual.I2, I3, and R3 from your project manual.

Successful completion of this module requires that Successful completion of this module requires that you understand: Interrupt handlers, context you understand: Interrupt handlers, context switching, and the role of the system stack and switching, and the role of the system stack and registers. (I2)registers. (I2)

Components of R3Components of R3

Add 1 temporary command to your Add 1 temporary command to your command handler:command handler: This command exists to allow the testing of This command exists to allow the testing of

your “dispatcher” and “system call handler”.your “dispatcher” and “system call handler”. This operation will be responsible for creating This operation will be responsible for creating

and initializing 5 PCB’s to represent 5 and initializing 5 PCB’s to represent 5 processes which will be dispatched.processes which will be dispatched.

You will implement 2 internal functions the You will implement 2 internal functions the ““dispatcherdispatcher” and the “” and the “systemsystem callcall handlerhandler”” The “dispatcher” is responsible for locating the next The “dispatcher” is responsible for locating the next

ready process and performing a context switch to ready process and performing a context switch to begin executing that processbegin executing that process

The “system call handler” will be responsible for The “system call handler” will be responsible for handling “system” calls issued by the running handling “system” calls issued by the running processes. These system calls will trigger an processes. These system calls will trigger an interrupt that will result in either the current process interrupt that will result in either the current process leaving the running state, and another being leaving the running state, and another being dispatched, or the current process being terminateddispatched, or the current process being terminated

Both of these functions will be structured as interrupt Both of these functions will be structured as interrupt handlers. handlers.

Addresses, the stack, and registersAddresses, the stack, and registers The 8086 processor uses an addressing The 8086 processor uses an addressing

scheme called “scheme called “segmentedsegmented addressing”addressing” A pair of 16 bit values is needed to create an A pair of 16 bit values is needed to create an

address: A address: A segment numbersegment number and an and an offset.offset. A segment # represents the startling location of A segment # represents the startling location of

a 64k byte region called a segmenta 64k byte region called a segment The offset represents a relative address within The offset represents a relative address within

that segmentthat segment Addresses are typically represented as Addresses are typically represented as

XXXXXXXX::YYYYYYYY where where XXXXXXXX is the segment #, and is the segment #, and YYYYYYYY the offset, both values are assumed to be the offset, both values are assumed to be in hexadecimalin hexadecimal

To facilitate addressing (so every instruction To facilitate addressing (so every instruction needn’t supply 2 separate 16-bit address needn’t supply 2 separate 16-bit address components) 4 - 16-bit segment registers components) 4 - 16-bit segment registers (containing segment #’s) are maintained:(containing segment #’s) are maintained:

Data Segment: Data Segment: contains the contains the staticstatic (persistent) data (persistent) data variables used by a program. This is stored in the DS variables used by a program. This is stored in the DS register. This value is automatically used for memory register. This value is automatically used for memory references that fetch and store data.references that fetch and store data.

Code Segment: Code Segment: Contains the instructions of the Contains the instructions of the program being executed. This is stored in the CS program being executed. This is stored in the CS register. This value is automatically used when register. This value is automatically used when instructions are being fetched. A 16-bit register called instructions are being fetched. A 16-bit register called the IP (instruction pointer) is used for determining the the IP (instruction pointer) is used for determining the address of the next instruction to be executed.address of the next instruction to be executed.

Stack Segment:Stack Segment: the storage area for the the storage area for the ““current stackcurrent stack”. This is stored in the SS ”. This is stored in the SS register. The stack is used for temporarily register. The stack is used for temporarily storing addresses and data, both explicitly via storing addresses and data, both explicitly via push and pop instructions, and implicitly push and pop instructions, and implicitly during subroutine calls and interrupts. (like during subroutine calls and interrupts. (like local variables.. Etc)local variables.. Etc)

Extra Segment: Extra Segment: this is a spare segment, this is a spare segment, whose location is stored in the ES register. whose location is stored in the ES register.

The StackThe Stack The stack is a data buffer. At any time a set of The stack is a data buffer. At any time a set of

data items occupies a contiguous region with in data items occupies a contiguous region with in the buffer.the buffer.

One end called the bottom is fixed, the other end One end called the bottom is fixed, the other end called the top is movable.called the top is movable.

Generally data is added and removed only at the Generally data is added and removed only at the top of the stack. A push adds a data item, a pop top of the stack. A push adds a data item, a pop removes a data item.removes a data item.

A pointer (called the stack pointer) must be A pointer (called the stack pointer) must be maintained maintained POINTINGPOINTING to the top of the stack. to the top of the stack.

In the 8086, stacks are In the 8086, stacks are designed to grow from designed to grow from higher addresses to lower higher addresses to lower addressesaddresses

Each push operation will:Each push operation will: Decrement the stack pointer Decrement the stack pointer

by 2by 2 Store a data word at the new Store a data word at the new

address given by the stack address given by the stack pointerpointer

Each Pop operation will:Each Pop operation will: Fetch a data word from the Fetch a data word from the

address given by the stack address given by the stack pointerpointer

Increment the stack pointer Increment the stack pointer by 2by 2

One important usage of the One important usage of the stack is that in C, function stack is that in C, function parameters are frequently parameters are frequently pushed onto the stack pushed onto the stack before calling a function.before calling a function.

Data10A

108

106

..

bottom

top

Addressing registers:Addressing registers:

Additionally, there are 4 registers that can be Additionally, there are 4 registers that can be used for offset calculation:used for offset calculation: SPSP: The stack pointer is used in pushing values and : The stack pointer is used in pushing values and

addresses on the stack, and for popping these from addresses on the stack, and for popping these from the stack.the stack.

BPBP: the “base pointer” can be used to compute : the “base pointer” can be used to compute offsets for both the stack segment and the data offsets for both the stack segment and the data segment. In turbo C it is often used as a temporary segment. In turbo C it is often used as a temporary register for saving the value of the stack pointer.register for saving the value of the stack pointer.

SI and DI: Source and destination indexes. These SI and DI: Source and destination indexes. These are used to compute the offset of dataare used to compute the offset of data

Data RegistersData Registers The 8086 also has 4 16-bit data registers (aka. General The 8086 also has 4 16-bit data registers (aka. General

registers or arithmetic registers) registers or arithmetic registers) AX, BX, CX, & DXAX, BX, CX, & DX

These registers can also be treated as 8 8bit registers:These registers can also be treated as 8 8bit registers:AH, AL, BH, BL, CH, CL, DH, DLAH, AL, BH, BL, CH, CL, DH, DL

(H refers to the high order byte, and L the low order)(H refers to the high order byte, and L the low order)

A,B,C & D stands for accumulator, base, count, and A,B,C & D stands for accumulator, base, count, and data.data.

When saving and restoring the context of processes When saving and restoring the context of processes we must ensure that values for these registers are we must ensure that values for these registers are not corrupted.not corrupted.

Program status wordProgram status word One last register to concern ourselves with: is the One last register to concern ourselves with: is the

program status wordprogram status word

This register contains condition codes, and bits that This register contains condition codes, and bits that determine whether or not certain interrupts can be determine whether or not certain interrupts can be generated.generated.

For R3, we will be interested in the 9For R3, we will be interested in the 9thth bit or the IF bit or the IF (interrupt enable flag). (interrupt enable flag).

During interrupt processing we will disable interrupts by During interrupt processing we will disable interrupts by setting this bit to 0. We must later restore it.setting this bit to 0. We must later restore it.

Dispatcher InitializationDispatcher Initialization This involves adding a new command to your This involves adding a new command to your

COMHAN called “dispatch”. COMHAN called “dispatch”. This will invoke a function which will perform the This will invoke a function which will perform the

following:following: Allocate and initialize 5 PCB:Allocate and initialize 5 PCB:

These will be linked to 5 test procedures provided in These will be linked to 5 test procedures provided in “PROCS_R3.C” on the MPX support web pages.“PROCS_R3.C” on the MPX support web pages.

These procedures will be statically compiled and linked to These procedures will be statically compiled and linked to your MPX executable, by adding them to your project.your MPX executable, by adding them to your project.

We need to store the starting address of each function in its We need to store the starting address of each function in its associated PCB.associated PCB.

Values for initialization are described in chapter R3 of your Values for initialization are described in chapter R3 of your project manualproject manual

Insert all 5 PCB’s on the ready queue. We Insert all 5 PCB’s on the ready queue. We will only need to use the FIFO insertion will only need to use the FIFO insertion method.method.

Link the Link the int 60hint 60h interrupt to your system interrupt to your system call handler. (accomplished via a call to a call handler. (accomplished via a call to a new support routine sys_set_vec )new support routine sys_set_vec )

Call your dispatcher to begin execution:Call your dispatcher to begin execution:

How the processes executeHow the processes execute The names of the test procedures are: The names of the test procedures are: test1-R3test1-R3

through through test5-R3test5-R3.. Each process is designed as a simple loop that prints out Each process is designed as a simple loop that prints out

a message a set number of times. a message a set number of times. Test1-R3Test1-R3 prints out prints out the message once… and so forththe message once… and so forth

Processes once dispatched are not preempted, nor are Processes once dispatched are not preempted, nor are they subject to timeout.they subject to timeout.

Each process will voluntarily give up control of the CPU Each process will voluntarily give up control of the CPU using a call to “sys_req” which will generate a system using a call to “sys_req” which will generate a system call interrupt. Processes call “sys_req” with two possible call interrupt. Processes call “sys_req” with two possible op_code values:op_code values: IDLE: which will return the process to the ready queueIDLE: which will return the process to the ready queue EXIT: which is a request to terminate the process, and free the EXIT: which is a request to terminate the process, and free the

PCB.PCB.

Processing system call interruptsProcessing system call interrupts You will need to write an interrupt handler called You will need to write an interrupt handler called

“sys_call” whose purpose is to “sys_call” whose purpose is to catch andcatch and processprocess system call interrupts generated by executing processes.system call interrupts generated by executing processes.

In the declaration of the prototype of this function we will In the declaration of the prototype of this function we will use the TURBO C keyword “interrupt”use the TURBO C keyword “interrupt”void interrupt sys_call(void);void interrupt sys_call(void);

The interrupt keyword generates executable assembler The interrupt keyword generates executable assembler language statements which automatically save the language statements which automatically save the registers:registers:AX, BX, CX, DX, ES, DS, SI, DI, & BP AX, BX, CX, DX, ES, DS, SI, DI, & BP

On the stack automatically. (in this order). Similarly code is added On the stack automatically. (in this order). Similarly code is added at the end of the procedure to automatically restore these at the end of the procedure to automatically restore these registers. registers.

““sys_call” Notes:sys_call” Notes: Sys_reqSys_req prepares for the system call interrupt prepares for the system call interrupt

by placing the parameters it was called with onto by placing the parameters it was called with onto the “current” stack. It then generates interrupt the “current” stack. It then generates interrupt 60, using the 60, using the int 60hint 60h machine instruction. machine instruction.

When When “sys_call”“sys_call” is invoked it is responsible for is invoked it is responsible for retrieving these values and interpreting what retrieving these values and interpreting what type of action has been requested.type of action has been requested.

Additionally, the interrupt hardware pushed the Additionally, the interrupt hardware pushed the flag register, CS and IP onto the stack.flag register, CS and IP onto the stack.

So when “sys_call” is invoked the status of the So when “sys_call” is invoked the status of the stack is:stack is:

(previous stack contents)(previous stack contents)Buffer length pointer parameter (*count)Buffer length pointer parameter (*count)Buffer address parameter (*buffer)Buffer address parameter (*buffer)Device_id parameterDevice_id parameterOp_code parameterOp_code parameterFlagFlagCSCSIPIPAXAXBXBXCXCXDXDXESESDSDSSISIDIDIBPBP

We will need to retrieve the contents of the 1We will need to retrieve the contents of the 1stst four parameters to identify the type four parameters to identify the type of request made by the calling process. (pseudo code can be found on page 11)of request made by the calling process. (pseudo code can be found on page 11)

Depending on the type of request made:Depending on the type of request made:

Switch to a temporary stack to avoid stack overflow.Switch to a temporary stack to avoid stack overflow. If a request to IDLE has been issued: The current If a request to IDLE has been issued: The current

processes status is switched to “ready” and the processes status is switched to “ready” and the process is placed back on the ready queue.process is placed back on the ready queue.

If a request to EXIT has been issued: The process is If a request to EXIT has been issued: The process is deleted using your function from R2.deleted using your function from R2.

A call to your “dispatcher” is issued to begin execution A call to your “dispatcher” is issued to begin execution of the next process.of the next process.

(Note) the dispatcher will not return to this function!(Note) the dispatcher will not return to this function!

The dispatcherThe dispatcher The dispatcher “dispatch” will be structured as The dispatcher “dispatch” will be structured as

an interrupt handler, but will be invoked via a an interrupt handler, but will be invoked via a regular function call.regular function call.

It is responsible for:It is responsible for: Remove the PCB at the front of the ready queueRemove the PCB at the front of the ready queue Change the process status to running, and set a Change the process status to running, and set a

pointer to this PCB identifying it as the running pointer to this PCB identifying it as the running process.process.

COPY the stack pointer (_SS and _SP) from the COPY the stack pointer (_SS and _SP) from the processes PCB to the actual stack pointer, to prepare processes PCB to the actual stack pointer, to prepare for context restoration.for context restoration.

When this function “ends” execution will resume in the When this function “ends” execution will resume in the newly restored process!!!! It will never return to the newly restored process!!!! It will never return to the interrupt handler!interrupt handler!

Notes of concernNotes of concern We do need to save the initial state of the MPX We do need to save the initial state of the MPX

the first time that the dispatcher is called.the first time that the dispatcher is called.

There fore we need to declare global variables There fore we need to declare global variables to store the SS and SP of the MPX. These will to store the SS and SP of the MPX. These will be restored when there are no more processes be restored when there are no more processes in the ready queue. Restoration of these in the ready queue. Restoration of these variables will return control to the COMHAN and variables will return control to the COMHAN and we will be able to terminate execution of MPX.we will be able to terminate execution of MPX.

Complete outline for the dispatcher:Complete outline for the dispatcher:if sp_save is null, if sp_save is null,

ss_save = _SS ss_save = _SS sp_save = _SP sp_save = _SP

remove the PCB at the head of the ready queue remove the PCB at the head of the ready queue

if a PCB was found set cop to this PCB if a PCB was found set cop to this PCB set _SS and _SP to the PCB's stack pointer set _SS and _SP to the PCB's stack pointer

else else set cop to NULL set cop to NULL set _SS to ss_save set _SS to ss_save set _SP to sp_save set _SP to sp_save

end if end if

"return from interrupt" "return from interrupt"