0% found this document useful (0 votes)
100 views7 pages

Thread in Operating System

A thread is a path of execution within a process that allows parallelism. Processes can contain multiple threads to divide work. Threads share memory and resources within a process for faster communication and context switching compared to processes which have separate memory spaces. The main benefits of multithreading are improved responsiveness, utilization of multiple processors, and higher system throughput. There are two types of threads - user level threads managed by users and kernel level threads managed by the operating system.

Uploaded by

Umesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
100 views7 pages

Thread in Operating System

A thread is a path of execution within a process that allows parallelism. Processes can contain multiple threads to divide work. Threads share memory and resources within a process for faster communication and context switching compared to processes which have separate memory spaces. The main benefits of multithreading are improved responsiveness, utilization of multiple processors, and higher system throughput. There are two types of threads - user level threads managed by users and kernel level threads managed by the operating system.

Uploaded by

Umesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Thread in Operating System

What is a Thread?
A thread is a path of execution within a process. A process can contain multiple
threads.

Multithreading
 A thread is also known as lightweight process. The idea is to achieve parallelism
by dividing a process into multiple threads.
 For example, in a browser, multiple tabs can be different threads.
 MS Word uses multiple threads: one thread to format the text, another thread to
process inputs, etc. More advantages of multithreading are discussed below

Process vs Thread?
 The primary difference is that threads within the same process run in a shared
memory space, while processes run in separate memory spaces.
 Threads are not independent of one another like processes are, and as a result
threads share with other threads their code section, data section, and OS
resources (like open files and signals). But, like process, a thread has its own
program counter (PC), register set, and stack space.

Advantages of Thread over Process


1. Responsiveness: If the process is divided into multiple threads, if one thread
completes its execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to
process context switch. Process context switching requires more overhead from the
CPU.
3. Effective utilization of multiprocessor system: If we have multiple threads in a
single process, then we can schedule multiple threads on multiple processor. This will
make process execution faster.
4. Resource sharing: Resources like code, data, and files can be shared among all
threads within a process.
Note: stack and registers can’t be shared among the threads. Each thread has its own
stack and registers.
5. Communication: Communication between multiple threads is easier, as the threads
shares common address space. while in process we have to follow some specific
communication technique for communication between two process.
6. Enhanced throughput of the system: If a process is divided into multiple threads,
and each thread function is considered as one job, then the number of jobs completed
per unit of time is increased, thus increasing the throughput of the system.

Types of Threads
There are two types of threads.

1. User Level Thread


2. Kernel Level Thread

By Mr. UMESH KUMAR, MCA (GGU) Page | 1


NET/SET/GATE/JRF OTHER EXAM
Difference between User Level thread and Kernel Level thread
USER LEVEL THREAD KERNEL LEVEL THREAD
User thread are implemented by users. kernel threads are implemented by OS.
OS doesn’t recognized user level threads. Kernel threads are recognized by OS.
Implementation of User threads is easy. Implementation of Kernel thread is
complicated.
Context switch time is less. Context switch time is more.
Context switch requires no hardware Hardware support is needed.
support.
If one user level thread perform blocking If one kernel thread perform blocking
operation then entire process will be operation then another thread can continue
blocked. execution.
Example : Java thread, POSIX threads. Example : Window Solaris.

thread in operating system


किसी भी ऑपरे ट ग
ॊ ससस् म िे प्रोसेस िे अॊदर थ्रेड execution िा एि रास्ता होता है । इसिे अऱावे एि
प्रोसेस िे अॊदर एि से ज्यादा थ्रेड भी हो सिते हैं।

मल्टीथ्रेड ग
िं क्यों?
 थ्रेड िो ऱाइ वे प्रोसेस भी िहा जाता है । किसी प्रोसेस िो बहुत सारे थ्रेड में ववभाजजत िर दे ते हैं
और स्मनान्तारीिरण िो प्राप्त िरते हैं।
 उदाहरण िे सऱए किसी ब्राउज़र में बहुत सारे ै ब्स िो थ्रेड िी तरह दे खा जा सिता है ।

By Mr. UMESH KUMAR, MCA (GGU) Page | 2


NET/SET/GATE/JRF OTHER EXAM
 एमएस वडड िई सारे थ्रेड िा प्रयोग िरता है- एि थ्रेड िो े क्सस् िो फॉमे िरने िे सऱए, दस
