OS Part 02 PDF
OS Part 02 PDF
2.1 Processes
2.2 Threads
2.3 Interprocess communication
2.4 Scheduling
1
2.1 Processes
2
Processes
The Process Model
3
Processes
Process Concept
• An operating system executes a variety of programs:
– Batch system – jobs
– Time-shared systems – user programs or tasks
• Process – a program in execution; process execution must
progress in sequential fashion
• A process resources includes:
– Address space (text segment, data segment, stack segment)
– CPU (virtual)
• program counter
• registers
• stack
– Other resource (open files, child processes…)
4
Processes
Process in Memory
5
Processes
Process Creation (1)
6
Processes
Process Creation (2)
• Address space
– Child duplicate of parent
– Child has a program loaded into it
• UNIX examples
– fork system call creates new process
– exec system call used after a fork to replace the
process’ memory space with a new program
7
Processes
Process Creation (3) : Example
8
Processes
Process Termination
9
Processes
Process Hierarchies
10
Processes
Process States (1)
13
Processes
context switch
14
Processes
Implementation of Processes (1)
17
Threads
The Thread Model
18
Threads
Process with single thread
• A process (heavyweight):
– Address space (text segment, data segment,
stack segment)
– Single thread of execution
• program counter
• registers
• Stack
– Other resource (open files, child processes…)
19
Threads
Process with multiple threads
Multiple threads of execution in the same
environment of process
– Address space (text segment, data segment,
stack segment)
– Multiple threads of execution, each thread has
private set:
• program counter
• registers
• stack
– Other resource (open files, child processes…)
20
Threads
Single and Multithreaded Processes
PC
PC PC PC
21
Threads
Items shared and Items private
• Responsiveness
• Resource Sharing
• Economy
• Utilization of Multiprocessor Architectures
23
Threads
Thread Usage (1)
26
Threads
Implementing Threads in User Space (1)
user
u u u u u u u u u thread
29
Threads
Implementing Threads in the Kernel (1)
31
Threads
Hybrid Implementations
34
Cooperating Processes
35
Problem of shared data
36
Race Conditions
• Two processes want to access shared memory at same time and the final result
depends who runs precisely, are called race condition
• Mutual exclusion is the way to prohibit more than one process from accessing
to shared data at the same time
• Example of race condition
37
Critical Regions (1)
The Part of the program where the shared memory is accessed is called
Critical Regions (Critical Section)
Four conditions to provide mutual exclusion
1. No two processes simultaneously in critical region
2. No assumptions made about speeds or numbers of CPUs
3. No process running outside its critical region may block another
process
4. No process must wait forever to enter its critical region
38
Critical Regions (2)
39
Solution: Mutual exclusion with Busy waiting
• Software proposal
– Lock Variables
– Strict Alternation
– Peterson's Solution
• Hardware proposal
– Disabling Interrupts
– The TSL Instruction
40
Mutual exclusion with Busy waiting
Software Proposal 1: Lock Variables
41
Mutual exclusion with Busy waiting
Software Proposal 1: Event
42
Mutual exclusion with Busy waiting
Software Proposal 2: Strict Alternation
43
Mutual exclusion with Busy waiting
Software Proposal 2: Strict Alternation
• Only 2 processes
• Responsibility Mutual Exclusion
– One variable "turn“, one process “turn” come
in CS at the moment.
44
Mutual exclusion with Busy waiting
Software Proposal 3: Peterson's Solution
Boolean
45
Mutual exclusion with Busy waiting
Software Proposal 3: Peterson's Solution
46
Mutual exclusion with Busy waiting
Comment for Software Proposal 3:
Peterson's Solution
• Satisfy 3 conditions:
– Mutual Exclusion
• Pi can enter CS when interest[j] == F, or turn == i
• If both want to come back, because turn can only receive value
0 or 1, so one process enter CS
– Progress
• Using 2 variables distinct interest[i] ==> opposing cannot lock
– Bounded Wait: both interest[i] and turn change value
• Not extend into N processes
47
Mutual exclusion with Busy waiting
Comment for Busy-Waiting solutions
48
Mutual exclusion with Busy waiting
Hardware Proposal 1: Disabling Interrupt (1)
49
Mutual exclusion with Busy waiting
Hardware proposal 1: Disable Interrupt (2)
• Not be careful
– If process is locked in CS?
• System Halt
– Permit process use command privileges
• Danger!
• System with N CPUs?
– Don't ensure Mutual Exclusion
50
Mutual exclusion with Busy waiting
Hardware proposal 2: TSL Instruction
51
Mutual exclusion with Busy waiting
Hardware proposal 2: Applied TSL
52
Mutual exclusion with Busy waiting
Comment for hardware solutions
53
Mutual exclusion with Busy waiting
Comment
54
Synchronous solution with Sleep & Wakeup
55
Synchronous solution with Sleep & Wakeup
"Sleep & Wakeup" solution
56
Synchronous solution with Sleep & Wakeup
"Sleep & Wake up" solution: Idea
• OS support 2 primitive:
– Sleep(): System call receives blocked status
– WakeUp(P): P process receives ready status
• Application
– After checking condition, coming in CS or calling
Sleep() depend on result of checking
– Process that using CS before, will wake up processes
blocked before
57
Synchronous solution with Sleep & Wakeup
Apply Sleep() and Wakeup()
58
Synchronous solution with Sleep & Wakeup
Problem with Sleep & WakeUp
• Reason:
– Checking condition and giving up CPU can be
broken
– Lock variable is not protected
59
Synchronous solution with Sleep & Wakeup
Semaphore
60
Synchronous solution with Sleep & Wakeup
Semaphore
61
Synchronous solution with Sleep & Wakeup
Semaphore
– Semaphore S; // initialized to 1
– Down(s);
Critical Section
– Up(s);
62
Synchronous solution with Sleep & Wakeup
Using Semaphore
63
Synchronous solution with Sleep & Wakeup
Monitor
64
Synchronous solution with Sleep & Wakeup
Monitor: structure
65
Synchronous solution with Sleep & Wakeup
Using Monitor
66
Synchronous solution with Sleep & Wakeup
Message Passing
• Processes must name each other explicitly:
– send (P, message) – send a message to process P
– receive(Q, message) – receive a message from process
Q
• Properties of communication link
– Links are established automatically
– A link is associated with exactly one pair of
communicating processes
– Between each pair there exists exactly one link
– The link may be unidirectional, but is usually bi-
directional
67
Classical Problems of Synchronization
• Bounded-Buffer Problem
(Producer-Consumer Problem)
• Readers and Writers Problem
• Dining-Philosophers Problem
(Lab)
68
2.4 Scheduling
69
Scheduling
Introduction to Scheduling (1)
70
Scheduling
Introduction to Scheduling (2)
• Selects from among the processes in memory that are ready to execute,
and allocates the CPU to one of them
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting or new process is created to ready
4. Terminates
• Nonpreemptive scheduling algorithm picks process and let it run until
it blocks or until it voluntarily releases the CPU
• preemptive scheduling algorithm picks process and let it run for a
maximum of fix time
73
Scheduling
Introduction to Scheduling (5)
terminated
terminated
new
new admit dispatch exit
ready
ready running
running
interrupt
I/O or event I/O or
completion event wait
waiting
waiting
74
Scheduling
Introduction to Scheduling (6)
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their execution
per time unit
• Turnaround time – amount of time to execute a particular
process
• Waiting time – amount of time a process has been waiting
in the ready queue
• Response time – amount of time it takes from when a
request was submitted until the first response is produced,
not output (for time-sharing environment)
75
Scheduling
Introduction to Scheduling (7)
Optimization Criteria
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
76
Scheduling
Introduction to Scheduling (8)
Scheduling Algorithm Goals
77
Scheduling
Scheduling in Batch Systems (1)
First-Come, First-Served (FCFS) Scheduling
Process Burst Time
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
78
Scheduling
Scheduling in Batch Systems (2)
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order
P2 , P3 , P1
• The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
80
Scheduling
Scheduling in Batch Systems (4)
Example of Non-Preemptive SJF
P1 P3 P2 P4
0 3 7 8 12 16
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
81
Scheduling
Scheduling in Interactive Systems (1)
82
Scheduling
Scheduling in Interactive Systems (2)
Round Robin (RR)`
• Each process gets a small unit of CPU time (time
quantum), usually 20-50 milliseconds. After this time has
elapsed, the process is preempted and added to the end of
the ready queue.
• If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU time
in chunks of at most q time units at once. No process
waits more than (n-1)q time units.
• Performance
– q large ⇒ FCFS
– q small ⇒ overhead is too high respect to context switch
83
Scheduling
Scheduling in Interactive Systems (3)
84
Scheduling
Priority Scheduling (1)
Priority Scheduling: A priority number (integer) is associated
with each process
– The CPU is allocated to the process with the highest priority
– Preemptive
– nonpreemptive
• SJF is a priority scheduling where priority is the predicted next CPU
burst time
• Problem ≡ Starvation – low priority processes may never execute
• Solution ≡ Aging – as time progresses increase the priority of the
process
85
Scheduling
Priority Scheduling (2)
86
Scheduling
Priority Scheduling (3)
88
Scheduling
Scheduling in Real-Time Systems(2)
91
Scheduling
Thread Scheduling (2)