Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Process control block (1) Operating Systems Previous lecture defined process as: Part of E1.9 - “Principles of Computers and Software Engineering” Program in execution Consisting of z z Lecture 3: Introduction to process management z z 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. E1.9 - Operating systems (Lect 3) Objectives Process Control Block (2) To introduce the concepts of: Process state and process control block Process scheduling Threads To Process state Program counter CPU registers CPU-scheduling information Memory management information Accounting information I/O status information e.g list of I/O devices allocated and list of open files Process creation Process termination Process switching E1.9 - Operating systems (Lect 3) Typical Process Control Block (PCB) includes: describe the steps involved in: 2 e.g. running, waiting e.g process priority e.g values of base and limit registers e.g process number E1.9 - Operating systems (Lect 3) 1 3 4 2 Process States (1) Process States (1) Two-state model (1) Two-state model (3) dispatch Creation Process Creation Not-running running Process Termination Termination Not-running-queue PN PN-1 … P2 P1 dispatch Processor pause pause Process scheduling decisions: Processes either running or not-running Processes created as not-running After completion, processes are terminated E1.9 - Operating systems (Lect 3) Which process to pick next from the not-running queue? When is a process paused? 5 E1.9 - Operating systems (Lect 3) Process States (1) Process States (2) Two-state model (2) Five-state model (1) Process management in the two-state model: Move process from not-running to running state Restore its context Move process from running to not-running state Save its context E1.9 - Operating systems (Lect 3) 6 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: Pausing of a process Two state model: some processes in the not-running queue might not actually be ready to execute Selecting a process for running Dispatching of a process 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 E1.9 - Operating systems (Lect 3) 3 7 8 4 Process States (2) Process States (3) Five-state model (2) Seven-state model (2) Process Creation Five-state model: Process Creation New dispatch admitted exit ready running pause I/O or event completion Waiting New Process Termination dispatch admitted exit ready ready-suspend I/O or event I/O or event wait running pause Load from disk Terminated Process Termination I/O or event wait Waiting completion Load from disk Terminated Suspend to disk Waiting-suspend Three queues: ready, new, waiting E1.9 - Operating systems (Lect 3) 9 E1.9 - Operating systems (Lect 3) Process States (3) Process States (3) Seven-state model (1) Seven-state model (3) 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: Five-state model: Long term scheduling: Medium-term scheduling Short-term or CPU scheduling 10 Determine whether process is admitted to system Determine whether process is placed to ready or ready-suspend queue Seven-state model takes into account these new states E1.9 - Operating systems (Lect 3) Seven state model illustrates the need for three different types of scheduling 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. Determine when to move a process to/from the suspended state 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. E1.9 - Operating systems (Lect 3) 5 11 12 6 Process States (4) Process States (5) Scheduling queues and queuing diagrams (1) 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. 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 Example: The ready queue and various I/O device queues [Silberschatz et al 2001] E1.9 - Operating systems (Lect 3) 13 E1.9 - Operating systems (Lect 3) Process States (4) Operations on Processes(1) Scheduling queues and queuing diagrams (2) Overview Creation PN I/O1 I/O2 PN-1 … B2 … P2 P1 Bm-1 Bm Blocked-waiting-for-I/O2 queue C1 C2 … Major process operations used in the diagrams: Blocked-waiting-for-I/O1 queue B1 Termination Ready-queue Cm-1 Cm dispatch Processor pause 15 Process creation Process termination Process switch Request I/O1 Request I/O2 (Example of a (partial) queuing diagram) E1.9 - Operating systems (Lect 3) 14 E1.9 - Operating systems (Lect 3) 7 16 8 Operations on Processes(2) Operations on Processes(3) Process creation (1) Process termination Process creation Create new PCB Allocate memory for instructions, data, stack Load instructions and data into allocated memory Initialise stack and context Initialise PCB Process termination When? 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 E1.9 - Operating systems (Lect 3) Release resources allocated to process Delete PCB entries 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. 17 E1.9 - Operating systems (Lect 3) Operations on Processes(2) Operations on Processes(4) Process creation (2) Process switch The creating process is called parent process; new processes are called children of this process Possibilities after creation: Parent continues execution concurrently with the children Parent waits for some or all of children to terminate in order to continue. Child process is a duplicate of the parent process Child process has a new program loaded into it E1.9 - Operating systems (Lect 3) 18 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 In terms of address space of the new process: Process or context switch In terms of execution 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). E1.9 - Operating systems (Lect 3) 9 19 20 10 Threads(1) Threads(2) Need for How? (2) Processes encompass two concepts: Kernel-level threads Kernel performs creation, scheduling and management in kernel space. Disadvantages: Advantages Threads: Code execution Resource grouping 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 E1.9 - Operating systems (Lect 3) Slower to create and manage than user-level threads Can easily handle threads with blocking system calls In multiprocessor environments, kernel can schedule threads on different processors Recycling threads 21 E1.9 - Operating systems (Lect 3) Threads(2) Threads(4) How? (1) Benefits User-level threads: Threads library (e.g POSIX pthreads) Have to write own scheduling algorithm Difficult to handle with blocking system calls E1.9 - Operating systems (Lect 3) 22 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 Fast to create and manage Disadvantages Responsiveness 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 Allocating memory and resources for processes is costly Utilization of multiprocessor architectures E1.9 - Operating systems (Lect 3) 11 23 24 12 Process management Recommended Reading What next? Next three lectures will examine issues introduced today First: Process scheduling Second: Process synchronization Third: Deadlocks E1.9 - Operating systems (Lect 3) 25 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 E1.9 - Operating systems (Lect 3) 27 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 E1.9 - Operating systems (Lect 3) 26 13 14