400 likes | 413 Views
Learn about the definition and management of processes and threads in operating systems, including process state transition, context switch, interrupt processing, interprocess communication, and threading models.
E N D
Operating Systems Operating Systems Unit 2: Process Context switch Interrupt Interprocess communication Thread Thread models
Definition of Process • Set of steps • Performance of a task • A program in execution COP 5994 - Operating Systems
Definition of Process • Process is • Identifiable Entity with properties • Text region • Stores the code that the processor executes • Data region • Stores variables and dynamically allocated memory • Stack region • Stores instructions and local variables for active procedure calls COP 5994 - Operating Systems
Process State Transition Diagram end begin COP 5994 - Operating Systems
… with Suspend and Resume COP 5994 - Operating Systems
Process Management • OS functionality: • Create process • Dispatch process • Block/wakeup process • Suspend/resume process • Terminate process • Also: • Change process attributes • Enable Interprocess communication COP 5994 - Operating Systems
Execution context Process Control Blocks COP 5994 - Operating Systems
Context Switch • stop a running process and start a ready process • Save execution context of running process • Load ready process’s execution context COP 5994 - Operating Systems
Context Switch COP 5994 - Operating Systems
Context Switch • Switch must be transparent to process • Effort for switch must be minimized • hardware support: • Special PCB register to help save/restore • Processor is given PCB and perform switch without software intervention COP 5994 - Operating Systems
Interrupt • Get attention of processor • enable reaction to signals from hardware • may be initiated by a running process: trap • E.g. dividing by zero or referencing protected memory • may be caused by external event • Asynchronous with the operation of the process • E.g., a keyboard key is pressed, or mouse is moved • Alternative: polling COP 5994 - Operating Systems
Interrupt Processing • Processor is running a process • Interrupt occurs: current instruction is completed • Processor determines nature of interrupt • Process executes context switch to interrupt handler • Interrupt handler executes to completion • Next ready process is dispatched COP 5994 - Operating Systems
Interrupt Processing COP 5994 - Operating Systems
Interrupt Classes • Interrupts are system specific • IA-32 Pentium architecture: • Interrupts • Hardware: from devices external to a processor • Software: to enable system calls • Exceptions • error has occurred: hardware or software instruction • Terms: fault, trap or abort COP 5994 - Operating Systems
IA32 Hardware Interrupt Classes COP 5994 - Operating Systems
IA32 Exception Classes COP 5994 - Operating Systems
Interprocess Communication • Process to process communication • Signal • Message COP 5994 - Operating Systems
Signals • Software interrupts • Limited data exchange • Processes may catch, ignore or mask a signal • Catch: run specific function on signal • Ignore: let OS run default function • Mask: prevent signal from occurring COP 5994 - Operating Systems
Message Passing • Send and receive functionality • Issues: • One directional • 1 sender, n receiver(s) • Blocking or non • Implementation: pipe or memory mapping • Security • Link reliability • Partner authentication COP 5994 - Operating Systems
? • Process is useful concept • to structure operating system • Also: for any complex software • Thread concept • Introduces two-level process concept COP 5994 - Operating Systems
Motivation for Threads • Threads have become prominent in: • Software design • More naturally expresses inherently parallel tasks • Performance • Scales better to multiprocessor systems • Cooperation • Shared address space incurs less overhead than IPC COP 5994 - Operating Systems
Thread definition • Lightweight process (LWP) • Thread of instructions or thread of control • Shares address space and other global information with its process • Registers, stack, signal masks and other thread-specific data are local to each thread COP 5994 - Operating Systems
Thread vs. Process COP 5994 - Operating Systems
Thread State Transition Diagram COP 5994 - Operating Systems
Thread Operations • Thread and process share common operations • Thread specific operations: • Cancel • Signals thread to terminate: thread can mask the cancellation signal • Join • Thread joins another thread: allows a thread to sleep until joined thread ends COP 5994 - Operating Systems
Threading Models • User-level threads • Kernel-level threads • Combination of user- and kernel-level threads COP 5994 - Operating Systems
User-level Threads • Threading operations occur in user space • Threads are created by runtime libraries • Many-to-one mapping: • User sees multiple threads • OS sees one process COP 5994 - Operating Systems
User-level Threads COP 5994 - Operating Systems
User-level Threads • Many-to-one thread mapping • Advantage: User-level scheduling • performance tuning • avoids OS context switch • more portable • Disadvantage: one process for OS • All threads in process will block as a whole • Cannot be scheduled on multiple processors COP 5994 - Operating Systems
Kernel-level Threads • Each thread has own execution • one-to-one mapping: • User and OS see multiple threads COP 5994 - Operating Systems
Kernel-level Threads COP 5994 - Operating Systems
Kernel-level Threads • one-to-one thread mapping • Advantage • Threads can run concurrently on multi processors: increased scalability and interactivity • Disadvantages: • context switching overhead • reduced portability COP 5994 - Operating Systems
Combining User- and Kernel-level Threads m-to-n thread mapping: COP 5994 - Operating Systems
Combining User- and Kernel-level Threads • Thread pool • Set of persistent worker kernel threads • Improves performance in environments where threads are frequently created and destroyed • Each new thread is executed by a worker thread COP 5994 - Operating Systems
Combining User- and Kernel-level Threads • Scheduler activation • Meant to address limitations of user-level threads • Kernel thread block, blocks all user threads • Multiple user threads in kernel thread cannot execute concurrently on multi processor • Upcall: • OS calls a user-level threading library that determines if any of its threads need rescheduling COP 5994 - Operating Systems
Thread Implementation Considerations • Signal delivery • Synchronous: • Occur as a direct result of program execution • Should be delivered to currently executing thread • Asynchronous • Occur due to an event typically unrelated to the current instruction • Threading library must determine each signal’s recipient so that asynchronous signals are delivered properly COP 5994 - Operating Systems
Thread Signal Delivery COP 5994 - Operating Systems
Example: UNIX Process COP 5994 - Operating Systems
Example: Unix threads • POSIX Pthreads: • User level thread library • Linux threads: • Task: process and thread • Fork vs. Clone system call • specify which resources to share with the child thread COP 5994 - Operating Systems
Agenda for next week: • Chapter 5 & 6 • Concurrency Issues • Read ahead ! COP 5994 - Operating Systems