Professional Documents
Culture Documents
OS Lec Week 5 23-12-19
OS Lec Week 5 23-12-19
Lecture week 05
Maryam Qamar
Lecturer CS & IT, UAJK
12/16/2019
Thread Usage
Reformatting thread
•
How would this have
worked with processes? A word processor with three threads
•
Interrupts
•
User Satisfaction?
12/16/2019
Thread Usage (Cont.)
•
Rough outline of code for previous slide
(a) Dispatcher thread
(b) Worker thread
12/16/2019
Why Threads?
•
Threads have the ability to share an address space and all of its data
among themselves.
•
They are lighter weight than processes, they are easier (i.e., faster) to
create and destroy than processes.
•
If there is substantial computing and also substantial I/O, having
threads allows these activities to overlap, thus speeding up the
application.
•
Useful on systems with multiple CPUs, where real parallelism is
possible.
12/16/2019
Thread
•
The process model is based on two independent concepts:
• Resource grouping and Execution.
• Entity of Execution now becomes Thread.
•
Has own program counter, register set, stack
•
Ready, Running and Blocked states like Process states and
Terminated state
•
Shares code (text), global data, open files
• With other threads within a single Heavy-Weight Process (HWP)
• But not with other HWP’s
12/16/2019
The Thread Model
•
Column 1: Items shared by all threads in a process
(process properties)
•
Column 2: Items private to each thread
12/16/2019
Making Single-Threaded Code Multithreaded
12/16/2019
Making Single-Threaded Code Multithreaded (Cont.)
12/09/2019
Concepts
•
Race condition
• Two or more processes are reading or writing some shared data and the final
result depends on who runs precisely when
12/09/2019
Concepts
•
Mutual exclusion
Prohibit more than one process from reading and writing the shared data at the
same time/Prohibit more than one process from entering their critical region at the
same time
•
Critical region
• Part of the program where the shared memory/file is accessed
12/09/2019
Critical Regions
entry section
critical section
exit section
remainder section
} while (TRUE);
12/09/2019
Mutual Exclusion with Busy Waiting
Disable interrupt
• After entering critical region, disable all interrupts
• Since clock is just an interrupt too, no CPU preemption can occur
Unwise: What if a process never turned them on again?
Doesn't work for multiprocessors: Disabling interrupts affects only the CPU that
executed the disable instruction. The other ones will continue running and can
access the shared memory.
12/09/2019
Mutual Exclusion with busy waiting
•
Lock variable
• A software solution
• A single, shared variable (lock, initially 0), before entering critical region,
programs test the variable, if 0, no contest; if 1, the critical region is occupied
Busy waiting - Continuously testing a variable until some desirable value
appears
Spin lock - A lock that uses busy waiting
• What is the problem?
• We've
12/09/2019 circled back from where we started: Race Condition
Mutual Exclusion with Busy Waiting : strict
alternation
12/09/2019
Mutual Exclusion with Busy Waiting : strict
alternation
Problem:
Both processes are in non-critical regions and turn is set to 1.
Process 0 completes its non-critical region and goes to while loop but
can not enter it critical region.
Violates condition 3.
So taking turns isn't a good idea when one process is much slower than
the other.
12/09/2019