Processes and Threads Chapter 2 Processes Process Creation

advertisement
Processes
The Process Model
Chapter 2
Processes and Threads
2.1 Processes
2.2 Threads
2.3 Interprocess communication
2.4 Classical IPC problems
2.5 Scheduling
• Multiprogramming of four programs
• Conceptual model of 4 independent, sequential processes
• Only one program active at any instant
1
Process Creation
2
Process Termination
Principal events that cause process creation
1. System initialization
• Execution of a process creation system
1. User request to create a new process
2. Initiation of a batch job
Conditions which terminate processes
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
3
4
Process Hierarchies
Process States (1)
• Parent creates a child process, child processes
can create its own process
• Forms a hierarchy
– UNIX calls this a "process group"
• Windows has no concept of process hierarchy
• Possible process states
– all processes are created equal
– running
– blocked
– ready
5
• Transitions between states shown
6
Implementation of Processes (1)
Process States (2)
• Lowest layer of process-structured OS
– handles interrupts, scheduling
• Above that layer are sequential processes
Fields of a process table entry
7
8
Threads
Implementation of Processes (2)
The Thread Model (1)
Skeleton of what lowest level of OS does when an
interrupt occurs
(a) Three processes each with one thread
(b) One process with three threads
9
The Thread Model (2)
10
The Thread Model (3)
• Items shared by all threads in a process
• Items private to each thread
Each thread has its own stack
11
12
Thread Usage (1)
Thread Usage (2)
A word processor with three threads
A multithreaded Web server
13
Thread Usage (3)
14
Thread Usage (4)
• Rough outline of code for previous slide
Three ways to construct a server
(a) Dispatcher thread
(b) Worker thread
15
16
Implementing Threads in User Space
Implementing Threads in the Kernel
A user-level threads package
A threads package managed by the kernel
17
Hybrid Implementations
18
Scheduler Activations
• Goal – mimic functionality of kernel threads
– gain performance of user space threads
• Avoids unnecessary user/kernel transitions
• Kernel assigns virtual processors to each process
– lets runtime system allocate threads to processors
• Problem:
Fundamental reliance on kernel (lower layer)
calling procedures in user space (higher layer)
Multiplexing user-level threads onto kernellevel threads
19
20
Pop-Up Threads
Making Single-Threaded Code Multithreaded (1)
• Creation of a new thread when message arrives
(a) before message arrives
(b) after message arrives
Conflicts between threads over the use of a global variable
21
22
Making Single-Threaded Code Multithreaded (2)
Interprocess Communication
Threads can have private global variables
Two processes want to access shared memory at same time
Race Conditions
23
24
Critical Regions (1)
Critical Regions (2)
Four conditions to provide mutual exclusion
1.
2.
3.
4.
No two processes simultaneously in critical region
No assumptions made about speeds or numbers of CPUs
No process running outside its critical region may block
another process
No process must wait forever to enter its critical region
Mutual exclusion using critical regions
25
Mutual Exclusion with Busy Waiting (1)
26
Mutual Exclusion with Busy Waiting (2)
Proposed solution to critical region problem
(a) Process 0.
(b) Process 1.
27
Peterson's solution for achieving mutual exclusion
28
Sleep and Wakeup
Mutual Exclusion with Busy Waiting (3)
Entering and leaving a critical region using the
TSL instruction
29
Semaphores
Producer-consumer problem with fatal race condition30
Mutexes
Implementation of mutex_lock and mutex_unlock
The producer-consumer problem using semaphores
31
32
Monitors (2)
Monitors (1)
• Outline of producer-consumer problem with monitors
Example of a monitor
33
Monitors (3)
Solution to producer-consumer problem in Java (part 1)
– only one monitor procedure active at one time
– buffer has N slots
34
Monitors (4)
Solution to producer-consumer problem in Java (part 2)
35
36
Message Passing
Barriers
• Use of a barrier
The producer-consumer problem with N messages
37
Dining Philosophers (1)
•
•
•
•
– processes approaching a barrier
– all processes but one blocked at barrier
– last process arrives, all are let through
38
Dining Philosophers (2)
Philosophers eat/think
Eating needs 2 forks
Pick one fork at a time
How to prevent deadlock
A nonsolution to the dining philosophers problem
39
40
Dining Philosophers (3)
Solution to dining philosophers problem (part 1)
Dining Philosophers (4)
41
The Readers and Writers Problem
A solution to the readers and writers problem
Solution to dining philosophers problem (part 2)
42
The Sleeping Barber Problem (1)
43
44
Scheduling
The Sleeping Barber Problem (2)
Introduction to Scheduling (1)
• Bursts of CPU usage alternate with periods of I/O wait
– a CPU-bound process
– an I/O bound process
Solution to sleeping barber problem.
45
Introduction to Scheduling (2)
46
Scheduling in Batch Systems (1)
An example of shortest job first scheduling
Scheduling Algorithm Goals
47
48
Scheduling in Batch Systems (2)
Scheduling in Interactive Systems (1)
• Round Robin Scheduling
– list of runnable processes
– list of runnable processes after B uses up its quantum
Three level scheduling
49
50
Scheduling in Real-Time Systems
Scheduling in Interactive Systems (2)
Schedulable real-time system
• Given
– m periodic events
– event i occurs within period Pi and requires Ci
seconds
• Then the load can only be handled if
m
Ci
≤1
∑
i =1 Pi
A scheduling algorithm with four priority classes
51
52
Policy versus Mechanism
Thread Scheduling (1)
• Separate what is allowed to be done with
how it is done
– a process knows which of its children threads
are important and need priority
• Scheduling algorithm parameterized
– mechanism in the kernel
Possible scheduling of user-level threads
• Parameters filled in by user processes
– policy set by user process
53
Thread Scheduling (2)
Possible scheduling of kernel-level threads
• 50-msec process quantum
• threads run 5 msec/CPU burst
55
• 50-msec process quantum
• threads run 5 msec/CPU burst
54
Download