0% found this document useful (0 votes)
23 views7 pages

OS Chapter 5 Simple Notes - 075257

Uploaded by

1415 lv54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

OS Chapter 5 Simple Notes - 075257

Uploaded by

1415 lv54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 5: Threads in Operating Systems

1. Process Characteristics
A process in an operating system is an instance of a program in execution. It has two main
characteristics:

a) Unit of Resource Ownership

 What it means: A process is allocated its own virtual address space (where its code,
data, and stack reside) and control over resources like files and I/O devices.
 Example: Think of a process as a container that holds a program's data, like the text file
you are working on in a word processor.

b) Unit of Dispatching (Execution)

 What it means: A process has an execution path. It can be broken down into several
threads (a smaller unit of execution), and the process can run concurrently with other
processes.
 Example: While one process is running, another can be waiting for its turn, or some tasks
of the process can run at different times.

2. Multithreading vs. Single-threading


a) Multithreading:

 What it means: In a multithreaded environment, multiple threads run within the same
process. Each thread is like a mini process that runs concurrently but shares the same
resources (memory, file handles) as other threads in the same process.
 Example: Web browsers use multithreading to load different parts of a webpage
simultaneously.

b) Single-threading:

 What it means: In a single-threaded environment, only one task runs at a time. No


threads are created within a process.
 Example: In older operating systems like MS-DOS, a single task was executed, and you
couldn’t perform multiple operations at once.
3. Relationship Between Threads and Processes
Types of Thread-to-Process Relationships:

1. 1:1 Model:
o What it means: Each thread is mapped to its own process. Each thread runs
independently, and the process provides resources to the threads.
o Example: Older versions of UNIX.
2. M:1 Model:
o What it means: Multiple threads are managed within a single process. The
threads share the resources of the process and can run concurrently within the
same address space.
o Example: Windows NT, Solaris.
3. 1:M Model:
o What it means: Threads can migrate across different processes or systems. A
thread may move between machines and address spaces.
o Example: Cloud-based systems like Ra.
4. M:M Model:
o What it means: A hybrid model where multiple threads are mapped to multiple
processes. Both user and kernel threads can be executed concurrently.
o Example: TRIX (a real-time OS).

4. Processes vs. Threads


a) Processes:

 Characteristics:
o Have a virtual address space (this is where the program code and data live).
o Have protected access to resources like files, processors, and other processes.

b) Threads:

 Characteristics:
o Have an execution state (whether they’re running, ready, or blocked).
o Have an execution stack and static storage for local variables.
o Share the process’s address space and resources with other threads of the same
process.
 Communication: Threads in the same process can communicate directly without
invoking the kernel (the core part of the OS that manages resources). This makes
communication faster and more efficient.
5. Thread Benefits over Processes
Threads offer several advantages over processes, including:

1. Faster Creation and Termination: Creating or terminating a thread is quicker than


creating or terminating a process.
2. Faster Context Switching: Switching between threads is faster than switching between
processes because threads share memory and resources.
3. Simpler Communication: Threads in the same process can easily share data and
resources, avoiding the need for more complex methods like inter-process
communication (IPC).

6. Examples of Thread Benefits


 File Server Example:
o Imagine a file server on a LAN. It needs to handle multiple requests quickly.
Instead of creating a new process for each request (which is slow), it can create a
new thread for each request. This is faster and more efficient.
 SMP (Symmetric Multiprocessing) Machines:
o On SMP machines with multiple processors, threads can run simultaneously on
different processors, making the process faster.

7. Synchronization Between Threads


Threads in the same process share resources, but this can lead to problems if two threads try to
access the same data at the same time. Synchronization is necessary to ensure that threads do
not conflict with each other.

Example of Inconsistent View:

Imagine two threads, T1 and T2, working with the same variables: A, B, and C.

 T1 calculates C = A + B.
 T2 modifies A and B by transferring some amount X.

If T1 computes C after T2 has updated A but before T2 updates B, T1 will get an incorrect
result. This is an example of an inconsistent view, and it can happen without proper
synchronization.
8. Thread States
Threads can be in one of three key states:

 Running: The thread is currently executing.


 Ready: The thread is ready to execute but is waiting for CPU time.
 Blocked: The thread cannot execute because it’s waiting for some external event (like an
I/O operation to complete).

Threads within the same process share the same address space, so if one thread is suspended,
the whole process gets suspended.

9. User-Level Threads (ULT)


In User-Level Threads (ULT):

 The kernel does not know about the threads. They are managed entirely by a user-level
library.
 Thread switching happens without entering kernel mode, making it faster.

Advantages:

 Fast switching of threads because the kernel is not involved.


 Can run on any OS as long as it has a thread library.

Inconveniences:

 When a system call (like accessing a file) blocks a thread, the whole process gets
blocked, including all threads.
 Threads cannot run on different processors simultaneously.

10. Kernel-Level Threads (KLT)


In Kernel-Level Threads (KLT):

 The kernel is responsible for managing threads, meaning it knows about each thread in
the system.
 Thread switching requires the kernel to be involved.
Advantages:

 Parallelism: Multiple threads can run on different processors.


 Blocking happens at the thread level, so one thread can block without affecting others.

Inconveniences:

 Thread switching is slower because the kernel is involved in the process, which requires
switching between user mode and kernel mode.

11. Combined ULT/KLT Approaches


Some operating systems, like Solaris, use a combined approach:

 Thread creation is done in user space (like in ULT).


 Scheduling and synchronization happen in user space.
 The kernel manages a limited number of threads, optimizing performance while
providing flexibility.

12. Multithreading Models


There are three types of multithreading models:

1. Many-to-One:
o Multiple user-level threads are mapped to a single kernel thread.
o Common in systems that do not support kernel threads.
2. One-to-One:
o Each user-level thread corresponds to a kernel thread.
o Examples: Windows NT, OS/2.
3. Many-to-Many:
o Multiple user-level threads are mapped to multiple kernel threads.
o Allows the OS to create as many kernel threads as needed.
o Examples: Solaris, Windows NT/2000 with the ThreadFiber package.

In conclusion, threads allow for more efficient multitasking and resource usage compared to
processes, and different systems and models are used to manage these threads, depending on the
OS capabilities and requirements.
Process and Threads:

You might also like