• Thread library provides programmer with API for creating and managing threads
• Two primary ways of implementing
- Library entirely in user space
- Kernel-level library supported by the OS
Thread Library
Labels: OS 5
Multithreading Methods
Multithreading Methods
• Many-to-One
• One-to-One
• Many-to-Many
MANY-TO-ONE
• Many user-level threads mapped to single kernel thread
• Examples:
- Solaris Green Threads
- GNU Portable Threads
----------
ONE-TO-ONE
• Each user-level thread maps to kernel thread
• Examples
- Windows NT/XP/2000
- Linux
- Solaris 9 and later
----------
MANY-TO-MANY
• Allows many user level threads to be mapped to many kernel threads
• Allows the operating system to create a sufficient number of kernel threads
• Solaris prior to version 9
• Windows NT/2000 with the ThreadFiber package
Labels: OS 5
Kernel Thread
• Supported by the Kernel
• OS manages threads
- Slower to create and manage because of system calls
- A blocking system call will not cause the entire process to block.
- The kernel can schedule threads on different CPUs.
Labels: OS 5
User Thread
• Thread management done by user-level threads library
• A blocking system call will cause the entire process to block
- OS is unaware of threads
• The kernel cannot schedule threads on different CPUs.
• Example: Pthread, a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.
Labels: OS 5
Benefits of Multi-Threaded Programming
• Responsiveness
- User interaction in parallel with data retrieval
• Utilization of MP Architectures
• Resource Sharing between Threads (vs. Process)
E.g. Synchronization by accessing shared data
• Economy (vs. Process)
- If the task is the same, why not share the code?
- In Solaris 2, creating a process is about 30 times slower than threads. Context switch threads is about 5 times slower.
Labels: OS 5
Thread
• A piece of code that run in concurrent with other threads.
• Each thread is a statically ordered sequence of instructions.
• Threads are being extensively used express concurrency on both single and multiprocessors machines.
• Programming a task having multiple threads of control – Multithreading or Multithreaded Programming.
•

Labels: OS 5
Interprocess Communication
DIRECT COMMUNICATION
• 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.
– The link may be unidirectional (e.g. signaling), but is
usually bi-directional (e.g. sockets).
----------
INDIRECT COMMUNICATION
• Messages are directed and received from
mailboxes (also referred to as ports).
– Each mailbox has a unique id. (e.g. shared memory,
shared file, message Q)
– Processes can communicate only if they share a
mailbox.
• Properties of communication link
– A link may be associated with many processes.
– Each pair of processes may share several
communication links.
– Link may be unidirectional or bi-directional.
• Operations
– create a new mailbox
– send and receive messages through mailbox
– destroy a mailbox
• Primitives are defined as:
send(A, message) – send a message to
mailbox A
receive(A, message) – receive a message
from mailbox A
----------
SYNCHRONIZATION
• Blocking send
- suspend sending process until message received
• Nonblocking send
- resume process immediately after sending message
• Blocking receive
- suspend receiving process until data is received
• Nonblocking receive
- return either a message or
- null if no message is immediately available
Synchronization Trade-Offs
• Blocking
- guarantees message has been delivered
- drastically reduces performance
• Non-Blocking
- much better performance (hides latency of message sending)
- could cause errors if messages are lost
----------
BUFFERING
• Buffering allows messages to be saved and read or transmitted later
• Requires sufficient memory to store messages
• Can drastically improve performance of applications
Types of Buffering
• Zero Capacity
- no buffering at all
- must be "listening" when a message comes in
• Bounded Capacity
- some max, n, of messages will be buffered
- be careful when queue gets full
• Unbounded Capacity
- no limit to the number of message
- not usually very realistic assumption
- this is not very realistic, buffers usually have finite capacity

----------
Producer-Consumer Example
• One process generates data – the producer
• The other process uses it – the consumer
• If directly connected – time coordination
- How would they coordinate the time ??

• One process generates data – the producer
• The other process uses it – the consumer
• If not directly connected – have a buffer
- Buffer must be accessible to both
- Finite Capacity N – Number in use - K


Labels: OS 4