0% found this document useful (0 votes)
8 views10 pages

OS Threads

OS Class Notes

Uploaded by

Himanshu Narayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

OS Threads

OS Class Notes

Uploaded by

Himanshu Narayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Operating System-Threads

Thread
Thread is smallest execution within process. It is basic unit of CPU utilization.
It is also called Lightweight Process.

It shares with other threads belonging to the same process its code section,
data section, and other resources.

A single-threaded application can only run on one core, while multithreaded


applications can utilize multiple cores for faster execution.

Multithreaded programming offers four main benefits:

1. Responsiveness: Multithreading allows parts of a program to keep


running even when one part is busy, making it more responsive.

2. Resource Sharing: Threads within the same process share memory and
resources by default, making it easier to manage compared to processes.

3. Economy: Creating threads is more efficient than creating entire


processes, saving both memory and time. Switching between threads is
also faster than switching between processes.

4. Scalability: In systems with multiple processors or cores, threads can run


in parallel, boosting performance.

© HIMANSHU NARAYAN PRASAD, VVIT 1

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

Thread Pool

Unlimited threads could exhaust system resources, such as time or memory.


One solution to this CPU problem is to use a thread pool.

Idea behind this mechanism is –

Instead of creating a new thread every time a task needs to be executed, a


pool of threads is created when the application starts. These threads remain
idle in the pool, waiting for tasks to be assigned.

Thread pool working mechanism:


 Thread Creation at Start-Up
 Handling Requests
 Thread Assignment
 Returning Threads to the Pool

Thread pool image (from Wikipedia)

© HIMANSHU NARAYAN PRASAD, VVIT 2

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

Multicore Programming

Multicore programming in an operating system refers to designing software


that can run efficiently on computers with multiple CPU cores.

Each core can handle its own tasks independently, allowing the system to
process multiple instructions at the same time, which boosts performance.

Benefits of Multicore Programming:


1. Parallelism: With multiple cores, different parts of a program (or
different programs) can run at the same time.
2. Multithreading: Programs can be written to use multiple threads (small
tasks within a program). These threads can run on different cores,
speeding up the program.
3. Load Balancing: The OS tries to evenly distribute tasks across cores so
that no single core is overwhelmed while others are idle. This helps
maximize efficiency and performance.
4. Concurrency: In multicore systems, multiple tasks can be handled at
once, increasing the system's ability to handle multiple users or
applications smoothly.

Difference between concurrency and parallelism:

 Concurrency: This refers to a system’s ability to handle multiple tasks by


allowing them to make progress, but not necessarily at the same time.

 Parallelism: This refers to performing multiple tasks at the same time.In


parallel systems, tasks are literally running simultaneously on different
cores or processors, leading to faster execution.

it is possible to have concurrency without parallelism.

© HIMANSHU NARAYAN PRASAD, VVIT 3

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

There are two types of parallelism:

1. Data Parallelism: Distributes subsets of the same data across multiple


cores, where each core performs the same operation on its subset.

2. Task Parallelism: Distributes different tasks (threads) across multiple


cores, where each thread performs a unique operation, either on the
same data or different data.

Threads can be implemented in Kernel Area and User Area.

© HIMANSHU NARAYAN PRASAD, VVIT 4

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

Important points related to User Space and Kernel Space Threads:

 In User Space, Threads are managed entirely within the user-level


application [ OS is unaware of their existence]
In Kernel Space, Threads are managed by the operating system. [ OS is
aware of their existence and schedules them]

 In User Space, the application itself is responsible for scheduling and


managing the threads.
In Kernel Space, the OS is responsible for scheduling and managing the
threads.

 In User Space, context switching between threads is typically faster since


it does not involve the operating system.
In Kernel Space, context switching between threads involves the OS,
making it generally slower than user space thread context switching.

Multithreading Model

Multithreading models define how threads are mapped to the system’s


resources.
It defines how user-level threads (created by a program) are mapped to kernel-
level threads (managed by the operating system).

There are 3 common model:


many-to-one model, one-to one model and many-to-many model.

