Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems
COP Operating Systems2 Definition of Process Set of steps Performance of a task A program in execution
COP Operating Systems3 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 Operating Systems4 Process State Transition Diagram begin end
COP Operating Systems5 … with Suspend and Resume
COP Operating Systems6 Process Management OS functionality: –Create process –Dispatch process –Block/wakeup process –Suspend/resume process –Terminate process Also: –Change process attributes –Enable Interprocess communication
COP Operating Systems7 Process Control Blocks Execution context
COP Operating Systems8 Context Switch stop a running process and start a ready process –Save execution context of running process –Load ready process’s execution context
COP Operating Systems9 Context Switch
COP Operating Systems10 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 Operating Systems11 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 Operating Systems12 Interrupt Processing 1.Processor is running a process 2.Interrupt occurs: current instruction is completed 3.Processor determines nature of interrupt 4.Process executes context switch to interrupt handler 5.Interrupt handler executes to completion 6.Next ready process is dispatched
COP Operating Systems13 Interrupt Processing
COP Operating Systems14 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 Operating Systems15 IA32 Hardware Interrupt Classes
COP Operating Systems16 IA32 Exception Classes
COP Operating Systems17 Interprocess Communication Process to process communication –Signal –Message
COP Operating Systems18 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 Operating Systems19 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 Operating Systems20 ? Process is useful concept –to structure operating system –Also: for any complex software Thread concept –Introduces two-level process concept
COP Operating Systems21 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 Operating Systems22 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 Operating Systems23 Thread vs. Process
COP Operating Systems24 Thread State Transition Diagram
COP Operating Systems25 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 Operating Systems26 Threading Models User-level threads Kernel-level threads Combination of user- and kernel-level threads
COP Operating Systems27 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 Operating Systems28 User-level Threads
COP Operating Systems29 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 Operating Systems30 Kernel-level Threads Each thread has own execution one-to-one mapping: –User and OS see multiple threads
COP Operating Systems31 Kernel-level Threads
COP Operating Systems32 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 Operating Systems33 Combining User- and Kernel-level Threads m-to-n thread mapping:
COP Operating Systems34 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 Operating Systems35 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 Operating Systems36 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 Operating Systems37 Thread Signal Delivery
COP Operating Systems38 Example: UNIX Process
COP Operating Systems39 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 Operating Systems40 Agenda for next week: Chapter 5 & 6 –Concurrency Issues Read ahead !