chap03

19
Processes • Process – Set of instructions and data • Sample system (Fig 3.1, pg. 109): – Dispatcher – Starts at address 100 – Process A – Starts at address 5000 – Process B – Starts at address 8000 • Starts an I/O operation after 4 instructions – Process C – Starts at address 12000 • Assume we can do 6 instructions before a timer interrupt occurs • Assume all processes are completely in memory • Assume dispatcher requires 6 instructions

Upload: mbadubai

Post on 08-Nov-2015

213 views

Category:

Documents


0 download

DESCRIPTION

OS SLIDES

TRANSCRIPT

  • ProcessesProcess Set of instructions and dataSample system (Fig 3.1, pg. 109):Dispatcher Starts at address 100Process A Starts at address 5000Process B Starts at address 8000Starts an I/O operation after 4 instructionsProcess C Starts at address 12000Assume we can do 6 instructions before a timer interrupt occursAssume all processes are completely in memoryAssume dispatcher requires 6 instructions

  • Process ExampleFigure 3.3, page 11115000271200425001281200535002 Timeout 450032910055004301016500531102 Timeout 321037100331048101341059102355006101033650071110437500812105385009138000395010148001405011158002 Timeout 16800341100 Start I/O 42101171004310218101441031910245104201034610521104471200622105481200723120004912008241200150120092512002511201026120035212011 Timeout

  • Process StatesTwo-state model (Fig. 3.4, pg 112):RunningNot Running - may be held in a queue

    RequirementsSome way of representing a processMust include state and memory useMust keep track of processes that are not currently running (Fig. 3.4b)DispatcherMoves processes to the waiting queueRemove completed/aborted processesSelect the next process to run

  • Handling ProcessesCreating a processBuild kernel data structuresAllocate memoryReasons to create a processSubmit a new batch job/Start programUser logs on to the systemDevice driver or DaemonOS creates on behalf of a user (printing)Often transparent to the userTerminating a process: Need some way to indicate a process is finished (system call or instruction)Program may fail (Table 3.2, pg 114)Protection errorAddress ErrorInvalid data operationInvalid or privileged instructionI/O FailureParent process requestOperator intervention (control-C)

  • Five-state ModelProcesses may be waiting for I/OUse additional states:Running: currently being runReady: ready to runBlocked: waiting for an event (I/O)New: just created, not yet admitted to set of runnable processesExit: completed/error exitFig. 3.5, pg 115 & Fig. 3.7a, pg 119

  • Five-state ModelMay have separate waiting queues for each event (Figure 3.7b, page 119) Transitions:Null New Process is createdNew Ready O.S. is ready to handle another process (Memory, CPU)Ready Running Select another process to runRunning Exit Process has terminatedRunning Ready End of time slice or higher-priority process is readyRunning Blocked Process is waiting for an event (I/O, Synchronization)Blocked Ready The event a process is waiting for has occurred, can continueReady Exit Process terminated by O.S. or parentBlocked Exit Same reasons

  • Suspending ProcessesMay suspend a process by swapping part or all of it to diskNote: not assuming virtual memory yetMost useful if we are waiting for an event that will not arrive soon (printer, keyboard)If not done well, can slow system down by increasing disk I/O activityState Transition Diagram (Fig 3.8, pg 121)

    Key States:Ready In memory, ready to executeBlocked In memory, waiting for an eventBlocked Suspend On disk, waiting for an eventReady Suspend Oon disk, ready to execute

  • Suspending ProcessesNew Transitions:Blocked Blocked Suspend Swap out a blocked process to free memoryBlocked Suspend Ready Suspend Event occurs (O.S. must track expected event)Ready Suspend Ready Activate a process (higher priority, memory available)Ready Ready Suspend Need to free memory for higher-priority processesNew Ready or Ready Suspend Can choose where to put new processesBlocked Suspend Blocked Reload process expecting event soonRunning Ready Suspend Preempt for higher-priority processReasons to Suspend (Table 3.3, pg 123)Manage ResourcesTiming (backup at midnight process)User request (debugging, background)Parent process (synchronization, errors)

  • O.S. Control StructuresThere are many pieces of information the O.S. needs to keep aroundMemoryMain memory, disk, protection infoDepends on the memory/disk allocation method in useMay have to handle processes partially in main memoryEspecially true in virtual memory systemsMemory may be allocated in blocks, possibly of various sizesI/O devicesIncludes status of pending operationsFiles in useInclude status, location on disk

  • O.S. Control StructuresProcess tables One per processProcess IDMay be an index into a table, or other value that can be used to locate the process informationRegisters, PSW/Flags, StackControl InformationMay be cross-linked with Memory, I/O, and other tablesNeed to coordinate access to these tablesMany different parts of the O.S. will access these tablesNeed to protect these blocks from faulty O.S. routines Want to be able to modify these tables when the O.S. changes

  • Process Control BlockTable 3.5, page 129IdentifiersProcess, Parent, UserProcessor StateUser-Visible RegistersControl/Status RegistersStack Pointers (User, System)Process Control InformationScheduling/state, PriorityData structure info (links)Interprocess CommunicationPrivilege InformationMemory ManagementUser CodeUser DataSystem StackResource Ownership and Utilization

  • KernelDepends on two or more processor statesUser state for ordinary programsPrivileged state for kernelMay have additional states286+ has 4 statesCurrent state often held in the processor status word (PSW)State changes in response to interrupts, system calls (INT, call gates) or special instructions (IRET, Change mode)Only Privileged (system) state can access/modify key data structuresOften done by protecting segments of memory that hold those structures

  • Creating a processAssign a unique process IDAllocate memory for the processCan use defaults for amount of memoryMay get values from creating processLink to shared memory (clipboard)Initialize the process control blockProcess ID, parent IDMost of this information can be zeroedSet program counter and stack pointer to appropriate valuesMay set up argc/argv informationState usually set to ReadySet links so it is in the appropriate queueCreate other data structuresMemory, files, accounting

  • Switching ProcessesWe will often switch between the various processes in the systemExternal InterruptTimer (time slice expired)I/O interrupt (an event occurred?)Internal TrapPagefaultInvalid Operation, ...Supervisor CallFor each interrupt (and often traps or calls):Save the current processor state (registers, flags, etc.)Set the program counter to the address of the appropriate routineSwitch to system/kernel mode so privileged instructions can be used

  • Switching Processes 2Steps to take to switch processesSave context (registers, etc.)Update state of current processChange Running to Blocked/Ready/ExitMove the control block to the appropriate queueSelect another process to executeMove the control block from the appropriate queueUpdate the control block of the selected process to RunningUpdate necessary memory-management structuresRestore the context (registers, etc.) of the newly selected process

  • O.S. Kernel StructureNonprocess KernelKernel runs outside the context of any running processKernel has its own memory and stackExecution within User ProcessSystem calls occur in the context of the current user processCommon in minicomputers and microcomputersEach process image has both user and system data structuresProcess-based O.S.Implements the O.S. as a collection of system processesEncourages a clean designUseful in a multiprocessor system

  • Unix SVR4 ProcessesUses 9 processes states (table 3.9)Preempted and Ready to run, in memory are nearly identicalA process may be preempted for a higher-priority process at the end of a system callZombie Saves information to be passed to the parent of this processProcess 0 Swapper, created at bootProcess 1 Init, creates other processes

  • Unix Process ImageTable 3.10, pg 145User-level contextUser CodeUser DataUser StackShared MemoryRegister ContextProgram CounterMay be in either user or kernel spacePSWStack PointerPoints to top of user/kernel stackGeneral RegistersSystem-level contextProcess table entryU areaPer process region table Virtual memory infoKernel Stack

  • System-Level contextProcess Table Entry (Table 3.11, pg 146)Status Current state of a processPointers to U area and user code/dataProcess sizeIdentifiers (real/effective user/group id)Process/Parent IDEvent DescriptorSignal Signals sent but not handledPriorityTimers - process execution time, user-set alarmMemory status Is it swapped out?U Area (Table 3.12, pg 147)Identifiers (real/effective user/group id)Timers (time spent in user/kernel mode)Signal Handler arrayControl terminal (if it exists)System call return valueSystem call errorsI/O and File parametersFile Descriptor informationPermission Mode FieldLimit on process size