CSC400 - Operating Systems 3. Process Concepts J. Sumey Overview Concurrency Processes & Process States Process Accounting Interrupts & Interrupt Processing Interprocess Communication CSC400 - Process Concepts 2 CSC400 Operating Systems 1
Concurrency ability to perform multiple actions at the same time (but not simultaneously) can truly only run as many tasks as there are processors available thus: multiprogramming rapid switching of a processor among multiple, independent, sequential processes in a fashion that makes it appear that all processes are running simultaneously ex: CPU timeline CSC400 - Process Concepts 3 Multiprogramming Side Effect programmer can not make assumptions about timing! because processor is shared with other processes otherwise, processes need not be concerned about underlying multiprogramming environment CSC400 - Process Concepts 4 CSC400 Operating Systems 2
Concurrency Levels 1. OS can run multiple processes multiple tasks typically independent 2. apps can have multiple threads multiple paths of execution within a single application usually dependent CSC400 - Process Concepts 5 Processes entity of a running program, a.k.a. task includes processor state & address space i.e. a "virtual CPU" memory diagram see refs demo: sizes.c CSC400 - Process Concepts 6 CSC400 Operating Systems 3
Processes process address space: text region contains the compiler-generated machine code fixed size by compiler set to "read-only" by loader data (static) region static variables: both global & local subdivided into initialized and uninitialized areas stack for subroutine linkage, parameters & local variables heap (opt.) for dynamically (runtime) allocated objects: malloc(), new CSC400 - Process Concepts 7 Process States 3 possible conditions of a given process: 1. running currently executing on a processor 2. blocked a suspended condition resulting from an I/O request or other event 3. ready process is runnable but awaiting processor time * modern OSs usually define some additional states like suspend CSC400 - Process Concepts 8 CSC400 Operating Systems 4
State Transition Diagram Dispatch Running Timeout Block Exit Blocked Entry Ready Wakeup ready processes maintained in ready queue blocked processes in blocked queue[s] CSC400 - Process Concepts 9 Process State Transitions insertion into ready list process creation ready run processor available run ready time slot (quantum) expiration run blocked I/O request or IPC event (user initiated) blocked ready I/O complete or IPC synchronization removal process completion (normal or abnormal) CSC400 - Process Concepts 10 CSC400 Operating Systems 5
Process Operations create destroy dispatch block wake up change priority suspend / resume facilitate IPC CSC400 - Process Concepts 11 Process Procreation fork / spawn concept of a parent process "cloning" itself into another child process that typically mutates into some other program Unix ex: init login shell process hierarchies parent-child relationships resulting from creating & destroying processes CSC400 - Process Concepts 12 CSC400 Operating Systems 6
Process Accounting OS keeps track of all processes using a process table of process control blocks (PCBs) PCB contains everything OS needs to manage the process process ID (PID, PPID), state, priority, privileges parent & child process pointers address space descriptors execution context (processor registers, stack) allocated resources (files, I/O devices, etc.) CSC400 - Process Concepts 13 Process/Context Switching stopping a running process and switching to some other ready process save execution context of running process in its PCB, update other PCB contents move PCB to appropriate queue load a ready process's previously saved context from its PCB process-switch time must be minimized occurs extremely frequently is pure overhead CSC400 - Process Concepts 14 CSC400 Operating Systems 7
Process Switch Causes interrupts clock/timer, I/O, faults traps exception to normal process execution supervisor call OS services, ex: open file CSC400 - Process Concepts 15 Process Scheduler the [very important] part of the OS that handles task switching (i.e. processor scheduling) via some prescribed algorithm yields a process-structured OS model must balance overall system efficiency with individual process fairness (including the OS) also includes interrupt processing (sometimes done in assembly) CSC400 - Process Concepts 16 CSC400 Operating Systems 8
Interrupts & Interrupt Processing Interrupts Happen! process traps (synchronous) math errors: divide by 0 protection violation: privileged instruction, illegal memory reference software interrupt, breakpoint external events (asynchronous) I/O hardware IPC CSC400 - Process Concepts 18 CSC400 Operating Systems 9
Interrupt Handlers in response to an interrupt, processor suspends current task and vectors to an interrupt handler is never called synchronously more efficient than polling when handling I/O devices but somewhat more complicated to program and debug CSC400 - Process Concepts 19 Interrupt Processing Sequence 1. CPU interrupt line is activated timers, I/O controllers, coprocessors 2. CPU completes current instruction then saves states (on stack) 3. CPU vectors to appropriate handler typically via vector table 4. interrupt handler processes interrupt 5. previous state (and process) is restored via special instruction (rti/rte, irt) CSC400 - Process Concepts 20 CSC400 Operating Systems 10
Interprocess Communication IPC Why? OS facility to support communications between sending & receiving processes for data exchange, synchronization, etc. ex: DBMSs, web servers, gaming controlled mechanism to legitimately circumvent memory protection mechanisms CSC400 - Process Concepts 22 CSC400 Operating Systems 11
Signals a type of software interrupt used to notify a process of some event data exchange process synchronization / timing failures user CSC400 - Process Concepts 23 Signal Handling a process can arrange with the OS a certain disposition for signals masked blocked (OS does not send) catch specify a routine the OS will call when signal is sent to the process ignore i.e. disabled default typically abort, w/ memory dump CSC400 - Process Concepts 24 CSC400 Operating Systems 12
IPC Implementations shared memory mapping of a common range of memory to multiple processes message passing process-defined data exchange via OS-supplied queues pipes memory-based using file model access is synchronized by OS sockets like pipes only between process on different hosts CSC400 - Process Concepts 25 References processes: http://www.geeksforgeeks.org/memory-layout-of-c-program/ http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part- 1.html CSC400 - Process Concepts 26 CSC400 Operating Systems 13