Thread 4
Thread 4
In an opera ng system, a thread is the smallest unit of execu on within a process, allowing
mul ple threads to run concurrently within the same process. A process can have mul ple
threads that share the same resources, such as memory and file handles, but each thread has
its own registers, program counter, and stack.
Types of thread
There are two main types of threads: user-level threads and kernel-level threads.
1. User-level threads (UTL): are managed by the applica on itself and are independent of
the opera ng system's visibility. They are lightweight and can be created and managed
quickly. Managed by user-space libraries, without kernel involvement.
2. Kernel-level threads (KTL): Managed directly by the opera ng system kernel and have
a dedicated thread control block. Kernel-level threads are more reliable and provide
be er performance but are heavier in terms of system resources.
Memory Each process has its own address space Threads of the same process
Sharing and memory. share memory and resources.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int main()
{
pthread_t thread1, thread2;
C program demonstra ng the processes using fork() (for process crea on)
Process Example (Using fork())
Each process has its own memory space and runs independently.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid = fork(); // Create a new process
if (pid > 0)
{
printf("Parent Process (PID: %d)\n", getpid());
} else if (pid == 0)
{
printf("Child Process (PID: %d)\n", getpid());
} else
{
printf("Fork failed!\n");
}
return 0;
}
Output Example:
Parent Process (PID: 12345)
Child Process (PID: 12346)
Each tab in a web browser (like Chrome, Firefox) runs in its own thread.
While one tab is loading a webpage, another can be playing a video, and another can
be downloading a file.
Example: Google Chrome uses multi-threading for better performance.
A web server like Apache, Nginx, or Node.js handles multiple users with threads:
o One thread per user request to handle webpage loading.
Example: Amazon, Facebook, Google use multi-threaded web servers to handle
millions of users.