0% found this document useful (0 votes)
13 views21 pages

4 PThreads

The document discusses the concept of threads as lightweight processes, highlighting their advantages over traditional processes in terms of speed, efficiency, and communication. It explains different threading models, such as many-to-one, one-to-one, and many-to-many, along with their respective pros and cons. Additionally, it covers the management of threads through user-level and kernel-level strategies, as well as the pthread library for creating and managing threads.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

4 PThreads

The document discusses the concept of threads as lightweight processes, highlighting their advantages over traditional processes in terms of speed, efficiency, and communication. It explains different threading models, such as many-to-one, one-to-one, and many-to-many, along with their respective pros and cons. Additionally, it covers the management of threads through user-level and kernel-level strategies, as well as the pthread library for creating and managing threads.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Threads

(light weight processes)


Credits: Chester Rebeiro
IIT Madras

1
Consider this Example
Process

Processor Processor Processor Processor


1 2 3 4

2
Speeding up with multiple processes
10000000 / 4 = 2500000 Create 4 processes, each loop does 1/4th of
the work Properties:

4 fork system calls needed; one


Process Process Process Process for creating each process

Each process is isolated from


each other

IPC mechanisms to
Processor Processor Processor Processor communicate – more system
1 2 3 4
Significant Overheads: calls
Can we do better?
Process management with
system calls
3
Thread model
10000000 / 4 = 2500000 Create 1 process with 4 threads; each loop
does 1/4th of the work
Process Properties:

1 fork system call needed;


4 threads need to be
created --- much more
lighter.

Each thread is not isolated


Processor Processor Processor Processor from others
1 2 3 4

Management of threads
with fewer or no system
calls.
4
Threads
• Separate streams of execution
within a single process
• Threads in a process not isolated
from each other
• Each thread state (thread control
block) contains
– Registers (including EIP, ESP)
– stack

5
Why threads?
• Lightweight

Cost of creating 50,000 processes / threads


([Link]

• Efficient communication between entities


• Efficient context switching 6
Threads vs Processes
• A thread has no data • A process has code, heap,
segment or heap stack, other segments
• A thread cannot live on its • A process has at-least one
own. It needs to be thread.
attached to a process
• There can be more than • Threads within a process
one thread in a process. share the same code, files.
Each thread has its own
stack
• If a process dies, all threads
Based on Junfeng Yang’s lecture slides
• If[Link]
a thread dies, its stack is die. 7
pthread library
Thread identifier (TID) much like
• Create a thread in a process
Pointer to a function,
int pthread_create(pthread_t *thread,
which starts execution in a
const pthread_attr_t *attr, different thread
void *(*start_routine) (void *),
void *arg); Arguments to the function

Exit value of the thread

• Destroying a thread
void pthread_exit(void *retval);
[Link] 8
pthread library contd.
• Join : Wait for a specific thread to complete

int pthread_join(pthread_t thread, void **retval);


TID of the thread to wait for Exit status of the thread

what is the difference with wait()?


9
Example

Note. You need to link the


pthread library

$ gcc threads.c –lpthread


$ ./[Link]

10
Other thread libraries
• Windows threads
• Boost (in C++)
• LinuxThreads
• etc.

11
Who manages threads?
• Two strategies
– User threads
• Thread management done by user level thread
library. Kernel knows nothing about the threads.
– Kernel threads
• Threads directly supported by the kernel.
• Known as light weight processes.

12
User level threads
• Advantages:
– Fast (really lightweight)
(no system call to manage threads. The thread library
does everything).
– Can be implemented in an OS that does not support
threading.
– Switching is fast. No switch from user to protected
mode.

• Disadvantages:
– Scheduling can be an issue. (Consider, one thread
that is blocked on an IO and another runnable.)
– Lack of coordination between kernel and threads. (A
process with 1000 threads competes for a timeslice
with a process having just 1 thread.)
13
Kernel level threads
• Advantages:
– Scheduler can decide to give more time to a
process having large number of threads
than process having small number of
threads.
– Kernel-level threads are especially good for
applications that frequently block.

• Disadvantages:
– The kernel-level threads are slow (they involve kernel
invocations.)
– Overheads in the kernel. (Since kernel must manage and
schedule threads as well as processes. It require a full thread
control block (TCB) for each thread to maintain information about
14
Thread Models
• Many-to-one model

• one-to-one model

• Many-to-many model

15
Many-to-one model user thread
• Many user level threads map to a single
kernel thread

• Pros:
– Fast. No system calls to manage threads.
– No mode change for switching threads

• Cons:
– No parallel execution of threads. All kernel thread
threads block when one has a system call.
– Not suited for multi-processor systems.
16
One-to-one model user thread

• Each user thread associated with


one kernel thread.
• Pros.
– Better suited for multiprocessor
environments.
– When one thread blocks, the other
threads can continue to execute.
• Cons. kernel thread
– Expensive. Kernel is involved.

17
Many-to-Many model
• Many user threads mapped to many
kernel threads
– Supported by some unix and windows
versions
• Pros: flexible
– OS creates kernel threads as required
– Process creates user threads as needed
• Cons: Complex
– Double management

18
Threading issues
• What happens when a thread invokes fork?
– Duplicate all threads?
• Not easily done… other threads may be running or blocked in
a system call or a critical section
– Duplicate only the caller thread?
• More feasible.
• Segmentation fault in a thread. Should only the
thread terminate or the entire process?

19
Typical usage of threads
Event? event occurred

No event occurred
create
thread
Service
event

terminate
thread
Creating and terminating thread lead to overheads

20
Thread pools
Event? event occurred

No event occurred Thread pool

assign job to thread


from pool
service
event

Block
Number of threads in pool is critical! thread
21

You might also like