TheHingineer

  • Operating System


  • OS Part-1

  • OS Part-2

  • OS Part-3

  • OS Part-4

  • OS Part-5

  • Threads in Operating System 

    Thread kya hota hai?

    Thread ek process ke andar ek chhoti unit hoti hai jo execute hoti hai. Ek process ke multiple threads ho sakte hain jo ek saath kaam karte hain aur same memory, files, aur data share karte hain.

    Example:

    Socho ek web browser jaise Google Chrome:

    • Ek thread web page load kar raha hai.

    • Dusra thread user input handle kar raha hai (mouse clicks, keyboard typing).

    • Teesra thread background me file download kar raha hai.

    Diagram: Process vs. Threads

    Process: Web Browser
      ├── Thread 1: Load Web Page
      ├── Thread 2: Handle User Input
      ├── Thread 3: Download Files
     

    Sabhi threads ek hi process ka hissa hain aur ek dusre ke sath memory share karte hain.


    Types of Threads 

    Threads 2 tareeke ke hote hain:

    1. User-Level Threads (ULTs)

    • Ye threads OS ke bina ek library dwara manage kiye jate hain.

    • Bahut fast aur lightweight hote hain.

    • Example: Java me jo threads hote hain, wo ULTs hote hain.

    2. Kernel-Level Threads (KLTs)

    • Ye threads operating system dwara manage kiye jate hain.

    • Ye thode slow hote hain, lekin powerful hote hain.

    • Example: Windows aur Linux ke system threads.


    Multithreading 

    Multithreading ka matlab hota hai ek process me ek saath multiple threads chalana.

    Multithreading ke Benefits

    ✔ CPU ka full utilization – Threads CPU ko idle nahi hone dete.
    ✔ Faster Execution – Multiple kaam ek saath ho sakte hain.
    ✔ Resource Sharing – Threads same process ki resources use karte hain, jo memory bachata hai.

    Diagram: Multithreading

    Process
      ├── Thread 1 (Task A)
      ├── Thread 2 (Task B)
      ├── Thread 3 (Task C)
     

    Multithreading Models

    Operating System threads ko manage karne ke liye alag-alag models use karta hai:

    1. Many-to-One Model

    • Multiple user threads ek hi kernel thread ko use karte hain.

    • Limitation: Ek time me sirf ek thread run ho sakta hai.

    • Example: Purane Solaris OS versions.

    2. One-to-One Model

    • Har user thread ka ek alag kernel thread hota hai.

    • Isme true parallel execution possible hoti hai.

    • Example: Windows, Linux, MacOS.

    3. Many-to-Many Model

    • Multiple user threads multiple kernel threads ke sath linked hote hain.

    • Yeh best model hai kyunki isme flexibility aur performance dono ache hote hain.

    • Example: Modern Linux aur Solaris OS.


    Thread Lifecycle 

    Ek thread apne execution ke dauraan different states se guzarta hai:

    1. -> New: Jab ek thread create hota hai, par start nahi hota.

    2. -> Ready: Thread CPU ka wait kar raha hai.

    3. -> Running: Jab thread CPU par execute ho raha hai.

    4. -> Blocked: Jab thread kisi event ka wait kar raha ho (jaise I/O operation).

    5. -> Terminated: Jab thread ka execution complete ho jaye.

    Diagram: Thread States

    New → Ready → Running → (Blocked) → Terminated


    Threads vs. Processes

    FeatureThreadProcess
    Memory UsageKamZyada
    CommunicationFast (Shared Memory)Slow (IPC needed)
    Creation TimeFastSlow
    Context SwitchingFastSlow
    ExampleWeb Browser ke TabsAlag-alag Applications

    Conclusion

    ✔ Threads system ko efficient aur fast banate hain.
    ✔ Multithreading se ek process ke multiple parts parallel chal sakte hain.
    ✔ OS different models use karta hai threads ko manage karne ke liye.

    Scroll to Top