ू रे
थ्रेड से प्रोसेस इनपु िरने िे सऱए, इत्याटद। मल् ीथ्रेडडॊग िे और भी फायदे हैं जजन्हें हम आपिो
बतायेंगे।
प्रोसेस vs थ्रे ?
 इन दोनों िे बीच में सबसे बड़ा अॊतर यही है कि किसी भी समान प्रोसेस िे अॊदर सारे थ्रेड शेयडड
मेमोरी स्पेस में रन होता है जबकि प्रोसेसर अऱग मेमोरी स्पेस में रन होते हैं।
 थ्रेड्स प्रोसेस िी तरह एि-दस
ू रे से स्वतॊत्र नहीॊ होते और इसिे पररणामस्वरूप थ्रेड्स िोड
सेक्सशन में दस
ू रे थ्रेड्स िे साथ डा ा सेक्सशन और ऑपरे ट ग
ॊ ससस् म ररसोसेज जैसे कि ओपन
फाइल्स और ससग्नल्स िो शेयर िरते हैं। ऱेकिन प्रोसेस िी तरह ही थ्रेड िा अपना अऱग प्रोग्राम
िाउॊ र (PC), रजजस् र से और एि स् ै ि स्पेस होता है ।
benefits of threads in os
1. Responsiveness: अगर प्रोसेस िो एि से ज्यादा थ्रेड में ववभाजजत िर टदया जाए और एि थ्रेड
अपना execution िो पूरा िर ऱे तब इसिे आउ पु िो जल्द से जल्द ररस्पाॊस टदया जा सिता है ।
2. Faster context switch: थ्रेड्स िे बीच िॉन् े क्सस् जस्वच ाइम प्रोसेस िॉन् े क्सस् जस्वच िे ाइम
से बहुत िम होता है । प्रोसेस िॉन् े क्सस् जस्वच िा अथड हुआ CPU िे सऱए ज्यादा ओवरहे ड।
3. Effective Utilization of Multiprocessor system: अगर हमारे पास एि ससॊगऱ प्रोसेस में
एि से ज्यादा थ्रेड हो तब हम एि से ज्यादा प्रोसेसर में एि से ज्यादा थ्रेड िो शेड्यूऱ िर सिते हैं। ये
प्रोसेस िे एक्ससीक्सयू होने िी गतत िो बढ़ा दे ता है ।
4. Resource sharing: ररसोसेज जैसे कि िोड, डा ा और फाइऱ िो किसी प्रोसेस िे अॊदर सभी थ्रेड
िे बीच शेयर किया जा सिता है ।
नो : स् ै ि और रजजस् र िो थ्रेड्स िे बीच शेयर नहीॊ किया जा सिता। हर थ्रेड िा अपना स् ै ि और
रजजस् र होता है ।
5. Communication: एि से ज्यादा थ्रेड्स िे बीच िा सॊवाद आसान होता है क्सयोंकि थ्रेड एि ही एड्रेस
स्पेस िो साझा िरते हैं। जबकि हमे प्रोसेस में दो प्रोसेस िे बीच िम्यतु निेशन िे सऱए िुछ ख़ास
िम्यतु निेशन तिनीि िो फॉऱो िरना पड़ता है ।
6. Enhanced Throughput of the system: अगर प्रोसेस िो बहुत सारे थ्रेड्स में ववभाजजत किया
गया है और हर थ्रेड फॊक्सशन िो एि जॉब िी तरह दे खा जाता है तब एि यतू न समय में परू े होने वाऱे
जॉब्स िी सॊख्या बढ़ जाती है । इसीसऱए ससस् म िा थ्रप
ू ु भी बढ़ता है ।

user level thread and kernel level thread

By Mr. UMESH KUMAR, MCA (GGU) Page | 3


NET/SET/GATE/JRF OTHER EXAM
थ्रेड्स दो प्रिार िे होते हैं- यूजर ऱेवऱ थ्रेड और िनेऱ ऱेवऱ थ्रेड। इन दोनों िो आप नीचे टदए गये चा ड
िी मदद से समझ सिते हैं:

यूजर ऱेवऱ थ्रे कर्नेऱ ऱेवऱ थ्रे


