0% found this document useful (0 votes)
4 views

Chapter 2 Extra

Chapter 2 discusses context switching and threads in operating systems. Context switching allows the CPU to switch between processes while saving their states, enabling multitasking. It also covers the concept of threads, their benefits, types (user-level and kernel-level), and the advantages and disadvantages of each type.
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)
4 views

Chapter 2 Extra

Chapter 2 discusses context switching and threads in operating systems. Context switching allows the CPU to switch between processes while saving their states, enabling multitasking. It also covers the concept of threads, their benefits, types (user-level and kernel-level), and the advantages and disadvantages of each type.
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/ 4

Chapter 2

 Context Switching

The Context switching is a technique or method used by the operating system to


switch a process from one state to another to execute its function using CPUs in the
system. When switching performs in the system, it stores the old running process's
status in the form of registers and assigns the CPU to a new process to execute its
tasks. While a new process is running in the system, the previous process must
wait in a ready queue. The execution of the old process starts at that point where
another process stopped it. It defines the characteristics of a multitasking operating
system in which multiple processes shared the same CPU to perform multiple tasks
without the need for additional processors in the system.

Example : Suppose that multiple processes are stored in a Process Control Block
(PCB). One process is running state to execute its task with the use of CPUs. As
the process is running, another process arrives in the ready queue, which has a high
priority of completing its task using CPU. Here we used context switching that
switches the current process with the new process requiring the CPU to finish its
tasks. While switching the process, a context switch saves the status of the old
process in registers. When the process reloads into the CPU, it starts the execution
of the process when the new process stops the old process. If we do not save the
state of the process, we have to start its execution at the initial level. In this way,
context switching helps the operating system to switch between the processes,
store or reload the process when it requires executing its tasks.

 Threads

A thread is a single sequential flow of execution of tasks of a process so it is also


known as thread of execution or thread of control. There is a way of thread
execution inside the process of any operating system. Apart from this, there can be
more than one thread inside a process. Each thread of the same process makes use
of a separate program counter [The program counter (PC) is a register that
manages the memory address of the instruction to be executed next ] and a stack of
activation records and control blocks. Thread is often referred to as a lightweight
process.

The process can be split down into so many threads. For example, in a browser,
many tabs can be viewed as threads. MS Word uses many threads - formatting text
from one thread, processing input from another thread, etc.
o Concept of multithreads

Multithreading allows the application to divide its task into individual


threads. In multi-threads, the same process or task can be done by the
number of threads, or we can say that there is more than one thread to
perform the task in multithreading. With the use of multithreading,
multitasking can be achieved.

The main drawback of single threading systems is that only one task can
be performed at a time, so to overcome the drawback of this single
threading, there is multithreading that allows multiple tasks to be
performed.

o Benefits of threads

 Enhanced throughput of the system: When the process is split into


many threads, and each thread is treated as a job, the number of jobs done
in the unit time increases. That is why the throughput of the system also
increases.
 Effective Utilization of Multiprocessor system: When you have more
than one thread in one process, you can schedule more than one thread in
more than one processor.
 Faster context switch: The context switching period between threads is
less than the process context switching. The process context switch
means more overhead for the CPU.
 Responsiveness: When the process is split into several threads, and when
a thread completes its execution, that process can be responded to as soon
as possible.
 Communication: Multiple-thread communication is simple because the
threads share the same address space, while in process, we adopt just a
few exclusive communication strategies for communication between two
processes.
 Resource sharing: Resources can be shared between all threads within a
process, such as code, data, and files. Note: The stack and register cannot
be shared between threads. There is a stack and register for each thread.

o Types of threads

In the operating system, there are two types of threads.


1. Kernel level thread.
2. User-level thread.

 User-level thread

The operating system does not recognize the user-level thread. User
threads can be easily implemented and it is implemented by the user. If a
user performs a user-level thread blocking operation, the whole process is
blocked. The kernel level thread does not know nothing about the user level
thread. The kernel-level thread manages user-level threads as if they are
single-threaded processes. examples: Java thread, etc.

Advantages of User-level threads

1. The user threads can be easily implemented than the kernel thread.
2. User-level threads can be applied to such types of operating systems that
do not support threads at the kernel-level.
3. It is faster and efficient.
4. Context switch time is shorter than the kernel-level threads.
5. It does not require modifications of the operating system.
6. User-level threads representation is very simple. The register, PC, stack,
and mini thread control blocks are stored in the address space of the user-
level process.
7. It is simple to create, switch, and synchronize threads without the
intervention of the process.

Disadvantages of User-level threads

1. User-level threads lack coordination between the thread and the kernel.
2. If a thread causes a page fault, the entire process is blocked.

 Kernel level thread

The kernel thread recognizes the operating system. There is a thread


control block and process control block in the system for each thread and
process in the kernel-level thread. The kernel-level thread is implemented by
the operating system. The kernel knows about all the threads and manages
them. The kernel-level thread offers a system call to create and manage the
threads from user-space. The implementation of kernel threads is more
difficult than the user thread. Context switch time is longer in the kernel
thread. Example: Window Solaris.
Advantages of Kernel-level threads

1. The kernel-level thread is fully aware of all threads.


2. The scheduler may decide to spend more CPU time in the process of
threads being large numerical.
3. The kernel-level thread is good for those applications that block the
frequency.

Disadvantages of Kernel-level threads

1. The kernel thread manages and schedules all threads.


2. The implementation of kernel threads is difficult than the user thread.
3. The kernel-level thread is slower than user-level threads.

You might also like