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 specific critical topics. One such vital topic is Threads in OS.
This blog will discuss everything you need to know to get started with multitasking in OS.
There is no concept of multiple users, administrators, or root accounts.
There is no protection between the OS and user programs.
MS-DOS is an example of a single-tasking OS.
Drawbacks:
CPU is not utilized efficiently, as only one task will run at a time.
Most of the time, memory will be wasted.
Multitasking OS
OS allows more than one program to be running in concurrency.
Multitasking is achieved by time-sharing, dividing available processor time between multiple processors.
OS gives the illusion that multiple processes are running at the same time. But in reality, the CPU keeps switching between different processes, and as this switching occurs very fast, such an illusion is created.
All modern OS can handle a large number of processes simultaneously.
Multitasking OS yields better CPU utilization than single-tasking OS.
Example
Multitasking
In the above screenshot, we can see four tabs open in the Google Chrome browser. Microsoft Word and Visual Studio Code are also running at the same time.
Multitasking can be categorized into the following two types,
Cooperative multitasking
Preemptive multitasking
In cooperative multitasking, if a task/process is not ready for execution, it will voluntarily give up the control of the processor so that other tasks can run. It is also known as non-preemptive multitasking. Cooperative multitasking is still used in RISC OS systems.
In the case of preemptive multitasking, if a high-priority task becomes ready for execution, it can immediately acquire the processor by preempting the execution of a lower-priority task.
It is a core feature of all Unix-like operating systems like Linux, and Solaris.
Process and Thread
A process is the instance of a program executed by one or many threads.
On the other hand, a thread is a basic CPU utilization unit. It has its own thread ID, a program counter, a register, and a stack.
The above diagram shows a process with two threads. Check this blog here to learn more about processes and threads.
Process-based and Thread-based Multitasking
Multitasking can further be divided into two types,
Process-based Multitasking
Thread-based Multitasking
Process-based Multitasking
In this type of multitasking, two or more processes can be run concurrently.
Features:
The smallest unit of execution is a process.
Inter-process communication is costly and inefficient.
Context-switching time is more in the case of processes.
It is unable to gain access over CPU idle time.
Every program has its own address space.
It requires more overhead than thread-based multitasking.
Example:
A browser and a video player are two processes that can be running at the same time on a modern computer.
Thread-based Multitasking
In this type of multitasking, two or more threads can be run concurrently.
Features:
The smallest unit of execution is a thread.
Inter-thread communication is inexpensive and efficient.
Context-switching time is less in the case of threads.
It can gain access over CPU idle time.
Threads share the same address space.
It requires less overhead than process-based multitasking.
Example:
A word processor can simultaneously have a thread for spelling and grammar checks and another thread for responding to keystrokes from the user.
Differences between Process-based Vs Thread-based Multitasking
Frequently Asked Questions.
What is preemption?
In computing, preemption is the act of preempting/stopping/pausing an ongoing process to execute a different process of higher priority.
What is a single-user operating system?
A single-user operating system is an operating system that allows only one user to access the computer at a time. The types of single-user operating systems are,
Single-user Single-tasking
Single-user Multi-tasking
Examples of single-user operating systems are MS-DOS, Windows 95, Windows NT etc.
What is a multi-user operating system?
A multi-user operating system is an operating system that allows two or more users to access the computer at a time.
Examples of multi-user operating systems are mainframes like IBM AS400, Linux, and Unix Distributed OS.
Name some scheduling algorithms that are based on preemptive scheduling.
Scheduling algorithms in OS define how much CPU time can be allotted for a particular process. In the case of preemptive scheduling, the processor can be preempted at any time to execute a different process while it is already running a process. Some scheduling algorithms based on preemptive scheduling are
Round Robin (RR)
Priority (Preemptive version)
Shortest Remaining Time First (SRTF)
Conclusion
In this blog, we discussed multitasking in Operating systems. We also talked about the types of multitasking - process-based and thread-based multitasking.
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 out our amazing course on Operating systems here, designed specifically for you!