Operating System: Question Set – 16
What is a thread?
Within a process, a thread is the smallest unit of execution. Although they can run independently, threads have the same memory and resources as the parent process.
How is a thread different from a process?
Feature | Process | Thread |
Memory Space | Separate memory space | Shared memory space within a process |
Resource Sharing | Resources are not shared | Threads share resources |
Communication | Inter-process communication (IPC) needed | Direct communication through shared memory |
Overhead | High (context switch is expensive) | Low (lightweight context switch) |
What are the types of threads?
- User Threads: Managed by user-level libraries, not visible to the OS kernel.
- Kernel Threads: Managed and scheduled directly by the OS kernel.
What are the advantages of using threads?
- Concurrency: Threads allow multiple tasks to be executed simultaneously within a single process.
- Resource Sharing: Threads share memory and resources, which reduces overhead compared to processes.
- Responsiveness: Multithreading improves responsiveness in applications by allowing background tasks.
- Efficient Communication: Threads within the same process can communicate more easily than processes.
What are the disadvantages of using threads?
- Synchronization Issues: Threads need mechanisms like locks to avoid race conditions, leading to complexity.
- Debugging Difficulty: Multithreaded programs are harder to debug due to issues like deadlocks.
- Resource Contention: Threads competing for shared resources can lead to performance bottlenecks.
What are common thread-related issues?
- Race Conditions: When threads access shared resources without proper synchronization.
- Deadlocks: When two or more threads wait for each other indefinitely.
- Starvation: When a thread is unable to gain access to the resources it needs.
- Context Switching: Frequent context switches can degrade performance.
How do threads achieve synchronization?
Threads use synchronization mechanisms such as:
- Mutex (Mutual Exclusion): Prevents simultaneous access to a resource.
- Semaphores: Used for signaling and resource management.
- Condition Variables: Allow threads to wait for certain conditions to be true.
- Barriers: Synchronize threads at specific execution points.
What are multithreading models?
- Many-to-One: Multiple user threads mapped to a single kernel thread.
- One-to-One: Each user thread maps to a kernel thread.
- Many-to-Many: Many user threads mapped to many kernel threads.
What are the main states of a thread?
Threads can typically be in one of the following states:
- New: The thread is created but hasn’t started running.
- Runnable: The thread is ready to run and waiting for CPU scheduling.
- Running: The thread is actively executing instructions.
- Blocked/Waiting: The thread is waiting for a resource or an event.
- Terminated: The thread has completed execution.
How are threads implemented?
Threads can be implemented in three main ways:
- User-Level Threads (ULTs): Managed by user-space libraries. The kernel is unaware of them.
- Advantage: Fast creation and management.
- Disadvantage: If one thread blocks, the entire process blocks.
- Kernel-Level Threads (KLTs): Managed directly by the operating system.
- Advantage: True parallelism is possible on multiprocessor systems.
- Disadvantage: Slower due to kernel involvement.
- Hybrid: Combines user and kernel-level threads (e.g., many-to-many models).
How are threads scheduled?
Threads are scheduled using the same algorithms as processes:
- Preemptive Scheduling: The OS can interrupt a running thread to switch to another thread.
- Non-Preemptive Scheduling: Threads voluntarily give up the CPU.
Common algorithms include:
- Round-Robin
- Priority Scheduling
- Multilevel Queue Scheduling
What is thread safety?
Thread safety refers to ensuring that shared resources are accessed or modified by threads in a way that prevents data corruption or race conditions. Techniques include:
- Using synchronized blocks or mutex locks.
- Avoiding shared mutable state.
- Using thread-safe data structures.