operating systems project 5: processes & multiprogramming

23
Operating Systems Project 5: Processes & Multiprogramming CSCI330 - Project 5 1

Upload: others

Post on 10-Dec-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems Project 5: Processes & Multiprogramming

Operating SystemsProject 5:

Processes & Multiprogramming

CSCI330 - Project 5 1

Page 2: Operating Systems Project 5: Processes & Multiprogramming

Multiprogramming Requirements• Memory Management

ØAbility to load multiple programs into memory

• Time SharingØAbility to periodically stop the running process and

transfer control to an ISR in the OS

• Process ManagementØAbility to keep track of and change between

executing processes.• Context switching• Ready queue

CSCI330 - Project 5 2

Page 3: Operating Systems Project 5: Processes & Multiprogramming

New Files• New versions of:

Økernel.asmØlib.asmØbootload.asm

• New files:Øproc.hØproc.cØtestproc.h

CSCI330 - Project 5 3

Page 4: Operating Systems Project 5: Processes & Multiprogramming

Segment-Based Memory Management• Allow one process to be loaded into each

segmentØ Segments: 0x0000, 0x1000, 0x2000, … 0x9000

• 0x0000 reserved for interrupt vector• 0x1000 reserved for kernel➜8 segments available for user programs

Ø0x2000 – 0x9000

• Maximum program + data + stack?Ø0x1000 bytes = 65536 bytes = 64kB

CSCI330 - Project 5 4

Page 5: Operating Systems Project 5: Processes & Multiprogramming

Tracking Free Memory• Memory segment map:

Ø Each index correspondsto one memory segment.• segment = (index+2)*0x1000• index = (segment/0x1000)-2

ØMarked as:•FREE or USED

0

1

23

4

5

6

7

Segment0x2000

Segment0x6000

CSCI330 - Project 5 5

Page 6: Operating Systems Project 5: Processes & Multiprogramming

Time Sharing:

Programmable Interrupt Timer

• Generates interrupt 0x08

• Will generate approximately 12 interrupts /

second

• ISR for interrupt 0x08 will do context switching

and scheduling

ØAssembly language code is given

ØWrite a C function that gets called on each interrupt

• Similar to handleInterrupt21 for interrupt 0x21.

CSCI330 - Project 5 6

Page 7: Operating Systems Project 5: Processes & Multiprogramming

Interrupt 0x08 ISR Details• makeTimerInterrupt()

Ø Sets entry 0x08 in interrupt vector to point to timer_ISRassembly routine in kernel.asm.

• timer_ISR()Ø Pushes context (GP registers + PC) onto stack of interrupted

program.Ø Invokes handleTimerInterrupt with segment & stack pointer

of interrupted program

CSCI330 - Project 5 7

Page 8: Operating Systems Project 5: Processes & Multiprogramming

Interrupt 0x08 ISR Details• handleTimerInterrupt(int segment, int

stackPointer)Ø C function that you add to your kernelØ Does process management and short term scheduling

• returnFromTimer(int segment, int stackpointer)Ø Assembly routine in kernel.asmØ Called at end of handleTimerInterrupt to return to program in

segment.• Pops context of program from its stack• Transfers control to program in segment

Ø Call does not return

CSCI330 - Project 5 8

Page 9: Operating Systems Project 5: Processes & Multiprogramming

Process Management Responsibilities• Starting a new process (executeProgram)

Ø Obtain process control block (PCB) for the processØ Load program into free segmentØ Add PCB onto ready queue

• Short-term scheduling (handleTimerInterrupt)Ø Save stack pointer of interrupted process in PCBØ Pick new process from ready queueØ Start new process by calling returnFromTimer

• Terminating a processes (terminate)Ø Release memory segmentØ Release PCB

CSCI330 - Project 5 9

Page 10: Operating Systems Project 5: Processes & Multiprogramming

Process Management: proc.*• proc.h defines constants, data structures,

global variables, and functions that you will use for memory and process managementØProvided for you

• You need to write (most of) proc.c to implement the defined functions

• testproc.cØProvided to help you test your implementation

CSCI330 - Project 5 10

Page 11: Operating Systems Project 5: Processes & Multiprogramming

proc.h Data Structures: memoryMap

0

1

23

4

5

6

7

memoryMap

FREEUSED

Constants:

CSCI330 - Project 5 11

Page 12: Operating Systems Project 5: Processes & Multiprogramming

proc.h Data Structures: PCB• Process Control Block:

struct PCBchar name[7]int stateint segmentint stackPointerstruct PCB *nextstruct PCB *prev

DEFUNCTSTARTINGRUNNINGREADYBLOCKED

Constants:

CSCI330 - Project 5 12

