Advanced Operating Systems Interview Questions - Process Scheduling, Deadlocks, Memory Management, and More
What is a deadlock?
Deadlock is basically unavailability of sufficient resources to concurrently run the processes. They normally occur because of Mutual Exclusion(its basically just a way of concurreny control that prevents race conditions in Operating Systems) , Premption(giving an external the resources of the ongoing process) ,Hold and Wait(where a process holds a resource and still waits for other resources to finish its job.) & Circular Wait(Two or resources wait for resources each have in a cyclic order)
Well, Deadlock Prevention is usually frowned upon though , due to its higher order of computation
Well, there are a few ways Deadlock Avoidance is possible - Banker's Algorithm
Deadlock Prevention is done by elimination of any one of the causations of the deadlock.
Deadlock Negligence - Treating deadlock is highly expensive on the resources , So in most cases it is neglected and the process is killed ( more like the times when we kill a task using task manager while our browsers show high latency )
Its a way to concurrently run multiple processes that don't affect each others (in terms of resource allocation and writes)
Semaphores are basically IPC mechanisms that ensure synchronisation of processes. Using semaphores is just one way of attaining sync.
There are basically 2 types Mutex-lock(binary) and counting semaphores(can range depending on the number of processes).
A race condition is a situation that occurs when two or more threads or processes attempt to perform operations on shared data at the same time1. Because the thread scheduling algorithm can swap between threads at any time, you don’t know the order in which the threads will attempt to access the shared data1. This can result in unpredictable behavior and undesirable outcomes.
To prevent race conditions from occurring, you would typically put a lock around the shared data to ensure only one thread can access the data at a time.
These are basially variables(entities) in programs that prevent multiple processes from accessing a shared resource at a time.
std::mutex mtx; // mutex for critical section
void print_block(int n, char c) {
// critical section (exclusive access to std::cout signaled by locking mtx):
mtx.lock();
for (int i = 0; i < n; ++i) { std::cout << c; }
std::cout << '\n';
mtx.unlock();
}
int main() {
std::thread th1(print_block, 50, '*');
std::thread th2(print_block, 50, '$');
th1.join();
th2.join();
return 0;
}
void print_block(int n, char c) { // critical section (exclusive access to std::cout signaled by locking mtx): mtx.lock(); for (int i = 0; i < n; ++i) { std::cout << c; } std::cout << '\n'; mtx.unlock(); }
int main() { std::thread th1(print_block, 50, '*'); std::thread th2(print_block, 50, '$');
th1.join();
th2.join();
return 0;
}
Difference between thread and process?
A process is an instance of a program that is being executed. It contains the program code and its current activity. Each process has its own memory space and system resources.
A thread, on the other hand, is a lightweight unit of execution within a process. Threads share the same memory space and system resources as their parent process.
Some notable differences are :
So basically an Orphan process is seen when the Parent process has finished execution and has terminated. In these cases , the system process adopts it and becomes its parent until it's termination.orphan processes don't inherently cause harm to the system, it's wise to terminate the orphan child, if it consumes more resources.
What is a Zombie Process?
A zombie process is basically a defunct process that has made has finished the execution and has made the exit() system call, but still has an entry in the process table.
Normally the entry is maintained so that the parent can read the child's exit status.
In normal conditions the parent calls wait on the child and then it checks the exit status terminates the zombie, whereas, in rare cases if the parent fails to call wait()/waitPid() on the zombie, then it can cause resource leaks(basically holding on to the allocated resource forever and failing to release them), which is fatal to concurrent systems.
Zombie processes can have adverse effects on the system if too many are generated. The process table has a finite size, and if it becomes full of zombie processes, the system will not be able to generate any new processes, causing it to come to a standstill . This typically indicates a bug in the parent program or an uncommon decision not to reap children .
They can be detected by using the ps command and a zombie process will be having a "Z" in the STAT column.Inorder to prevent Zombies , we better create APIs that do wait on their children
What is the role of the kernel in an OS?
What is the difference between paging segmentation?
What is thrashing?
What is virtual memory?
What is aging?
What is context switching?
Explain process synchronization?
Mention any four scheduling algorithms and which is the best scheduling algorithm?
What is spooling?
What is the use of the trap bit?
What is a bootstrap program?
Can a system run without an OS?
What sockets do hackers use?
What are the types of semaphores?
What is compaction?
How can you increase the priority of a process?
What is disk scheduling?
What is Belady's anomaly?
What is C-SCAN?
What is interprocess communication?
What are real-time systems?
What is timestamp?
What are the different cache mapping techniques?
What is response and turnaround time?
What are the different file accessing methods?
What is memory management, and how is the OS responsible for it?
Mention any two deadlock prevention algorithms.
What is system software?
What is a critical section in the context of process synchronization?
What is a deadlock avoidance algorithm, and how does it work?
What is the difference between preemptive and non-preemptive scheduling?
Explain the concept of paging in virtual memory. What is the purpose of a file system, and how does it work?
What is a system call, and how is it different from a regular function call?
What is a thread pool, and how does it improve performance in multi-threaded applications?
Explain the concept of demand paging and how it improves the efficiency of virtual memory.
What is a cache hit and a cache miss, and how do they affect system performance?
What is the role of the memory manager in an operating system?
What is the purpose of the process scheduler in an operating system, and how does it work?
Explain the concept of a deadlock detection algorithm, and how is it used in practice?
What is the difference between a kernel thread and a user-level thread?
What is the difference between a semaphore and a mutex, and when would you use each one?
How does virtual memory protect processes from interfering with each other?
What is a context switch, and why is it an important operation in an operating system?
How do you measure the performance of an operating system, and what metrics are used?
What is a device driver, and what is its role in an operating system?
How does the operating system manage input/output operations, and what challenges does it face?
What is a trap, and how is it used in operating system design?