1. Many to One Model: [ Threads in early Java and Solaris ]


 It maps many user-level threads to a single kernel thread.
 Threads are handled entirely by the user-space thread library.
 This model is efficient in thread management due to no kernel
involvement.
Drawbacks:
 If a thread makes a blocking system call, the entire process blocks.
 Only one thread can access the kernel at a time, preventing parallel
execution on multicore systems.

© HIMANSHU NARAYAN PRASAD, VVIT 5

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

2. One to One Model: [ Linux, Windows ]


 This model maps each user-level thread to a corresponding kernel
thread.
 Threads are managed by the operating system.
 This model allows greater concurrency and true parallelism since
other threads can run when one thread blocks.
Drawbacks:
 Creating user threads incurs the overhead of creating kernel
threads.
 Having too many kernel threads may impact system performance.

© HIMANSHU NARAYAN PRASAD, VVIT 6

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

3. Many to Many Model:


 This model multiplexes many user-level threads to a smaller or
equal number of kernel threads.
 In this model, kernel can schedule multiple user threads on
available kernel threads.
 In this model, user can create many user threads without concern
for kernel thread limitations. It also allow true parallelism because
the kernel can schedule another thread when one blocks.

Two- level model is variation of the many-to-many model that allows


some user threads to be bound to specific kernel threads.

Although the many-to-many model appears to be the most flexible of


the models, in practice it is difficult to implement.
In addition, with an increasing number of processing cores appearing
on most systems, limiting the number of kernel threads has become
less important.

As a result, most operating systems now use the one-to-one model.

© HIMANSHU NARAYAN PRASAD, VVIT 7

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

Threads Library:

Three main thread libraries are in used for thread operations:


POSIX Pthreads, Windows, and Java.

POSIX threads:
 A standard for thread creation and management in UNIX-like systems.
 It is widely used in C/C++ applications on UNIX-like systems, providing a
robust set of thread management functions.

Windows library:
 The threading API for the Windows operating system.
 It provides functions for creating, managing, and synchronizing threads.

Java:
 Built-in support for multithreading in the Java programming language.

© HIMANSHU NARAYAN PRASAD, VVIT 8

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

Example – 10 thread creation and join them using C Posix library

#include <pthread.h>
#include <stdio.h>

// Thread function definition


void* threadFunction(void* arg) {
int thread_id = *(int*)arg; // dereference the
passed argument
printf("Thread %d is running\n", thread_id);
return NULL;
}

int main() {
pthread_t threads[10]; // Array to store thread
int thread_ids[10];// Array to store thread num

// Create 10 threads
for (int i = 0; i < 10; i++) {
thread_ids[i] = i + 1;
pthread_create(&threads[i], NULL,
threadFunction, &thread_ids[i]);//function call
}

// Join all threads


for (int i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}

printf("All threads completed\n");


return 0;
}
Output:

© HIMANSHU NARAYAN PRASAD, VVIT 9

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads

Example – 10 thread creation and join them using Win32API

#include <windows.h>
#include <stdio.h>
//Function definition
DWORD WINAPI threadFunction(LPVOID arg) {
int thread_id = *(int*)arg; // Cast and dereference
printf("Thread %d is running\n", thread_id);
return 0;
}

int main() {
HANDLE threads[10]; // Array to store thread
int thread_ids[10]; // Array to store thread num

// Create 10 threads
for (int i = 0; i < 10; i++) {
thread_ids[i] = i + 1;
threads[i] = CreateThread(
NULL, // Default security attributes
0, // Default stack size
threadFunction, // Thread function call
&thread_ids[i], // Thread function arg
0, // Default creation flags
NULL // Ignore thread ID
);

if (threads[i] == NULL) {
printf("Failed to create thread %d\n",i + 1);
return 1;
}
}

// Wait for all 10 threads to finish


WaitForMultipleObjects(10, threads, TRUE, INFINITE);

// Close thread handles


for (int i = 0; i < 10; i++) {
CloseHandle(threads[i]);
}

printf("All threads completed\n");


return 0;
}

© HIMANSHU NARAYAN PRASAD, VVIT 10

References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition

You might also like