Operating System: Question Set – 18
What is thread-local storage (TLS)?
Thread-local storage enables a unique instance of variables for every thread. When several threads separately access variables, this prevents data corruption.
What is the difference between cooperative and preemptive multithreading?
- Cooperative Multithreading: Threads voluntarily yield control to allow other threads to execute. It relies on thread cooperation.
- Preemptive Multithreading: The OS scheduler forcibly interrupts threads to allocate CPU time to others.
What are race conditions in threads?
When several threads access shared data at once and the time of their execution determines the outcome, this is known as a race condition. In order to prevent race scenarios, proper synchronization techniques are required.
How do you prevent deadlocks in multithreaded programs?
Strategies to prevent deadlocks include:
- Avoid Circular Wait: Assign a global order to resources and acquire them in a consistent order.
- Use Timeout: Implement timeouts for thread operations.
- Avoid Holding Multiple Locks: Minimize the number of locks held simultaneously by a thread.
- Deadlock Detection: Periodically check for circular dependencies.
How do threads handle I/O operations?
Threads handle I/O operations using:
- Blocking I/O: The thread waits until the I/O operation completes.
- Non-blocking I/O: The thread performs other tasks while waiting for the I/O operation to complete.
- Asynchronous I/O: The I/O operation is handled by the OS, and the thread is notified when it’s complete.
How are threads managed in Linux?
In Linux:
- Threads are treated as lightweight processes, implemented using the clone() system call.
- Linux provides a POSIX-compliant threading library called pthreads.
- The sched library is used for thread scheduling.
What is thread pooling, and how does it work?
Maintaining a set number of threads to carry out tasks is known as thread pooling. A task is allocated to an open thread as soon as it is received. The task waits in a queue if there are no open threads. The overhead of continually generating and removing threads is decreased with this method.
What is the difference between hard and soft threads?
- Hard Threads: Threads that are managed by the operating system kernel (kernel-level threads).
- Soft Threads: Threads that are managed in user space, without kernel involvement (user-level threads).
How do you debug multithreaded programs?
Debugging multithreaded programs can involve:
- Using Thread Debuggers: Tools like
gdb
, Visual Studio Debugger, or Java Debugger (JDB). - Logging: Adding thread-specific logs.
- Analyzing Core Dumps: For identifying thread states and issues.
- Deadlock Detection Tools: Tools to detect circular dependencies among threads.
What is a semaphore, and how is it used in threads?
A semaphore is a synchronization primitive used to manage resource access among threads. Types of semaphores:
- Counting Semaphore: Tracks multiple available resources.
- Binary Semaphore: Acts as a lock, allowing one thread at a time to access a resource.
How are threads terminated?
Threads can be terminated by:
- Completing execution of the thread function.
- Calling a thread termination function (e.g.,
pthread_exit()
in POSIX). - Forcefully terminated by the parent process (not recommended as it may lead to resource leaks).
What is a fork-join model in threading?
The fork-join model is a parallel programming model where:
- A thread forks into multiple threads to perform tasks in parallel.
- These threads later join back into the main thread after completing their tasks.