530 likes | 697 Vues
Introducing Processes: Description and Control. Fred Kuhns Washington University Computer Science Dept. Processes. A process is a program in execution; process execution must progress in sequential fashion. Can be characterized by its trace.
E N D
Introducing Processes:Description and Control Fred Kuhns Washington University Computer Science Dept.
Processes • A process is a program in execution; process execution must progress in sequential fashion. Can be characterized by its trace. • A process requires resources, which are managed by the operating system • The OS interleaves the execution of several processes to maximize processor utilization • OS supports InterProcess Communication (IPC) and user creation of processes
Dispatch Exit Running Not Running Enter Pause (a) State transition diagram Simple Two-State Model
Processor Queuing Diagram • Dispatcher • Program that assigns the processor to one process or another • Prevents a single process from monopolizing processor time Dispatch Queue Exit Enter Pause (a) Queuing diagram
5 Process States • As a process executes, it changes state • New: The process is being created. • Running: Instructions are being executed. • Waiting or blocked: The process is waiting for some event to occur. • Ready: The process is waiting to be assigned to a process. • Terminate or Exit: The process has finished execution.
OS Control Structures Memory Tables Process Image Memory I/O Tables User data User program System stack PCB I/O File File Tables Processes Primary Table Process 1 Process 2 … Process N
Process Control Block (PCB) Information associated with each process. • Process state • Program counter • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information
Process Scheduling Queues • Job queue – set of all processes in the system. • Ready queue – set of all processes residing in main memory, ready and waiting to execute. • Device queues – set of processes waiting for an I/O device. • Process migration between the various queues.
Schedulers • Long-term scheduler: job scheduler • selects which processes should be brought into the ready queue. • invoked infrequently (seconds, minutes) • controls the degree of multiprogramming • Medium-term scheduler • allocates memory for process. • invoked periodically or as needed. • Short-term scheduler: CPU scheduler • selects which process should be executed next and allocates CPU. • invoked frequently (ms)
Context Switch • When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process. • Context-switch time is overhead; the system does no useful work while switching. • Time dependent on hardware support.
Process Creation • Parents create children; results in a tree of processes. • Resource sharing • Parent and children share all resources. • Children share subset of parent’s resources. • Parent and child share no resources. • Execution • Parent and children execute concurrently. • Parent waits until children terminate.
Process Creation (Cont.) • Address space • Child duplicate of parent. • Child has a program loaded into it. • UNIX examples • fork system call creates new process • execve system call used after a fork to replace the process’ memory space with a new program.
Process Termination • Process executes last statement (exit). • Output data from child to parent (via wait). • resources deallocated by operating system. • Parent may terminate execution of children processes (abort). • Child has exceeded allocated resources. • Task assigned to child is no longer required. • Parent is exiting. • Operating system does not allow child to continue • Cascading termination.
Cooperating Processes • Independent process cannot affect or be affected by the execution of another process. • Cooperating process can affect or be affected by the execution of another process • Advantages of process cooperation • Information sharing • Computation speed-up • Modularity • Convenience
char block Device drivers A Traditional UNIX Kernel execution environment application trap libraries user System call interface kernel System Services File subsystem dispatcher IPC Process control subsystem Buffercache Scheduler Exceptions Interrupt Memory hardware
Basic Concepts and Terminology • Two privilege levels • user and system mode • kernel space protected: requires special instruction sequence to change from user to kernel mode • Process has "protected" address space • all process share same kernel space • per process state (u_area) in kernel • per process kernel stack • kernel is re-entrant • system versus process context
Mode, Space and Context Privileged user kernel mode context process Application (user code) System calls Exceptions kernel X not allowed Interrupts, System tasks system space
The UNIX Kernel • loaded at boot time and initializes system, • creates initial system processes. • remains in memory and manages the system • Resource manager/mediator - • process is key abstraction • Time share (time-slice) the CPU, • coordinate access to peripherals, • manage virtual memory. • Performsprivilegedoperations. • provides synchronization primitives. • Well defined entry points • syscall, exceptions or interrupts.
Entry into the Kernel • Synchronous - kernel performs work on behalf of the process: • System call interface (UNIX API): central component of the UNIX API • Hardware exceptions - unusual action of process • Asynchronous - kernel performs tasks that are possibly unrelated to current process. • Hardware interrupts - devices assert hardware interrupt mechanism to notify kernel of events which require attention (I/O completion, status change, real-time clock etc) • System processes - scheduled by OS • swapper and pagedaemon.
Trap or Interrupt Processing • Hardware switches to kernel mode, using the per/process kernel stack. • HW saves PC, Status word and possibly other state on kernel stack. • Assembly routine saves any other information necessary and dispatches event. • On completion a return from interrupt (RFI) instruction is performed.
Interrupt handling • Asynchronous event. • Name three? • Must be serviced in system context, • What does this mean? • Must not block, • Why? • What happens to the currently running process?
Interrupt handling - some details • Multiple interrupt priority levels (ipl), traditionally 0-7. • User processes and kernel operate at the lowest ipl level. • Higher level Interrupts can preempt lower priority ones. • The current ipl level may be set while executing critical code in order to block (or mask) higher priority interrupts.
Exception handling • Synchronous to the currently running process. • What is an example? • Must run in the current processes context. • Why? • An Interrupt may occur during the processing of an exception or trap. • Could this be a problem?
Software Interrupts • Interrupts typically have the highest priority in a system. • Software interrupts are assigned priorities above that of user processes but below that of interrupts. • Software interrupts are typically implemented in software. • Examples are callout queue processing and network packet processing.
System Call Interface • System call API • Why do we need one? • Implemented as a set of assembly language stubs • Trap into kernel dispatch routine which invokes a high-level system call. • User privileges are verified and any data is copied into kernel (copyin()). • On return, copy out any user data (copyout()), check for an Asynchronous System Trap (signal, preemption, etc).
Traditional UNIX Synchronization • Kernel is re-entrant. • only one thread/processes active at any given time (others are blocked). • Nonpreemptive. • Blocking operations • Masking interrupts
Blocking Operations • When resource is unavailable (possibly locked), process sets flag and calls sleep() • sleep places process on a blocked queue, sets state to asleep and calls swtch() • when resource released wakeup() is called • all sleeping processes are woken and state set to runnable (placed on the runnable queues). • When running, process must verify resource is available. Why?
Process Scheduling • Preemptive round-robin scheduling • fixed time quantum • priority is adjusted by nice value and usage factor. • Processes in the kernel are assigned a kernel priority (sleep priority) which is higher than user priorities. • Kernel 0-49, user 50-127.
Signals • Asynchronous events and exceptions • Signal generation using the kill() system call • Default operation or user specific handlers • sets a bit in the pending signals mask in the proc structure • All signals are handled before return to normal processing • System calls can be restarted
The Process • Fundamental abstraction • Executes a sequence of instructions • Competes for resources • Address space - memory locations accessible to process • virtual address space: memory, disk, swap or remote devices • System provides features of a virtual machine • shared resources (I/O, CPU etc) • dedicated registers and memory
The UNIX Process • Most created by fork or vfork • well defined hierarchy: one parent and zero or more child processes. The init process is at the root of this tree • different programs may be run during the life of a process by calling exec • processes typically terminate by calling exit
User Running Kernel Running Runnable Stopped Zombie Initial Idle Stopped Asleep Asleep UNIX Process States fork system call, interrupt return fork swtch() exit swtch() wait sleep() continue stop wakeup stop stop continue wakeup
dispatch admit New Ready Running Exit time-out release activate event wait Suspend Blocked suspend Simpler State Diagram
Process Context • Address Space • text, data, stack, shared memory ... • Control information (u area, proc) • u area, proc structure, maps • kernel stack • Address translation maps • Credentials • user and groupids • Environment variables • variable=value • typically stored at bottom of stack
Process Context (cont) • Hardware context • program counter • stack pointer • processor status word • memory management registers • FPU registers • Machine registers saved in u area's process control block (PCB) during context switch to another process
Kernel stack stack Process address space Data Text (shared) 0x00000000 Process Address Space 0xffffffff Kernel address space 0x7fffffff
proc struct User Stack User Stack User Stack Big Picture kernel memory Kernel stack/u area Kernel stack/u area Kernel stack/u area Data Data Data Text (shared) Text (shared) Text (shared)
Control Information • U area. • Part of user space (above stack). • typically mapped to a fixed address. • contains info needed with running. • Can be swapped • Proc • contains info needed when not running. • Not swapped out. • traditionally fixed size table
U Area PCB - HW context pointer to proc real/effective ids args, return values or errors to current syscall. Signal info file descriptor table controlling terminal vnode Proc structure process ID and group u area pointer process state queue pointers - scheduler, sleep, etc. Priority memory management info flags U area and Proc structures