Network Programming
BITS Pilani Instructor Vinayak Naik
K K Birla Goa Campus Slides courtesy Mahadev Gawas
BITS Pilani
K K Birla Goa Campus
Introduction
Introduction
❖General-purpose languages such as Java, C#, Visual C++
.NET, Visual Basic .NET and Python have made concurrency
primitives available to applications programmer
❖Multithreading
❖Programmer specifies applications contain threads of execution
❖Each thread designate a portion of a program that may execute concurrently with other
threads
BITS Pilani, K K Birla Goa Campus
Definition of Thread
BITS Pilani, K K Birla Goa Campus
Motivation for Threads
❖Threads have become prominent due to trends in
❖Software design
❖More naturally expresses inherently parallel tasks
❖Performance
❖Scales better to multiprocessor systems
❖Cooperation
❖Shared address space incurs less overhead than
IPC
BITS Pilani, K K Birla Goa Campus
Motivation for Threads
❖Each thread transitions among a series of discrete thread states
❖Threads and processes have many operations in common (e.g. create,
exit, resume, and suspend)
❖Thread creation does not require operating system to initialize
resources that are shared between parent processes and its threads
❖Reduces overhead of thread creation and termination compared to process creation and
termination
BITS Pilani, K K Birla Goa Campus
Why do we need threads?
▪ To enhance parallel processing
▪ To increase response to the user
▪ To utilize the idle time of the CPU
▪ Prioritize your work depending on the priority
BITS Pilani, K K Birla Goa Campus
Example
▪ Consider a simple web server
▪ The web server listens for request and serves it
▪ If the web server was not multithreaded, the requests
processing would be in a queue, thus increasing the response
time and also might hang the server if there was a bad request.
▪ By implementing in a multithreaded environment, the web
server can serve multiple request simultaneously thus
improving response time
BITS Pilani, K K Birla Goa Campus
Thread States: Life Cycle of a
Thread
BITS Pilani, K K Birla Goa Campus
Multithreading vs. Single threading
▪ Multithreading: The OS supports multiple threads of execution
within a single process
▪ Single threading: The OS does not recognize the separate
concept of thread
▪ MS-DOS supports a single user process and a single thread
▪ Traditional UNIX supports multiple user processes but only one thread per process
10
BITS Pilani, K K Birla Goa Campus
Threads and Processes
Single Threading Multi-Threading
11
BITS Pilani, K K Birla Goa Campus
In a Multithreaded Environment, Processes
Have:
• A virtual address space which holds the process image
• Protected access to processors, other processes (inter-process
communication), files, and other I/O resources
12
BITS Pilani, K K Birla Goa Campus
While Threads...
▪ Have execution state (running, ready, etc.)
▪ Save thread context (e.g. program counter) when not running
▪ Have private storage for local variables and execution stack
▪ Have shared access to the address space and resources (files
etc.) of their process
▪ when one thread alters (non-private) data, all other threads
(of the process) can see this
▪ threads communicate via shared variables
▪ a file opened by one thread is available to others
13
BITS Pilani, K K Birla Goa Campus
Single Threaded and Multithreaded
Process Models
Thread Control Block contains a register image,
thread priority and thread state 14
information
BITS Pilani, K K Birla Goa Campus
Benefits of Threads
Less time to Threads enhance
terminate a efficiency in
thread than a Switching between communication
Takes less process between programs
time to create two threads takes
a new thread less time than
than a switching between
process processes
Benefits of Threads vs Processes
▪ Far less time to create a new thread than a new process
▪ Less time to terminate a thread than a process
▪ Less time to switch between two threads within the same
process than to switch between processes
▪ Threads can communicate via shared memory
▪ processes have to rely on kernel services for IPC
16
BITS Pilani, K K Birla Goa Campus
Application benefits of threads
• Consider an application that consists of several independent
parts that do not need to run in sequence
• Each part can be implemented as a thread
• Whenever one thread is blocked waiting for I/O, execution
could switch to another thread of the same application (instead
of switching to another process)
17
BITS Pilani, K K Birla Goa Campus
Benefits of Threads
▪ Example 1: File Server on a LAN
▪ Needs to handle many file requests over a short period
▪ Threads can be created (and later destroyed) for each request
▪ If multiple processors: different threads could execute simultaneously on
different processors
▪ Example 2: Spreadsheet on a single processor
machine:
▪ One thread displays menu and reads user input while the other executes the
commands and updates display
18
BITS Pilani, K K Birla Goa Campus
Types of Threads
User Level
Thread (ULT)
Kernel level Thread
(KLT)
NOTE: we are talking about threads for user
processes. Both ULT & KLT execute in user
mode. An OS may also have threads but that
is not what we are discussing here.
User-Level Threads (ULTs)
• Thread management
is done by the
application
• The kernel is not
aware of the
existence of threads
• Not the kind we’ve
discussed so far.
User-level Threads
❖User-level threads perform threading operations in user space
❖Threads are created by runtime libraries that cannot execute privileged instructions or
access kernel primitives directly
❖User-level thread implementation
❖Many-to-one thread mappings
❖Operating system maps all threads in a multithreaded process to single execution context
❖Advantages
❖ User-level libraries can schedule its threads to optimize performance
❖ Synchronization performed outside kernel, avoids context switches
❖ More portable
❖Disadvantage
❖ Kernel views a multithreaded process as a single thread of control
❖ Can lead to suboptimal performance if a thread issues I/O
❖ Cannot be scheduled on multiple processors at once
BITS Pilani, K K Birla Goa Campus
User-level Threads
BITS Pilani, K K Birla Goa Campus
Advantages of ULTs
ULTs can
Scheduling can be run on
application specific any OS
Thread switching does not
require kernel mode privileges
(no mode switches)
Disadvantages of ULTs
• In a typical OS many system calls are blocking
▪ as a result, when a ULT executes a system
call, not only is that thread blocked, but all
of the threads within the process are
blocked
• In a pure ULT strategy, a multithreaded
application cannot take advantage of
multiprocessing
Kernel-level Threads
❖ Kernel-level threads attempt to address the limitations of user-
level threads by mapping each thread to its own execution
context
❖ Kernel-level threads provide a one-to-one thread mapping
❖Advantages: Increased scalability, interactivity, and
throughput
❖Disadvantages: Overhead due to context switching and
reduced portability due to OS-specific APIs
❖ Kernel-level threads are not always the optimal solution for
multithreaded applications
BITS Pilani, K K Birla Goa Campus
Kernel-level Threads
BITS Pilani, K K Birla Goa Campus
Combining User- and Kernel-level
Threads
❖The combination of user- and kernel-level thread implementation
❖Many-to-many thread mapping (m-to-n thread mapping)
❖Number of user and kernel threads need not be equal
❖Can reduce overhead compared to one-to-one thread mappings by implementing thread pooling
❖Worker threads
❖Persistent kernel threads that occupy the thread pool
❖Improves performance in environments where threads are frequently created and
destroyed
❖Each new thread is executed by a worker thread
❖Scheduler activation
❖Technique that enables user-level library to schedule its threads
❖Occurs when the operating system calls a user-level threading library that determines
if any of its threads need rescheduling
BITS Pilani, K K Birla Goa Campus
Combining User- and Kernel-level
Threads
BITS Pilani, K K Birla Goa Campus
Advantages of KLTs
• The kernel can simultaneously schedule multiple
threads from the same process on multiple
processors
• If one thread in a process is blocked, the kernel can
schedule another thread of the same process