OS Threads
OS 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.
2. Resource Sharing: Threads within the same process share memory and
resources by default, making it easier to manage compared to processes.
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
Thread Pool
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
Multicore Programming
Each core can handle its own tasks independently, allowing the system to
process multiple instructions at the same time, which boosts performance.
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
Multithreading Model
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
Threads Library:
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.
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
#include <pthread.h>
#include <stdio.h>
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
}
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition
Operating System-Threads
#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;
}
}
References: “Operating System Concept” 10th edition & “Operating system design and implementation” 3rd edition