18
© 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 1 Implementing Processes and Threads

Embed Size (px)

Citation preview

Page 1: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 1

Implementing Processes and Threads

Page 2: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 2

Required Software for ThreadsUNIX (Linux, OpenBSD, FreeBSD,

Solaris)■ Exported POSIX API or use "Pthreads" API■ gcc or g++ with -lpthread -lposix4 -

lthreadWindows (98/ME/NT/XP)

■ WIN32 API – not POSIX compliant■ Pthreads.DLL – freeware

• sources.redhat.com/pthreads-win32

• Copy pthread.dll to C:\windows

• Keep .h files wherever you want them

Page 3: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 3

Exploring the Abstraction

User i

Processes & RAMUser j

Processes & RAMUser k

Processes & RAM

CPU i CPU j CPU k

User i

ProcessesUser j

ProcessesUser k

Processes

CPU

Page space

ActualRAM

Abstract

Loc 0Loc n

Loc 0

Loc n

Loc 0

Loc n

Loc 0

Loc n

Page 4: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 4

Process Manager Responsibilities Define & implement the essential characteristics

of a process and thread■ Algorithms for behavior■ Process state data

Define the address space (and thus available content)

Manage the resources (I/O, RAM, CPU) Tools to manipulate processes & threads Tools for scheduling the CPU (Ch 7) Tools for inter-thread synchronization (Ch 8,9) Handling deadlock (Ch 10) Handling protection (Chapter 14) (if time

permits)

Page 5: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 5

ResourcesWhat is a "resource"

■ Requestable blocking object or service■ Reusable – CPU, RAM, disk space, etc■ Non-reusable (consumable)

• Data within a reusable resource

R={Rj|0<=j<=m}■ Rj is one type of resource, such as RAM

C={cj>=0| Rj R(0<=j<m)}■ cj is the # of available units of Rj

Page 6: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 6

Resource Mgmt Model {Mgr} : Rj (Mgr(Rj) gives ki<=ci units of

Rj to Pn)

Pn may only request i units of Rr

Pn may only request unlimited units of Rn

Why do we need the set notation?■ Formalized descriptions can lead to deadlock

detection and prevention algorithms

Page 7: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 7

Windows NT/2K/XP Process MgmtSplit into 2 facilities:

■ NT Kernel• Object mgmt

• Interrupt handling

• Thread scheduling

■ NT Executive• All other Process aspects

■ See "Inside Windows 2000", 3e, Solomon & Russinovich, Ch. 6, MS Press, 2000

Page 8: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 8

The Address SpaceBoundaries of memory accessH/W can help (DAT) (more later)Multiprogramming possible without H/W!!!!

■ Self-relocation■ Pre-load relocation■ Both use true addresses FIXED at load time■ NO paging, but MAY have swapping

• Windows 3.1

• IBM OS/VS1

Page 9: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 9

Address BindingGiven:

int function X(y,z){Int q; return ff(y,z)}

Void function M {X(3,4);}

Where are X, y, z and q??How does X get control from M?What happens if there is an interrupt

BETWEEN M's call to X and X starting?

Page 10: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 10

Address Binding-2-fixed

1. Gather all files of the program

2. Arrange them in RAM in linear fashion

3. Determine runloc for the executable

4. Find all address constants (functions and External data)

5. Find all references to those constants

6. Modify the references in RAM

7. Store as an executable file

8. Run at the pre-determined location in RAM

Page 11: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 11

Address Binding-3-dynamicPerform "fixed binding", but in step 3, use

a value of "zero"In step 6, mark as "relocatable"For step 8, before actually transferring

control, REPEAT 4-6 using the actual runloc determined by the loader

Same for DLL members

Page 12: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 12

Address Binding-4 DLL'sHow does a program find a DLL it didn't

create?

Each DLL member has a specific nameSystem has list of DLL member namesWhen DLL is requested, system fetches

module and dynamically binds it to memory, but NOT to the caller!

System transfers control to DLL member

Page 13: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 13

Address Binding - 5How does the system make it look as if

each abstract machine starts at 0?How does the system keep user spaces

apartHow does the system protect address

spaces

Page 14: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 14

Context SwitchingPower on, ROM reads bootstrap program

from head 0 of deviceROM transfers control to the programBootstrap program reads the loaderLoader reads the kernelKernel gets control and initializes itselfKernel loads User Interface Kernel waits for an interruptKernel starts a process, then waits again

Page 15: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 15

Context Switching - 2Device requests interruptROM inspects system for ability to acceptIf interrupts are masked off, exit

■ Future interrupts may be queued by hardware■ Or devices may be informed to re-try

If interrupts are allowed: ■ set status (in RAM, control store, etc)■ Atomically: load new IC, privileged mode,

interrupts masked offKernel processes the interrupt

Page 16: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 16

Context Switching - 3The actual Context Switch:

■ Save all user state info:• Registers, IC, stack pointer, security codes, etc

■ Load kernel registers• Access to control data structures

Locate the interrupt handler for this deviceTransfer control to handler, then:

■ Restore user state values■ Atomically: set IC to user location in user

mode, interrupts allowed again

Page 17: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 17

Questions to ponderWhy must certain operations be done atomically?

What restrictions are there during context switching?

What happens if the interrupt handler runs too long?

Why must interrupts be masked off during interrupt handling?

Page 18: © 2004, D. J. Foreman 1 Implementing Processes and Threads

© 2004, D. J. Foreman 18

What is a "Handle"?Application requests an object

■ A window, a chunk of RAM, a file, etc.Must give application a way to access itDone via a "handle"

■ A counter (file handles)■ An address in user RAM (structures)■ Always a "typed" variable

• Helps insure correct usage (except "C" doesn't enforce typed usage)