Explain the difference between processes and threads.
Process: An independent program in execution. Each process has its own memory space.
Thread: A lightweight process that can run concurrently with other threads. Threads of the same process share the same memory space.
Context switching between threads is faster than between processes.
What is deadlock? Describe strategies to prevent, avoid, and detect it.
Deadlock is a situation where two or more processes are blocked forever, waiting for each other to release resources.
Strategies:
- Prevention: Ensure that at least one of the four necessary conditions for deadlock (mutual exclusion, hold and wait, no preemption, circular wait) cannot hold.
- Avoidance: Use algorithms like the Banker's Algorithm to ensure the system never enters an unsafe state.
- Detection & Recovery: Allow deadlocks to occur, detect them, and then take action to recover, such as terminating a process or preempting resources.
How does virtual memory work, and what role does paging play?
Virtual Memory is a memory management technique that provides an "idealized" abstraction of the storage resources that are actually available on a given machine. It creates the illusion to users of a very large (main) memory.
Paging is the mechanism that implements virtual memory. It divides the virtual address space of a process into fixed-size blocks called pages, and the physical memory into blocks of the same size called frames. When a process needs a page that is not in a frame, the OS loads it from secondary storage.
Compare preemptive and non-preemptive scheduling algorithms.
Preemptive Scheduling: The operating system can interrupt a process in the middle of its execution and allocate the CPU to another process. It is suitable for time-sharing environments.
Non-preemptive Scheduling: Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. It is simpler to implement but can lead to starvation for some processes.
What are semaphores and monitors? Provide use cases.
Semaphores: Integer variables that are used to solve the critical section problem. They have two atomic operations: `wait()` and `signal()`. Use cases include controlling access to a shared resource with a limited number of instances.
Monitors: High-level synchronization constructs that allow safe sharing of an abstract data type among concurrent processes. Only one process can be active in a monitor at any given time. Use cases include implementing thread-safe data structures.
Explain how modern operating systems handle system calls.
When a user program needs a service from the OS, it executes a system call. This causes a trap, switching the processor from user mode to kernel mode. The OS then executes the requested service and returns the result to the user program, switching the processor back to user mode.
How does an OS manage file systems and I/O operations?
File Systems: The OS provides a file system to organize and store files on storage devices. It manages file creation, deletion, and access, as well as directory structures.
I/O Operations: The OS manages I/O devices through device drivers. It handles I/O requests from processes, schedules them, and communicates with the device controllers to perform the actual I/O operations.