यूजर थ्रेड िो यूजर द्वारा इमप्ऱेमें किया जाता िनेऱ थ्रेड िो ऑपरे ट ग
ॊ ससस् म द्वारा इमप्ऱेमें
है । किया जाता है ।
ऑपरे ट ग
ॊ ससस् म यूजर ऱेवऱ थ्रेड िो नहीॊ िनेऱ थ्रेड िो ऑपरे ट ग
ॊ ससस् म पहचान ऱेते हैं।
पहचान पाता।
यूजर थ्रेड िो आसानी से इमप्ऱेमें किया जा िनेऱ थ्रेड िा इम्प्ऱीमें े शन िटिन है ।
सिता है ।
िॉन् े क्सस् जस्वच ाइम िम होता है । िॉन् े क्सस् जस्वच ाइम ज्यादा होता है ।
िॉन् े क्सस् जस्वच िो किसी हाडडवेयर सपो ड िी हाडडवेयर सपो ड िी जरूरत पड़ती है ।
जरूरत नहीॊ पड़ती।
अगर एि यूजर ऱेवऱ थ्रेड ब्ऱॉकिॊग ऑपरे शन अगर एि िनेऱ थ्रेड ऑपरे शन ब्ऱॉकिॊग ऑपरे शन
परफॉमड िरता है तब सारे प्रोसेस िो ब्ऱाि िर परफॉमड िरता है तो बाॊिी थ्रेड execution जारी रख
टदया जाता है । सिते हैं।
उदाहरण: जावा थ्रेड, POSIX थ्रेड्स इत्याटद। उदाहरण: ववॊडो सोऱाररस

Introduction for Multithreading


 एि साथ अनेि threads या sub-programs िो run किया जाना Multithreading होता है |
 Java में Multithreading िा program एि साथ एि से ज्यादा threads िो run िरता है | ये
thread बहुत ही light-weight िी process होती है |
 अगर Multithreading िा उदाहरण ऱे तो जब MS Word में िोई data सऱखते वक्सत spell
checks होता है , ये process MS Word िे background पर होता है |
Thread क्या है ?
 thread ये एि process िा sub-process है |
 thread light-weight होते है |
 program में एि या एि से ज्यादा threads create किये जा सिते है |
 जब process पर िोई thread create नहीॊ किया जाता तो main thread create होता है |
 thread; process िी common memory area िो share िरता है | thread अऱग से
memory िो allocate नहीॊ िरता |

By Mr. UMESH KUMAR, MCA (GGU) Page | 4


NET/SET/GATE/JRF OTHER EXAM
Life Cycle for Thread
threads िा एि Life Cycle है | threads अऱग-अऱग पड़ावों से गुजरता है |
 New State
 Runnable State
 Running State
 Waiting State
 Dead State

By Mr. UMESH KUMAR, MCA (GGU) Page | 5


NET/SET/GATE/JRF OTHER EXAM
New State : यहाॉ से thread िी शुरुआत होती है | यहाॉ पर thread िा objet create किया
जाता है |

By Mr. UMESH KUMAR, MCA (GGU) Page | 6


NET/SET/GATE/JRF OTHER EXAM
Ready/Runnable State : जब start() method िो call किया जाता है , तब thread
New से Runnable State पर आ जाता है | ये execute होने िे सऱए Ready होता है |
Running State : जब thread execution होना शुरू होता है , तब thread इस state पर
होता है |
Waiting State : दस
ु रे thread िो perform िरने िे सऱए िुछ thread िो block या
waiting state पर होते है , waiting state पर जो thread होता है उसे resume किया जाता है
|
Dead State : यहाॉ पर thread िा िाम परू ा होिर वो बॊद हो जाता है |

Example for Thread States


Source Code :
public class CheckState extends Thread{

public void run() {


System.out.println("run method");
}
public static void main(String args[]){

CheckState t1 = new CheckState();


CheckState t2= new CheckState();

System.out.println("t1 State : " + t1.getState());


System.out.println("t2 State : " + t2.getState());

t1.start();
System.out.println("t1 State : " + t1.getState());
System.out.println("t2 State : " + t2.getState());

t2.start();
System.out.println("t1 State : " + t1.getState());
System.out.println("t2 State : " + t2.getState());
}
}
Output :
t1 State : NEW
t2 State : NEW
t1 State : RUNNABLE
run method
t2 State : NEW
t1 State : TERMINATED
run method
t2 State : RUNNABLE

By Mr. UMESH KUMAR, MCA (GGU) Page | 7


NET/SET/GATE/JRF OTHER EXAM

You might also like