Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Operating Systems

Lecture week 05

Maryam Qamar
Lecturer CS & IT, UAJK
12/16/2019
Thread Usage
Reformatting thread

Automatic Save thread


Interactive thread


How would this have
worked with processes? A word processor with three threads

Interrupts

User Satisfaction?
12/16/2019
Thread Usage (Cont.)

A multithreaded Web server


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

Conflicts between threads over the use of a global variable

12/16/2019
Making Single-Threaded Code Multithreaded (Cont.)

Threads can have private global variables – new


Scoping level
12/16/2019
Threads: Benefits

User responsiveness
• When one thread blocks, another may handle user I/O
• But: depends on threading implementation

Resource sharing: economy
• Memory is shared (i.e., address space shared)
• Open files, sockets shared

Speed
• E.g., Solaris: thread creation about 30x faster than heavyweight
process creation; context switch about 5x faster with thread

Utilizing hardware parallelism
• Like heavy weight processes, can also make use of multiprocessor
12/16/2019
architectures
Threads: Drawbacks

Synchronization
• Access to shared memory or shared variables must be controlled if the
memory or variables are changed
• Can add complexity, bugs to program code
• E.g., need to be very careful to avoid race conditions, deadlocks and other
problems

Lack of independence
• Threads not independent, within a Heavy-Weight Process (HWP)
• The RAM address space is shared; No memory protection from each other
• The stacks of each thread are intended to be in separate RAM, but if one
thread has a problem (e.g., with pointers or array addressing), it could
write over the stack of another thread
12/16/2019
Interprocess Communication
Race Conditions
A folder that holds files waiting to
be printed until the printer is ready.

Two processes want to access shared memory at same time

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

• In our former example, the possibilities are various

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

Four conditions to provide mutual exclusion and have parallel


processes cooperate correctly and efficiently using shared
data.
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


12/09/2019
Critical Section Problem
do {

entry section

critical section

exit section

remainder section
} while (TRUE);

General structure of a typical process Pi 16


12/09/2019
Critical Regions (Cont.)

Mutual exclusion using critical regions

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

Proposed solution to critical region problem


(a) Process 0. (b) Process 1.

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

You might also like