Threads in OS: Unit 2-Chapter 1
Threads in OS: Unit 2-Chapter 1
Unit 2- Chapter 1
Threads in OS
• Definition: Each process has a single thread of • Definition: OS supports multiple processes, but
execution. each process has only one thread of execution.
• Example: MS-DOS • Example: Traditional UNIX systems
• Explanation: • Explanation:
• single application runs at a time • multiple processes run concurrently.
• each application runs as a single-threaded • Each process is independent and has its own
process. address space and resources.
• There is no support for concurrent execution • each process contains only one thread,
within the application. meaning tasks within the process execute
sequentially.
Multithreading
• Definition: A single process contains multiple • Definition: OS supports multiple processes, each
threads that execute concurrently. of which can contain multiple threads.
• Example: Java Run-time Environment • Example: Modern OS- Windows, Linux, and
• Explanation: Solaris
• single Java process can create multiple • Explanation:
threads. • running multiple processes at the same time.
• a Java program can have a main thread and • Each application can create multiple threads.
several other threads performing tasks like • web server can have multiple processes (one
handling user input, performing computations, for each service or application)
and managing background tasks • and each process can have multiple threads
simultaneously. handling different client requests concurrently.
Key benefits of threads
If process is
Suspend is process level swapped out, all threads
are also swapped out.
Thread States- Operations associated with
thread state
Spawn Block Unblock Finish
• When a new process • When a thread • When the event for • When a thread
is spawned, initial needs to wait for an which a thread is completes, its
thread for that event, it will enter in blocked occurs, the register context and
process is also block state. thread is moved to stacks are
spawned • The processor may the Ready queue deallocated
• a thread within a now turn to the
process may spawn execution of another
another thread ready thread in the
within the same same or a different
process, providing an process.
instruction pointer
and arguments for
the new thread
Windows Thread states
Types of Thread
threads library
Implementation of User Level Threads
By default, an application begins with a single thread.
This application and its thread are allocated to a single process managed by the kernel.
The application may spawn a new thread to run within the same process which is done by
invoking the spawn utility in the threads library.
The threads library creates a data structure for the new thread and then passes control to one of
the threads within this process that is in the Ready state, using some scheduling algorithm.
Operations such as thread creation, context switching, and scheduling are handled by the
threads library without involving the kernel.
Advantages of User level Thread
Efficiency
• Switching between user-level threads does not require transitioning to kernel mode.
• This eliminates the overhead associated with mode switching between user mode and
kernel mode.
• As a result, thread switching is faster and more efficient since it avoids the two-mode
switches (user-to-kernel and kernel-to-user)
Compatibility:
• ULTs can run on any operating system as they do not require any special support from the
kernel.
• The threads library, which provides the ULT functionality, is implemented as a set of
application-level functions. This library is shared across applications, enabling thread
management without modifying the underlying OS kernel.
Disadvantages of User Level Thread
Single Processor Utilization:
Processor Utilization:
• Applications create • Each thread has its own • When the kernel switches
threads through system TCB between threads, it saves
calls provided by the • Contains information such the current thread's state
operating system's API. as thread ID, program (registers, program
counter, stack pointer, counter, etc.) into its TCB
register values, and
and loads the context of
scheduling information.
the next thread to be
• The kernel manages these
TCBs to track the state and executed.
execution context of each • This ensures that threads
thread. can run concurrently and
efficiently utilize processor
resources.
Advantages of Kernel Level Thread
True Parallelism
• KLTs allow multiple threads from the same process to run simultaneously on different
processors. This means tasks can be executed in parallel, speeding up performance.
Improved Responsiveness:
• If one thread in a process is waiting (blocked), the kernel can switch to another thread in
the same process that is ready to run. This keeps applications responsive and efficient.
• Switching between threads managed by the kernel requires saving and restoring thread contexts, which adds
processing time and can reduce overall performance.
Complexity:
• KLT management is more intricate than ULT. It involves careful synchronization and resource management to
prevent issues like deadlock
Flexibility:
• Applications that rely solely on KLTs may have fewer options for customizing thread management compared to
user-level thread libraries. This limits how threads can be controlled and optimized for specific tasks.
OS Dependency:
• KLTs heavily rely on specific features and APIs provided by the operating system. This reliance can restrict
portability, meaning applications may need adjustments to run on different operating systems that have different
thread management capabilities.
Combined Approach
Combined Approach
Summary
Combined Approach -
User-Level Thread -
Kernel-Level Thread - OS Thread created in user
Application managed,
managed, TCB space, mapped to kernel
thread library used to
maintained by kernel thread, implemented by
manage read
few OS
Concurrency
Mutual Exclusion and Synchronization
• Concurrency- Executing
Multiple processes simultaneously
• Shared resources refer to any hardware or
software components that are accessed and used
by multiple processes or threads concurrently.
Concurrency and o Memory- common portion of a memory,
Shared Resources common global variables
o Files- read/write from same file
o I/O Devices- printers, disks, keyboard, monitor,
etc
o CPU
Challenges in Concurrency
• Software Solution
o Spinlocks
o Semaphore
o Mutex