[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

Thread 4

A thread is the smallest unit of execution within a process, allowing multiple threads to run concurrently and share resources. There are user-level threads, managed by the application, and kernel-level threads, managed by the operating system, each with distinct benefits. Utilizing threads improves performance, responsiveness, and simplifies complex system design, with real-world applications in web browsers, video games, and online transactions.

Uploaded by

gaurav prajapati
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)
12 views5 pages

Thread 4

A thread is the smallest unit of execution within a process, allowing multiple threads to run concurrently and share resources. There are user-level threads, managed by the application, and kernel-level threads, managed by the operating system, each with distinct benefits. Utilizing threads improves performance, responsiveness, and simplifies complex system design, with real-world applications in web browsers, video games, and online transactions.

Uploaded by

gaurav prajapati
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/ 5

Thread

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.

Benefits of using Threads


U lising threads in a program enhances performance by enabling concurrent execu on of
mul ple tasks. As a result, this approach can op mise system resource u lisa on and achieve
faster response mes. Moreover, threads enhance applica on responsiveness by enabling
con nuous execu on even when a thread is blocked or awai ng comple on of I/O
opera ons. Moreover, threads simplify the design and implementa on of complex systems by
decomposing tasks into smaller, more manageable units of execu on. Overall, using threads
can lead to more efficient and scalable so ware applica ons.

 Faster context switching compared to processes.


 Efficient resource sharing since threads within a process share the same memory
space.
 Parallel execution allows multiple tasks to run concurrently, improving performance.
 Responsiveness by handling multiple tasks simultaneously (e.g., UI updates while
processing data).

Difference Between Process and Thread in Opera ng Systems


Defini on Process Thread

A process is an independent program A thread is a lightweight


Defini on in execu on with its own memory subprocess within a process that
space. shares the same memory.

Memory Each process has its own address space Threads of the same process
Sharing and memory. share memory and resources.

Process crea on is slow because it Thread crea on is faster since it


Crea on Time requires alloca ng separate memory shares resources with its parent
and resources. process.
Processes communicate via Inter-
Threads communicate easily
Process Communica on (IPC) (e.g.,
Communica on since they share memory within
pipes, message queues, shared
the same process.
memory).

Switching between threads is


Context Switching between processes is slower
faster because they share the
Switching due to high overhead.
same memory.

If a thread crashes, it may affect


If a process crashes, it does not affect
Crash Impact other threads in the same
others.
process.

Mul ple browser tabs, or


Running mul ple instances of a
Example handling mul ple tasks in a
browser or text editor.
game (e.g., rendering and AI).

Mul threading example in C using the pthread library:


Example: Crea ng and Running Threads in C

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

// Function to be executed by the thread


void* print_message(void* arg)
{
char* message = (char*)arg;
for (int i = 0; i < 5; i++)
{
printf("%s\n", message);
sleep(1); // Simulate some work
}
return NULL;
}

int main()
{
pthread_t thread1, thread2;

// Create two threads


pthread_create(&thread1, NULL, print_message, "Thread 1: Hello");
pthread_create(&thread2, NULL, print_message, "Thread 2: World");

// Wait for threads to finish execution


pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Threads finished execution.\n");
return 0;
}
Explana on:
1. pthread_create() is used to create a new thread.
2. pthread_join() waits for the threads to complete execution before exiting the
program.
3. Each thread runs the function print_message(), printing messages concurrently.

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)

Note: fork() creates a new process with a different memory space.

Key Differences in Code:

1. fork() creates separate processes with different memory spaces.


2. pthread_create() creates threads within the same process, sharing memory.
Real-Life Examples of Threads in Opera ng Systems
Threads are widely used in modern compu ng for mul tasking and improving performance.
Here are some real-world scenarios where threads are used:

1. Web Browsers (Mul -Tab Browsing)

 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.

2. Video Games (Parallel Tasks)

 Modern games use threads to handle multiple tasks simultaneously:


o Rendering Thread: Draws graphics on the screen.
o AI Thread: Controls NPC behavior.
o Physics Thread: Manages collision detection and movements.
o Network Thread: Handles multiplayer communication.
 Example: Games like Call of Duty, GTA, and FIFA use multi-threading for smooth
gameplay.

3. Online Streaming (Ne lix, YouTube, Spo fy)

 Buffering Thread: Loads the video/audio in the background.


 Rendering Thread: Displays the video or plays audio.
 User Interaction Thread: Handles clicks, play/pause actions.
 Example: YouTube uses threads to ensure seamless video playback while buffering in
the background.

4. Operating System (Task Scheduling)

 The OS uses multiple threads to handle different tasks:


o User Interface Thread: Manages windows, menus, and input.
o Background Services: Handles file indexing, updates, and notifications.
 Example: Windows, macOS, and Linux all use multi-threading.

5. Banking Applications (Online Transactions)

 One thread verifies login credentials.


 Another thread processes the transaction.
 Another thread updates the account balance.
 Example: UPI payments, net banking, and stock trading apps use multi-threading to
handle simultaneous transactions.
6. Microsoft Word / Google Docs (Auto-Save & Editing)

 Main Thread: Allows the user to type and edit.


 Auto-Save Thread: Saves the document in the background.
 Spell Check Thread: Checks grammar while typing.
 Example: MS Word, Google Docs, Notepad++ use threads to ensure smooth editing
experience.

7. Server Applications (Web Servers & Databases)

 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.

8. Ride-Sharing Apps (Uber, Lyft, Ola)

 One thread fetches nearby drivers.


 Another thread tracks the real-time location.
 Another thread processes the payment.
 Example: Uber, Lyft, and Ola use threads to manage rides efficiently.

You might also like