Chapter 4: Threads & Concurrency: Difference Between Multiprocessing and Multithreading
Chapter 4: Threads & Concurrency: Difference Between Multiprocessing and Multithreading
Chapter 4: Threads & Concurrency: Difference Between Multiprocessing and Multithreading
Benefits
Responsiveness
Resource Sharing
Economy
Scalability
Multicore or multiprocessor systems putting pressure on programmers,
challenges include:
Dividing activities
Balance
Data splitting
Data dependency
Testing and debugging
While in Multithreading,
In Multiprocessing, Process creation is
4. process creation is according
a time-consuming process.
to economical.
.NO Multiprocessing Multithreading
While in Multithreading, a
In Multiprocessing, every process
5. common address space is
owned a separate address space.
shared by all the threads.
Multicore Programming
Types of parallelism
Data parallelism – distributes subsets of the same data across
multiple cores, same operation on each
Amdahl’s Law
Linux
Mac OS X
iOS
Android
Multithreading Models
Many-to-One = Many user-level threads mapped to
single kernel thread
One thread blocking causes all to block
Multiple threads may not run in parallel on multicore
system because only one may be in kernel at a time
Few systems currently use this model
Examples:
Solaris Green Threads
Pthreads
May be provided either as user-level or kernel-level
A POSIX standard (IEEE 1003.1c) API for thread
creation and synchronization
Specification, not implementation
API specifies behavior of the thread library,
implementation is up to development of the library
Common in UNIX operating systems (Linux & Mac
OS X)
Implicit Threading
Growing in popularity as numbers of threads
increase, program correctness more difficult with
explicit threads
Creation and management of threads done by
compilers and run-time libraries rather than
programmers
Five methods explored
Thread Pools
Fork-Join
OpenMP
Thread Pools
Create a number of threads in a pool where they
await work
Advantages:
Usually slightly faster to service a request with
periodically
Fork-Join Parallelism
Multiple threads (tasks) are forked, and then joined.
Intel Threading Building Blocks (TBB)
Template library for designing parallel C++ programs
A serial version of a simple for loop
Thread-local storage
Scheduler Activations
Signal Handling
Signals are used in UNIX systems to notify a
process that a particular event has occurred.
A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled by one of two signal handlers:
1. default
2. user-defined
Every signal has default handler that kernel runs
when handling signal
User-defined signal handler can override
default
For single-threaded, signal delivered to process
signal applies
Deliver the signal to every thread in the process
process
Assign a specific thread to receive all signals for
the process.
Thread Cancellation
Terminating a thread before it has finished
Thread to be canceled is target thread
Two general approaches:
Asynchronous cancellation terminates the
Windows Threads
Linux Threads
Windows Threads
Windows API – primary API for Windows
applications
Implements the one-to-one mapping, kernel-level
Each thread contains
A thread id
Linux
Threads
Linux refers to them as tasks rather than threads
Thread creation is done through clone() system
call
clone() allows a child task to share the address
space of the parent task (process)
Flags control behavior
struct task_struct points to process data
structures (shared or unique)
Bounded-Buffer Problem
n buffers, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full initialized to the value 0
Semaphore empty initialized to the value n
The structure of the producer process
while (true) {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
while (true) {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
}
Readers-Writers Problem
A data set is shared among a number of concurrent
processes
Readers – only read the data set; they do not
Shared Data
Data set
Semaphore rw_mutex initialized to 1
Semaphore mutex initialized to 1
Integer read_count initialized to 0
The structure of a writer process
while (true) {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
}
The structure of a reader process
Dining-Philosophers Problem
N philosophers’ sit at a round table with a bowl of
rice in the middle.
They spend their lives alternating thinking and eating.
They do not interact with their neighbors.
when done
In the case of 5 philosophers, the shared
data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
Semaphore Solution
The structure of Philosopher i :
while (true){
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Each philosopher “i” invokes the operations pickup() and putdown() in
the following sequence:
DiningPhilosophers.pickup(i);
/** EAT **/
DiningPhilosophers.putdown(i);
No deadlock, but starvation is possible
Atomic variables
atomic_t is the type for atomic integer
Consider the variables
atomic_t counter;
int value;