Top Operating Systems Interview Questions
- What is the difference between a kernal and shell?
- What is virtual memory and how does it work?
- What is the difference between a process and a thread?
- What is deadlock in operating system? How does we can prevent it?
- What is semaphore in operating system? How is it used in operating system?
- What is a context switch in an operating system?
- What is a page fault and how is it handled in an operating system?
- What is a scheduling algorithm in an operating system and what are the different types?
- What are LRU and LFU cache?
- What is Thrashing?
- What is Belady’s Anomaly?
- What is Banker's Algorithm?
- What is Memory Management Unit (MMU)?
- What is cache memory in an operating system?
- Difference between Paging and Segmentation.
1. What is the difference between a kernal and shell?
In an Operating System,
- The
kernelis the core component of the operating system, which is reponsible for various functionalities for the OS like process management, memory management, file management, etc. It is a bridge between the software and the hardware. - The
shellis a user interface, that we can use to communicate with the system using commands. For example, cmd in Windows, bash in Linux, etc.
2. What is virtual memory and how does it work?
Virtual memory is a memory management technique that allows the system to use the disk space in addition to the RAM. It enables the process to use more memory than physically availabe in RAM.
2.1 How does it work?
- When a process is running in virtual address space, it is divided into multiple pages,
- The pages that are not used frequently are moved to the disk (swap space),
- If a required page is not available in RAM a page fault occurs, and the os swaps the page from the dist to RAM,
- This way, the process can use more memory than physically available in RAM.
3. What is the difference between a process and a thread?
In an Operating System,
- A
processis a instance of a program that is being executed. It has its own memory space, resources, etc. - A
threadis a subset of a process. A process can have multiple thread. These thread shares the memory of the process. These are also called as lightweight processes.
4. What is deadlock in operating system? How does we can prevent it?
Deadlock is a situation where two or more processes are waiting for a some resources that never become available. This leads to a situation where none of a process can move further. Example: Process A is holding resource R1 and waiting for R2, while another Process B is waiting for R1 and holding R2.
Conditions for Deadlock:
- Mutual Exclusion,
- Hold and wait,
- No preemption,
- Circular wait.
How to prevent deadlock?
There are several ways to prevent deadlock,
- Deadlock Avoidance: Banker's Algoritm, Resource Allocation Graph.
- Banker's Algorithm: It checks before allocating the resource, if the system will remain in a safe state or not after allocating the resource.
- Resource Allocation Graph: It is a graph that shows relationship between the processes and resources in the system, if this graph has no cycle, then the system remains in safe state.
- Deadlock Detection and Recovery:
- Detection: Periodically checks for the deadlock in the system.
- Recovery: Once the deadlock is detected, the system can recover by killing the processes, or by preemting the resources from the processes.
5. What is semaphore in operating system? How is it used in operating system?
Semaphore is a synchronization mechanism used in operating system to control the access of shared memory by multiple processes or threads to prevent race conditions. It is a** integer variable** that can be accessed by different processes or threads, and it can be incremented or decremented atomically.
Operations on Semaphore:
- Wait: It decrements the semaphore value.
- Signal: It increments the semaphore value.
Types of Semaphores:
- Binary Semaphore: It can have only two values, 0 or 1. It is used for mutual exclusion.
- Counting Semaphore: It can have values greater than 1, and it is used for resource allocation the value of semaphore represents the number of resources available.
How semaphore works?
- Semaphore is intialized with value (1 for binary, N for counting),
- When a process wants to access the shared memory aka when it enters the critical section, it calls the wait operation,
- The wait operation checks the semaphore value wheather it is greater than 0 or not,
- If the value is greater than 0, then it decrements the semaphore value and allows the process or thread to the critical section,
- If the value is 0, then the process or thread is blocked until the semaphore value is greater than 0,
- Once the process or thread exits the critical section, it calls the signal operations,
- The signal operation increments the semaphore value.
How semaphore is used in operating system?
- Semaphore is used in os for process synchronization,
- It is used for multiprogramming, multitasking, etc.
- It is used for cuncurrency programming, etc..
6. What is a context switch in an operating system?
Context switching in os is the process of storing the process control block (PCB) of the currently running process and loading the process control block (PCB) of the next process to be executed. It is done by CPU scheduler. Context switching is essential for multitasking, and it is used to prevent the starvation of the processes.
7. What is a page fault and how is it handled in an operating system?
Page Fault is a situation when a process tries to access a page that is not currently availabe in the main memory but in the disk, then the os retrieves the page from the disk to the main memory.
How is it handled in an operating system?
- OS checks for the demanded page in the page table of the process,
- If the page is not available in the main memory, then it is a page fault,
- OS checks for the page in the disk, if it exists in the disk, if not process termiantes with an segmentaion fault error,
- If page existes and there are some free space availabe in the RAM it directly loads the page to the RAM, if RAM is full, then it swaps a page from the RAM with the demanded page with some algorithm like LRU, FIFO, etc.
8. What is a scheduling algorithm in an operating system and what are the different types?
Scheduling algorithm is the algorithm used by the CPU scheduler to schedule the processes in the system. It is used to decied which process is going to be executed next. The CPU scheduler decides which algorithm to use based on the system need's, such as maximizing the throughput, CPU utilization, etc.
-
Non-preemptive Scheduling Algorithms:
- First Come First Serve (FCFS): It schedules the process based on the arrival time of the process.
- Shortest Job First (SJF): It schedules the process based on the burst time (how much time process need for execution) of the process.
-
Preemptive Scheduling Algorithms:
- Shortest Remaining Time First (SRTF): It schedules the process based on the remaining burst time of the process.
- Round Robin (RR): It schedules the process based on the a fixed time slice known as time quantum, before moving to the next process.
- Priority Scheduling (PS): It schedules the process based on the priority of the process, the process with the highest priority first.
- Multilevel Queue Scheduling (MLQ): Processes are divied into multiple queues based on priority, it schedules the process based on the multiple queues, each queue has its own scheduling algorithm.
- Multilevel Feedback Queue (MFL): Similar to MLQ, but the process can move between the queues based on the priority dynamically.
9. What are LRU and LFU cache?
- LRU (Least Recently Used) cache is a cache replacement algorithm that replaces the least recently used cache block with the new cache block.
- LFU (Least Frequently Used) cache is a cache replacement algorithm that replaces the least frequently used cache block with the new cache block.
10. What is Thrashing?
Thrashing is a situation where the system spends more time in swapping pages between the disk and the main memory then executing the process. Tharahing occurs when the system has insufficient memory to run the program. Example, in a 4Gb RAM system, if the system is running 5Gb of programs, then the system spends more time in swapping the pages between the disk and the main memory.
11. What is Belady’s Anomaly?
Belady's Anomaly is a issue in the page replacement algorithm, where increasing the number of frames may increase page faults. Belady's Anomaly is observed in the FIFO page replacement algorithm,
- In FIFO, the page that is loaded first replace first, regardless of future needs,
- Adding more frames can cause usefull pages to be replaced,
- Increasing the number of page faults.
12. What is Banker's Algorithm?
Banker's Algorithm is a deadlock avoidance algorithm, it checks for safe state before allocating the resources to the processes.
How Banker's Algorithm works?
- Every process declares the maximum resources it needs,
- The system checks if the resources is allocated to the process, if the system remains in the safe state,
- If the system remains in the safe state, then the resources are allocated to the process, else the process is blocked until the resources are available.
13. What is Memory Management Unit (MMU)?
Memory Management Unit (MMU) is a hardware unit that is responsible to handle the memory request of processes, it translating the virtual address to the physical address. This is responsible for paging, segmentaion.
14. What is cache memory in an operating system?
Cache memory is a small memory unit that is used to store the frequently used data, so the the CPU don't have to access the main memory RAM every time. It is faster than the main memory.
Why cache memory is needed?
- Cache memory is much faster than the main memory RAM, so the CPU can access the data faster.
- This reduces the memory access time, and imporves the execution speed.
Types of Cache Memory:
- L1 Cache: It is the fastest cache memory, it is present in the CPU itself size is around 32KB to 128KB.
- L2 Cache: It is slower than L1 cache, but faster than the main memory, it is present in the CPU itself, size is around 256KB to 512KB.
- L3 Cache: It is slower than L2 cache, but faster than the main memory, it is present in the CPU itself, size is around 1MB to 8MB.
15. Difference between Paging and Segmentation.
- Paging is a memory management technique that divides the memory into fixed size pages, but caueses a potential of internal fragmentation.
- Segmentation is a memory management technique that divides the memory into variable size segments, but caueses a potential of external fragmentation.
16. What happens when we do fork()?
fork() is a system call that creates new child process from the current process, the child process is the exact copy of the parent process, and it executes from the next line of the fork().
- The child process (the newly created process) return 0, if the fork() is successful else it returns -1.
- The parent process return the process id of the child process.