Transcript Unit 2
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
2
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
3
Process State Transition Diagram
end
begin
COP 5994 - Operating Systems
4
… with Suspend and Resume
COP 5994 - Operating Systems
5
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
6
Process Control Blocks Execution context
COP 5994 - Operating Systems
7
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
8
Context Switch
COP 5994 - Operating Systems
9
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
10
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
11
Interrupt Processing
1.
2.
3.
4.
Processor is running a process
Interrupt occurs: current instruction is completed
Processor determines nature of interrupt
Process executes context switch to interrupt
handler
5. Interrupt handler executes to completion
6. Next ready process is dispatched
COP 5994 - Operating Systems
12
Interrupt Processing
COP 5994 - Operating Systems
13
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
14
IA32 Hardware Interrupt
Classes
COP 5994 - Operating Systems
15
IA32 Exception Classes
COP 5994 - Operating Systems
16
Interprocess Communication
• Process to process communication
– Signal
– Message
COP 5994 - Operating Systems
17
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
18
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
19
?
• Process is useful concept
– to structure operating system
– Also: for any complex software
• Thread concept
– Introduces two-level process concept
COP 5994 - Operating Systems
20
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
21
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
22
Thread vs. Process
COP 5994 - Operating Systems
23
Thread State Transition
Diagram
COP 5994 - Operating Systems
24
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
25
Threading Models
• User-level threads
• Kernel-level threads
• Combination of user- and kernel-level
threads
COP 5994 - Operating Systems
26
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
27
User-level Threads
COP 5994 - Operating Systems
28
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
29
Kernel-level Threads
• Each thread has own execution
• one-to-one mapping:
– User and OS see multiple threads
COP 5994 - Operating Systems
30
Kernel-level Threads
COP 5994 - Operating Systems
31
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
32
Combining User- and Kernel-level
Threads
m-to-n thread mapping:
COP 5994 - Operating Systems
33
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
34
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
35
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
36
Thread Signal Delivery
COP 5994 - Operating Systems
37
Example: UNIX Process
COP 5994 - Operating Systems
38
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
39
Agenda for next week:
• Chapter 5 & 6
– Concurrency Issues
• Read ahead !
COP 5994 - Operating Systems
40