580 likes | 590 Views
Learn about the fundamental unit of operation in an operating system - processes. Understand how processes are used to run programs, their requirements, mechanisms, and principles of cooperation. Acquire the necessary terminology related to processes and their execution.
E N D
Introduction Computer System Structures Operating System Structures Processes Process Synchronization Deadlocks CPU Scheduling Memory Management Virtual Memory File Management Security Networking Distributed Systems Case Studies Conclusions Course OverviewPrinciples of Operating Systems
Motivation Objectives Processes and Programs Process States Operations on Processes Operating System Control Structures Process Control Block Processes and Threads Process Scheduling Cooperating Processes Interprocess Communication Important Concepts and Terms Chapter Summary Chapter Overview Processes
Motivation • applications can only run through the use of processes • processes are the fundamental unit of operation in an operating system • most components of the operating systems provide services for the execution of processes • processes require resources for their execution
Objectives • understand the use of processes for running programs • know the requirements and mechanisms for the execution of processes • understand the principles of cooperation between processes • acquire the terminology related to processes and their execution
Processes and Programs • both terms are used to describe the activities performed by a computer • program refers to the instructions as specified by the programmer • process refers to the activities performed by the computer as the instructions of a program are executed
Terminology • program • application • job • task • process • thread
Program • set of instructions specifying the activities necessary to accomplish a task • frequently used in a broad sense • static: usually doesn’t change unless modified by the programmer
Application • program used for a specific task • often used for programs available to the user, in contrast to programs used internally by the OS
Job • used to describe the unit of work in a batch system • frequently used synonymously to process
Task • unit of work from the user’s perspective in a time-sharing or multitasking system • often corresponds to an application program • may comprise several processes
Process • program in execution • unit of work from the OS perspective, in particular with respect to resource ownership • dynamic: changes its state over time (during execution) • may consist of several threads
Thread • smallest dispatchable unit in the OS • several threads are usually grouped into a process, and can cooperate on a task • sometimes also called lightweight processes
Execution of Programs • multiprogramming • multiprocessing • multitasking • multithreading
Example Processes • consider three processes • process A executes 100 instructions, reads two blocks from hard disk, and executes another 100 instructions • process B reads one block from hard disk, executes 100 instructions, and writes one block to disk • process C executes 1000 instructions, and writes two blocks to disk • timing • 10 ns per CPU instruction (100 MHertz clock frequency) • 10 ms average transfer time per block between hard disk and memory
overall execution time for single-programming20,002,000 + 20,001,000 + 20,010,000 ns = 60,013,000 ns CPU is idle for 60,000,000 ns CPU CPU CPU CPU Example Single-Programming 1,000 ns A B C 10,000 ns 1,000 ns 1,000 ns 1,000 ns I/O I/O I/O I/O 20 ,000 ,000 ns 10 ,000 ,000 ns 10 ,000 ,000 ns 20 ,000 ,000 ns
Multiprogramming • several programs are simultaneously under execution • they are between start and finish • simultaneously refers to a human time-scale (seconds) • at the CPU time scale, only one process is handled by the CPU (single processor systems) • creates logical parallelism • mainly used in batch systems to increase CPU utilization
logical parallelism: all three programs are run simultaneously problem: only one CPU available 1,000 ns 1,000 ns I/O A CPU A CPU A 20 ,000 ,000 ns 1,000 ns I/O B I/O B CPU B 10 ,000 ,000 ns 10 ,000 ,000 ns 10,000 ns CPU C I/O C 20 ,000 ,000 ns Example Multiprogramming 1,000 ns 1,000 ns A B C
solution: multiplexing of the CPU by switching between processes I/O operations concurrently with CPU operations problem: more complex requires process management Proc. A Proc. C Proc. B Proc. A I/O B I/O B I/O C Example Multiprogramming 1,000 ns 1,000 ns Attention: Dimensions not to scale 10,000 ns 1,000 ns 1,000 ns 1,000 ns I/O A 10 ,000 ,000 ns 20 ,000 ,000 ns 20 ,000 ,000 ns 10 ,000 ,000 ns
Multiprocessing • several processes run simultaneously on different CPUs • creates physical parallelism • advantages • short overall execution time • problems • CPUs might have to sharememory, I/O devices • low CPU utilization • communication between processes • process allocation and load balancing
physical parallelism: all three programs are run simultaneously on three different CPUs 1,000 ns 1,000 ns I/O A Proc. A Proc. A 20 ,000 ,000 ns 1,000 ns I/O B I/O B Proc. B 10 ,000 ,000 ns 10 ,000 ,000 ns 10,000 ns Proc. C I/O C 20 ,000 ,000 ns Example Multiprocessing 1,000 ns 1,000 ns CPU 1 CPU 2 CPU 3
Multitasking • conceptually similar to multiprogramming: better CPU utilization by switching between processes • more frequent switching so that users can interact with the program • necessary for time-sharing system
Multithreading • within one single process, multiple threads of execution are used • independent activities within one program or application can be performed in parallel • either on different CPUs, or via switching between threads • decreases the overhead of switching between processes
Process States • over their existence, processes can be in different states • newly created • ready to run on the CPU • running on the CPU • blocked because it is waiting for an event or an I/O operation • terminated
Process State Diagram new terminated admission release dispatch ready running time-out I/O or event completion I/O or event wait blocked
Operations on Processes • process creation • new batch job, user login, OS service, child process • process termination • normal completion, time limit exceeded, resources unavailable, protection error, calculation error, invalid instruction, OS intervention, parent termination, parent request • context switch • execution of one process is stopped, and another process continues • change of process state • implicit via context switch, explicit by the OS
Operating System Control Structures • used to maintain information about important entities and activities in the computer system • management of processes and resources • information about the status of processes and devices • usually stored in tables, possibly with pointers to further information • cross-references must exist between different tables • the OS must know the basic configuration of the computer system
Memory Tables Process 1 Memory Device Tables Devices Files File Tables Processes Process 1 Process n Process 2 Process 3 Process Tables . . . Process n OS Control Structures Process Image Process Image [adapted from Stallings 98]
Memory Tables • used to keep track of memory usage • allocation of main memory to processes • OS and user processes • allocation of secondary memory to processes • swap space or virtual memory • protection attributes of memory segments • access permissions
I/O Tables • used for the management of I/O devices • allocation of devices to processes • status of devices • available, allocated to a process • I/O operation in progress • memory segment involved in the I/O operation • in some operating systems, I/O devices are integrated into the file system
File Tables • used for the management of files and directories • access information for files • name, path • location on secondary memory • file status information • open, closed • processes using the file
Process Tables • used for the management of processes • location of the process • main memory, secondary storage • process attributes • process identification (pid) • unique number, often used as index into the process table • parent process, affiliated user, children
Process Image • user program • program to be executed • user data • program data, user stack, modifiable parts of the program • system stack • parameters, procedure call and return addresses • at least one per process • process control block • essential process data needed by the OS
Process Control Block • process identification • processor state information • process control information
Process Identification • process id, parent process, user id • used by the operating system for all activities involving processes • process management, main memory, I/O devices, interprocess communication • cross-reference to between process and other OS tables
Processor State Information • user registers • registers available to the user program • system registers • used for control and status information • program counter, condition codes (e.g. division by zero, overflow), status register (interrupt enabled, system/user mode) • stack pointers • points to the top of system stacks • these stacks contain parameters, procedure call and return addresses, and execution-related data
Process Control Information • scheduling and state information • process state, priority, time in ready queue, etc. • process relations • links to other processes (waiting queues, parent/child)interprocess communications • flags, signals, messages, shared memory • process privileges • memory access, instruction execution, resources • memory management • pointers to memory segments used by the process • resources • ownership and utilization of resources
Processes and Address Spaces Process 1 Process 2 Process n Process Identification Process Identification Process Identification Process Control Block Process State Information Process State Information Process State Information Process Control Information Process Control Information Process Control Information System Stack System Stack System Stack User Stack User Stack User Stack User Address Space User Address Space User Address Space Shared Address Space Shared Address Space [adapted from Stallings 98]
several processes need to be accomodated OS has its own memory section simplified view larger number of processes processes do not occupy one single section in memory, but several smaller ones (non-contiguous allocation) not the whole process image is always present in memory (virtual memory) Process 2 Main Memory Process n Process 1 Operating System Processes in Memory
Process Scheduling • objective: efficient allocation of CPU processing time to processes • in uniprocessor systems: multiplexing of the CPU between processes • in multiprocessor systems: allocation of processes to CPUs, load balancing across CPUs, multiplexing if there are more processes than processors • very important in multiprogramming and multitasking • maximization of CPU utilization • interaction between programs and users
Schedulers • job scheduler • long-term scheduling • medium-term scheduler • not used in all systems • CPU scheduler • short-term scheduling
Job Scheduler • manages processes that can’t be executed immediately • not enough memory available • CPU load too high • processes waiting for I/O are kept separately • controls the degree of multiprogramming • number of processes in main memory • is not invoked too frequently • mainly when processes enter or leave the system
Medium-Term Scheduler • processes are temporarily moved out of main memory to secondary storage (“swapping”) • memory space restrictions • to improve the process mix (balance between CPU-intensive and I/O-intensive processes) • determines which processes to swap out and in
CPU scheduler • manages processes in the ready queue • processes have all the resources they need, except for CPU time • is invoked very frequently • a process requests an I/O operation • time slice of a process is over • interrupt or trap • OS intervention • must be very fast to reduce overhead
Scheduling Queues • first-in, first-out (FIFO) data structures used to administer the scheduling of processes • types of queues • job queue: newly created processes not yet ready for execution • ready queue: processes in main memory and ready for execution • device queue: processes waiting for a particular device • event queue: processes waiting for an event • queuing diagrams display the interdependencies • can be derived from the process state diagram
Process State Diagram dispatch admission release not running running pause Queue Two-State Process Model Queuing Diagram release dispatch admission CPU pause [adapted from Stallings 98]
new terminated admission release dispatch ready running time-out I/O or event completion I/O or event wait blocked Five-State Process Model
Job Queue Event 1 Queue Event 2 Queue Event n Queue Queuing Diagram Five-State admission release forward dispatch Ready Queue CPU time-out I/O or event completion I/O or event wait . . .
Context Switch • the execution of one process on the CPU is halted, and the CPU continues with another • information about the old process must be saved • saved information about the new process must be restored • highly dependent on hardware support • multiple sets of registers • special instructions, e.g. to load and store registers • context switching time is pure overhead • no productive work is done • should be kept as low as possible
Threads • reduce the overhead of context switching • only essential information about individual threads is saved for a thread switch • program counter, register set, stack • other information is shared by a group of threads within a process or task • code section, data section, resources • enable asynchronous and distributed processing • support modular programs • also sometimes referred to as lightweight processes
Types of Threads • user-level threads • managed by the user process instead of the OS • uses user-level libraries instead of system calls • often more efficient since the OS is not involved • can be difficult to program • kernel-level threads • scheduled by the OS • threads may be distributed over several processors