700 likes | 748 Views
Chapter 3: Processes. Prerequisite Test Results. 80% of students are familiar with C/C++ Total score is 120 Above 80: 0 70-79: 3 60-69: 6 50-59: 6 40-49: 6 Below 40: 11. Objectives. To introduce the notion of a process
E N D
Prerequisite Test Results • 80% of students are familiar with C/C++ • Total score is 120 • Above 80: 0 • 70-79: 3 • 60-69: 6 • 50-59: 6 • 40-49: 6 • Below 40: 11
Objectives • To introduce the notion of a process • To describe process scheduling, creation, termination, and communication • To explore interprocess communication using shared memory and message passing • To describe communication in client-server systems
OS Major Functions • Interleave the execution of multiple processes, to maximize processor utilization while providing reasonable response time • Allocate resources to processes • Support interprocess communication and user creation of processes
Process • A program in execution • An instance of a program running on a computer • The entity that can be assigned to and executed on a processor • A unit of activity characterized by • the execution of a sequence of instructions • a current state • an associated set of system resources
Process Concept • Multiple parts • The program code, also called text section • Current activity context including programcounter, processor registers • Stackcontaining temporary data • Function parameters, return addresses, local variables • Data sectioncontaining global variables • Heapcontaining memory dynamically allocated during run time
Process Concept (Cont.) • Program is passive entity stored on disk (executable file), process is active • Program becomes process when executable file loaded into memory • How to execute a program? • Execution of a program starts via GUI mouse click, command line entry of its name, etc. • Can we have one program in several processes? • Consider multiple users executing the same program on cse.unl.edu server
Process Stack Parameter passing in a read procedure call: Figure (a) The stack before the call to read. (b) The stack while the called procedure is active.
Process State • As a process executes, it changes state • new: The process is being created • running: Instructions are being executed • waiting: The process is waiting for some event to occur • e.g., a completion of an I/O operation or • a semaphore signal from another process • ready: The process is waiting to be assigned to a processor • terminated: The process has finished execution
Process Elements (I) • Identifier • State • Priority • Program counter • Memory pointers: to code and data How to describe a process? What elements are included in it?
Process Elements (II) • Context data: value of CPU registers • I/O status information • Outstanding I/O requests • Assigned I/O devices and used files • Accounting information • Amount of processor time & clock time used • Time limits
Process Control Block (PCB) • Each process is represented by a PCB • Created and managed by the operating system • Contains the process elements
Process Control Block • Allows support for multiple processes
OS Control Tables Tables are constructed for each entity (i.e., process, resource) the operating system manages
Process Tables • Manage processes • A process is composed of program, data, heap, stack, and attributes ---- process image • User Data • The modifiable part of the user space, including data section and heap. • User Program • The program to be executed. • Stack • Each process has one or more last-in-first-out (LIFO) stacks associated with it. A stack is used to store parameters and returning addresses for procedure and system calls. • Process Control Block • Data needed by the OS to control the process
Process Location • Where are the processes located? • To manage and execute a process, at least a small portion of its image must be maintained in main memory • OS must know the location of each page of each process image, achieved by process tables
PCB: Process Control Information • Data structuring: a process may be linked to other process in a queue, ring, or some other structure. • For example, all ready processes for a particular priority level may be linked in a queue. • A process may exhibit a parent-child (creator-created) relationship with another process. • The process control block may contain pointers to other processes to support these structures.
Process Creation • Assign a unique process identifier • Allocate space for the process • Initialize process control block • Set up appropriate linkages • Create or expand other data structures
When to Switch Process • Clock interrupt • process has executed for the maximum allowable time slice • I/O interrupt • Memory fault • memory address is in disk so it must be brought into main memory
When to Switch Process • Trap • error or exception occurred • may cause process to be moved to Exit state • System Call • Inter-process communication • I/O operation such as file open • Lead to a transfer to an OS routine
Process (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 via a context switch • Context of a process represented in the PCB • Context-switch time is overhead; the system does no useful work while switching • The more complex the OS and the PCB the longer the context switch • Time dependent on hardware support • Some hardware provides multiple sets of registers per CPU multiple contexts loaded at once
Process (Context) Switch • Save context of processor including program counter and other registers • Update the process control block of the process that is currently in the Running state • Move process control block to appropriate queue – e.g., ready; blocked • Select another process for execution • Update the process control block of the process selected • Restore context of the selected process • Update memory-management data structures (page tables, TLB, etc.)
Execution of the OS Funtions • Process Switch vs. • Mode Switch
Execution of the Operating System • Process-based operating system • Implement the OS as a collection of system processes • Process switch for OS service
Execution of the Operating System • Execution Within User Processes • Operating system software within context of a user process • Mode switch for OS service
OS Executes in User Space • Mode switch for OS service
Process vs. Mode Switch • Save context of processor including program counter and other registers • Update the process control block of the process that is currently in the Running state • Move process control block to appropriate queue – ready; blocked; ready/suspend • Select another process for execution • Update the process control block of the process selected • Restore context of the selected process • Update memory-management data structures
Mode Switch • Save context of processor including program counter and other registers • Update the process control block of the running process • Mode changes to kernel mode, finishes the OS routine • Mode changes back, restore the context, and continue the running process in user mode
Process Scheduling (I) • What does process scheduling do? • What is the objective of doing process scheduling?
Process Scheduling (II) • To maximize CPU use and for time sharing, quickly switch processes onto CPU • Process scheduler selects among available processes for next execution on CPU • OS maintains scheduling queues of processes • Ready queues – set of all processes residing in main memory, ready and waiting to execute • Device queues – set of processes waiting for an I/O device • Processes migrate among the various queues
Process Scheduling (III) • Three different categories of process scheduler • Short-term scheduler • Long-term scheduler • Medium-term scheduler
Short-term Scheduler • Short-term scheduler (or CPU scheduler or Dispatcher) – selects which process should be executed next and allocates CPU • Sometimes the only scheduler in a system • Short-term scheduler is invoked frequently (milliseconds) (must be fast) • Trace of the Process • Sequence of instructions that execute for a process • Dispatcher switches the processor from one process to another Interleaving process traces
Long-term Scheduler • Long-term scheduler (or job scheduler) – selects which processes should be admitted (i.e., brought into ready queue) • The long-term scheduler controls the (maximum) degree of multiprogramming • Long-term scheduler is invoked infrequently (seconds, minutes) (may be slow) • Processes can be described as either: • I/O-bound process • – spends more time doing I/O than computations, many short CPU bursts • CPU-bound process • – spends more time doing computations; few very long CPU bursts • Long-term scheduler strives for good process mix
Addition of Medium Term Scheduling • Medium-term scheduler can be added if (actual) degree of multiprogramming needs to change • Remove process from memory, store on disk, bring back in from disk to continue execution: swapping • Present in all systems with virtual memory (Chap 9)
Suspended Processes • Processor is faster than I/O so all executable processes could be waiting for I/O, while there are some new processes waiting to be admitted • Swap these processes to disk to free up more memory to admit new processes • Blocked state becomes suspend state when swapped to disk • Two new states • Blocked/Suspend • Ready/Suspend
Operations on Processes • System must provide mechanisms for: • process creation, • process termination, • and so on as detailed next
Process Creation • Parentprocess creates childrenprocesses, which, in turn create other processes, forming a tree of processes • Generally, process identified and managed via aprocess identifier (pid) • Resource sharing options • Parent and children share all resources • Children share subset of parent’s resources • Parent and children share no resources • For example, rfork in FreeBSD permits fine-grained sharing of resources between parent and child processes • Execution options • Parent and children execute concurrently (e.g., fork) • Parent waits until children terminate (e.g., vfork)
Process Creation (Cont.) • Address space • Child duplicate of parent • UNIX example • fork()system call creates new process
fork() • Process creation in Unix could be made by means of the kernel system call, fork() • When a process issues a fork request, the OS: • Allocates a slot in the process table for the new process; • Assigns a unique process ID to the child process; • Makes a copy of the process image of the parent, with the exception of any shared memory; • Increments counters for any files owned by the parent, to reflect that an additional process now also owns those files; • Assigns the child process to the Ready to Run state; • Returns the ID number of the child to the parent process, and a 0 value to the child process.
Process Creation (Cont.) • Address space • Child duplicate of parent • Child has a program loaded into it • UNIX examples • fork()system call creates new process • exec() system call used after a fork() to replace the process’ memory space with a new program