Page 13: Operating Systems Project 5: Processes & Multiprogramming

proc.h Data Structures: pcbPool• PCB Pool

0

1

23

4

5

6

7

pcbPool

struct PCB

struct PCB

.

.

.

struct PCBchar name[7]: "\0"int state: DEFUNCTint segment: 0x0000int stackPointer 0x0000struct PCB *next NULLstruct PCB *prev NULL

CSCI330 - Project 5 13

Page 14: Operating Systems Project 5: Processes & Multiprogramming

proc.h Data Structures•struct PCB *running

0

1

23

4

5

6

7

pcbPool

struct PCB

struct PCB

.

.

.

struct PCBchar name[7]: "uprog2\0"int state: RUNNINGint segment: 0x3000int stackPointer 0xFF00struct PCB *next NULLstruct PCB *prev NULL

CSCI330 - Project 5 14

The currently running process

Page 15: Operating Systems Project 5: Processes & Multiprogramming

proc.h Data Structures• Ready Queue

ØDoubly-linked list

0

1

23

4

5

6

7

pcbPool

struct PCBstate: READYnext: NULLprev:

struct PCB

.

.

.

struct PCBstate: READYnext: prev: NULL

.

.

.

readyHead

readyTail

struct PCBstate: READYnext:prev:

CSCI330 - Project 5 15

Page 16: Operating Systems Project 5: Processes & Multiprogramming

proc.h Data Structures• Initially the running process will be the Idle

Processstruct PCB *running

struct PCBchar name[7]: "IDLE\0"int state: READYint segment: 0x1000int stackPointer 0x????struct PCB *next NULLstruct PCB *prev NULL

idleProc

CSCI330 - Project 5 16

If no processes in the ready queue to be run, run idle process

Page 17: Operating Systems Project 5: Processes & Multiprogramming

proc.h Functions•proc.h defines functions for manipulating

these data structures:Ø void initializeProcStructures();Ø int getFreeMemorySegment();Ø void releaseMemorySegment(int seg);Ø struct PCB *getFreePCB();Ø void releasePCB(struct PCB *pcb);Ø void addToReady(struct PCB *pcb);Ø struct PCB *removeFromReady();

CSCI330 - Project 5 17

Page 18: Operating Systems Project 5: Processes & Multiprogramming

proc.c && testproc.c• Implement those functions in proc.c• Use and extend testproc.c to test your

implementations before trying to use them in the kernel.ØCompile with gcc: •gcc –o testproc testproc.c proc.c

ØRun on local machine: •./testproc

CSCI330 - Project 5 18

Page 19: Operating Systems Project 5: Processes & Multiprogramming

Using proc.h and proc.c• To use the variables in proc.h and the functions

in proc.c:Ø In kernel.c:

• #define MAIN• #include "proc.h"• Need to link proc.o when creating kernel

Ø In any other files that use proc.h (e.g., proc.c)• #include "proc.h"

CSCI330 - Project 5 19

Page 20: Operating Systems Project 5: Processes & Multiprogramming

Accessing the Kernel’s Data Segment• The global variables defined in proc.h are put

into the kernel’s data segment by the compiler• Variables in the data segment are addressed by

offset into the data segment.Ø If readyHead = 0x0450,Ø then the PCB pointed to by readyHead is stored at

memory address:ds*0x10 + 0x0450

CSCI330 - Project 5 20

Page 21: Operating Systems Project 5: Processes & Multiprogramming

Accessing the Kernel’s Data Segment• When handleTimerInterrupt is called,ds register will contain address of the interrupted process’ data segment.Ø If readyHead = 0x0450,

when the kernel attempts to access the PCB pointed to by readyHead, it looks at memory address:ds*0x10 + 0x0450

which is now in the interrupted process’ data segment not the kernel’s data segment!

CSCI330 - Project 5 21

Page 22: Operating Systems Project 5: Processes & Multiprogramming

Accessing the Kernel’sData Segment• kernel.asm provides functions to deal with this

situation:ØsetKernelDataSegment()

• Invoke this in your kernel before accessing any global variables defined in proc.h and before calling any functions from proc.h that access those variables!

ØrestoreDataSegment()• Invoke this in your kernel after you are finished

accessing the global variables

CSCI330 - Project 5 22

Page 23: Operating Systems Project 5: Processes & Multiprogramming

Copying Data to the

Kernel’s Data Segment

• In executeProgram(char *fname) you need

to copy the name from fname into the PCB

• But…

Ø fname is addressed relative to shell’s stack segment

Ø The PCB is addressed relative to kernel’s data

segment

ØUse the kStrCopy function given in project

description when running in shell’s data segment

• Not between setKernelDataSegment and restoreDataSegment

CSCI330 - Project 5 23