“Operating System” Chapters to Read
Threads
A thread is a lightweight process, meaning it’s a smaller unit of execution inside a Process. A process can have multiple threads sharing the same memory and resources. Threads make programs faster and more responsive, especially for tasks that can run in parallel.
Main types :
- User-level threads : managed by: thread library at user level
- Kernel-level threads : managed by: the operating system kernel
Multicore programming
Multicore programming is the practice of designing software to take advantage of multiple cpu cores in a single processor.
Purpose :
- It Improves performance by running tasks in parallel.
- Efficient resource utilization – multiple tasks can proceed at the same time instead of sequentially.
- Faster execution of cpu-intensive or multi-task workloads.
- Example : downloads images, videos, and scripts concurrently using separate threads.
Multithreading model
A multithreading model defines how user-level threads are mapped to kernel-level threads for execution on the cpu.
Types : many-to-one model
Description : many user-level threads are mapped to a single kernel thread.
Characteristics
- Only one thread executes at a time per process.
- Thread creation is fast (user-level threads don’t need kernel involvement).
- No true parallelism, even on multicore cpus.
Multithreading model
Types : one-to-one model
Description: each user thread is mapped to a separate kernel thread.
Characteristics
- True parallelism is possible on multicore cpus.
- Each thread can run independently.
Types : many-to-many model
Description : many user threads are mapped to a smaller or equal number of kernel threads.
Characteristics
- Combines advantages of both previous models.
- Multiple threads can run in parallel.
- Allows flexible scheduling.
Thread library
A thread library is a collection of functions or classes that allow programmers to create, manage, and synchronize threads in a program.
Functions
- Create new threads and terminate them when done.
- Coordinate threads to avoid conflicts over shared resources etc
Examples
- pthreads (posix threads) – widely used in c/c++
- java threads – built-in support in java
Implicit threading
Implicit threading is a multithreading approach where threads are created and managed automatically by the compiler, runtime system, or libraries, rather than explicitly by the programmer.
Advantages
- Easier programming model
- Better resource management
- Automatic load balancing
- Reduced thread creation overhead
Threading issues
Race conditions : It occur when multiple threads access shared data simultaneously, causing unpredictable results.
Deadlocks : It is situation where two or more threads wait indefinitely for resources held by each other.
Thread cancellation : Terminating a thread before it finishes its task. It must be handled carefully to avoid leaving shared resources in an inconsistent state.
