Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
OS(Operating System) is one the most asked topic in interviews. So if you are preparing for interviews, it’s essential to be clear with certain critical topics. One such important topic is Threads in OS.
This blog will discuss everything you need to know to get started with threads.
‘A thread is a basic unit of CPU utilization, which consists of its thread ID, a program counter, a register and a stack.’
A process may contain multiple threads, which is referred to as a ‘multi-threaded’ process. These multiple threads share the code, data, files, and signals.
However, a traditional process has a single thread of control and is known as a ‘single-threaded’ process.
On the left, we have a single-threaded processor, and on the right, we have a multi-threaded process.
Examples
All modern applications are multi-threaded.
For instance, a word processor software might have multiple threads running in the background.
The word processor may have,
a thread for displaying the GUI
a thread for responding to keystrokes from the user
Thread libraries provide an API (Application Programming Interface) to the programmer for creating and managing the threads. There are two methods to implement a thread library,
User-level library
Kernel-level library
User-level library
This method implements a user-level library supported by users with no kernel support.
In this method, all the code and data structures for the threads exist in the user space.
Whenever a function is invoked in the library, it results in a local systemin the user space.
Kernel-level library
This method implements a kernel-level library supported directly by the kernel or operating system.
In this method, all the code and data structures for the threads exist in the kernel space.
Invoking a function in the library causes a system to the kernel.
POSIX Pthreads: Pthreads is the threads extension of the POSIX standard. It may be provided as a user-level or kernel-level library. Pthreads are often used by UNIX and Linux systems.
Win32: Win32 is a kernel-level thread library available on Windows systems.
Java: Java thread API allows the creation and management of threads directly in Java programs. It is implemented using a thread library available on the host system.
A process is the instance of a program executed by one or many threads.
A process may contain multiple threads, which is called a multi-threaded process. A traditional process consists of a single thread and is known as a single-threaded process.
What are the different states of a process?
The state of a process refers to the current activity of the process. The different states of a process are
New: Process is created.
Running: Instructions of the process are executed by a processor.
Waiting: Process is waiting for some event (I/O, signal).
Ready: Process is waiting to be assigned to a processor.
Terminated: Process is terminated after execution is finished.
What is context switching?
Context switching is a technique the operating system uses to switch from one process or task to another. The current state or context of a process or thread is saved so that it can be restored, and the execution can be resumed later.
What is a kernel?
The kernel is the core part of an operating system. It is a computer program that has complete control over everything in the system. It manages the operations of hardware and software.
Conclusion
In this blog, we discussed threads in Operating systems. We talked about the fundamental differences between processes and threads, the types of threads, and thread libraries.
If you are preparing for interviews, don’t forget to study Operating Systems - as it is one of the most asked topics in interviews. You should check our amazing course on Operating System here, designed specifically for you!