Operating Systems Part of E1.9 - Principles of Computers and Software Engineering Lecture 3: Introduction to process management
Objectives To introduce the concepts of: Process state and process control block Process scheduling Threads To describe the steps involved in: Process creation Process termination Process switching
Process control block (1) Previous lecture defined process as: Program in execution Consisting of Instructions Data Stack and context Process attributes The state of a process is defined by its current activity (e.g. running, waiting) Process is represented using a process control block.
Process Control Block (2) Typical Process Control Block (PCB) includes: Process state e.g. running, waiting Program counter CPU registers CPU-scheduling information e.g process priority Memory management information e.g values of base and limit registers Accounting information e.g process number I/O status information e.g list of I/O devices allocated and list of open files
Process States (1) Two-state model (1) dispatch Process Creation Not-running running Process Termination pause Processes either running or not-running Processes created as not-running After completion, processes are terminated
Process States (1) Two-state model (2) Process management in the two-state model: Selecting a process for running Dispatching of a process Move process from not-running to running state Restore its context Pausing of a process Move process from running to not-running state Save its context
Process States (1) Two-state model (3) Creation Not-running-queue PN PN-1 P2 P1 dispatch Termination Processor pause Process scheduling decisions: Which process to pick next from the not-running queue? When is a process paused?
Process States (2) Five-state model (1) Two state model: some processes in the not-running queue might not actually be ready to execute e.g waiting for I/O Dispatching of such process is a waste of processor time Need to separate not running processes in more categories: New: process was just created but has not yet entered the system Waiting: process is waiting for I/O Ready: process is ready to execute Result: Five state model
Process States (2) Five-state model (2) Five-state model: Process Creation New admitted ready dispatch running exit Process Termination Terminated I/O or event completion pause Waiting I/O or event wait Three queues: ready, new, waiting
Process States (3) Seven-state model (1) Five-state model: All processes (including those waiting for I/O) are loaded in memory Problem if memory is limited We can store (suspend) some of processes in secondary storage Need to add new two categories of process states: Ready-suspend: Process is ready to execute (not waiting for I/O) but is suspended to secondary storage Waiting-suspend: Process is waiting for I/O and has been suspended to secondary storage. Seven-state model takes into account these new states
Process States (3) Seven-state model (2) Process Creation New admitted ready dispatch running exit Process Termination Terminated Load from disk ready-suspend I/O or event completion Load from disk pause Waiting Suspend to disk I/O or event wait Waiting-suspend
Process States (3) Seven-state model (3) Seven state model illustrates the need for three different types of scheduling Long term scheduling: Determine whether process is admitted to system Determine whether process is placed to ready or ready-suspend queue Medium-term scheduling Determine when to move a process to/from the suspended state Short-term or CPU scheduling Determine when to interrupt a process currently running (note: this is different to processes interrupted by other events (e.g. hardware interrupt)). Determine which process from the ready queue to run next.
Process States (4) Scheduling queues and queuing diagrams (1) Scheduling queues Need even more queues apart from new, ready and waiting, now need new, ready, waiting, waiting-suspend, ready-suspend. Also useful to have various I/O device queues to place processes waiting for a specific device (one per device). Process scheduling moves processes between queues Queuing diagrams
Process States (4) Scheduling queues and queuing diagrams (2) Creation I/O1 Ready-queue PN PN-1 P2 P1 Blocked-waiting-for-I/O1 queue B1 B2 Bm-1 Bm dispatch pause Request I/O1 Termination Processor I/O2 Blocked-waiting-for-I/O2 queue C1 C2 Cm-1 Cm Request I/O2 (Example of a (partial) queuing diagram)
Process States (5) Process Tables OS maintains process tables that hold the PCBs. One entry per PCB Pointer entry in PCB points to the next PCB in the queue. Example: The ready queue and various I/O device queues [Silberschatz et al 2001]
Operations on Processes(1) Overview Major process operations used in the diagrams: Process creation Process termination Process switch
Operations on Processes(2) Process creation (1) Process creation Create new PCB Allocate memory for instructions, data, stack Load instructions and data into allocated memory Initialise stack and context Initialise PCB When? System initialisation Execution of a process-creation system call by a running process (e.g. POSIX fork, Win32 CreateProcess) User creates a new process (e.g. through the shell) Initiation of a batch job
Operations on Processes(2) Process creation (2) The creating process is called parent process; new processes are called children of this process Possibilities after creation: In terms of execution Parent continues execution concurrently with the children Parent waits for some or all of children to terminate in order to continue. In terms of address space of the new process: Child process is a duplicate of the parent process Child process has a new program loaded into it
Operations on Processes(3) Process termination Process termination Release resources allocated to process Delete PCB entries When? Completion of task (normal, voluntary exit) Exit due to error, e.g division by zero, I/O error, protection error. Exit due to termination of parent process (cascading termination) Termination by the OS, e.g if a resource is unavailable or its use has exceeded accounting limits.
Operations on Processes(4) Process switch Process or context switch Kernel saves context (e.g registers, pointers, memory management information etc) into the process PCB Loads context of the new process scheduled to run Context switch time is pure overhead can become substantial depending on whether parts of memory must be saved too. Hardware support can reduce this overhead (e.g by providing multiple sets of registers).
Threads(1) Need for Processes encompass two concepts: Code execution Resource grouping Threads: Can be thought at lightweight processes Share code section, data section and OS resources (e.g open files) with other threads within the process Have own program counter, register set and stack. Allow several threads of execution within the same process Avoid context switching overheads But no protection (e.g. memory) between threads
Threads(2) How? (1) User-level threads: Threads library (e.g POSIX pthreads) Each process starts with a single thread present. A thread can create new threads by calling library procedures, e.g thread_create. Parent thread typically specifies the name of a procedure for the new thread to run Parent thread gets identifier for the new thread. Advantages Fast to create and manage Disadvantages Have to write own scheduling algorithm Difficult to handle with blocking system calls
Threads(2) How? (2) Kernel-level threads Kernel performs creation, scheduling and management in kernel space. Disadvantages: Slower to create and manage than user-level threads Advantages Can easily handle threads with blocking system calls In multiprocessor environments, kernel can schedule threads on different processors Recycling threads
Threads(4) Benefits Responsiveness Interactive processes can perform a lengthy operation in a separate thread, while they handle user s input in another thread Example: Multithreaded Web browser Economy Allocating memory and resources for processes is costly Utilization of multiprocessor architectures
Process management What next? Next three lectures will examine issues introduced today First: Process scheduling Second: Process synchronization Third: Deadlocks
Summary Process management Process state models include two-, five- and seven-state models The steps involved in process creation, termination, and switch were presented. Threads are used to allow for multiple executions to take place within the same process. Next lecture: Process scheduling
Recommended Reading Read chapter 4 and section 5.1 of Silberschatz s book Read chapter 2, sections 2.1-2.2.5 of Tanenbaum s book Rest of chapter 2 will be covered in later lectures Read chapter 3, and section 4.1 of Stallings book