OPERATING SYSTEMS
THREADS
Dr. [Link]
Associate Professor – (SCORE)
VIT University
What is a Thread?
A process is divided into number of light weight process, Each
lightweight process is called as thread.
The smallest sequence of programmed instructions that can be
managed independently by a scheduler.
Example (Word Processor)
Each thread has its own:
Program Counter
Registers ( holds current working variables)
Stack (Contains execution history)
Thread State
PROPERTIES
Threads can share
=> Address space
=> Opened Files & Other resources
=> CPU
(but only one thread is active at a time)
Threads can create
=> Childs threads
Threads are not independent of one another
THREAD LIFE CYCLE
BORN
READY
RUNNING
WAIT SLEEP TERMINATE
THREAD TYPES
Based on Implementation Based on Functionality
Kernel Level User Level => One Process One Thread
Thread Thread
=> One Process Multiple Thread
=> Many-to-one
=> Multi Process One Thread
=> One-to-one
=> Many-to-many => Multi Process Multi Thread
Thread Libraries
A thread library provides programmers with an API for creating and
managing threads.
Thread Libraries-Examples
USER LEVEL THREADS
USER LEVEL THREADS ISSUES
Threads managed entirely by the user-level library.
The kernel knows nothing about user-level threads and manage them
as if they where single-threaded processes.
Each process maintains its own private thread table which contains
details about PC,STACK,REGISTER & STATE.
USER LEVEL THREADS
Advantages
(1) Simple management: Creating, switching and synchronizing
threads done in user-space without kernel intervention.
(2) Fast and efficient: switching threads not much more expensive.
USER LEVEL THREADS
Disadvantages
(1) Lack of coordination between the user-level thread manager and
the kernel.
(2) OS may make poor decisions like:
(i) Scheduling a process with idle threads.
(ii) Giving a process as a whole one time slice irrespective of whether
the process has 1 or 1000 threads.
KERNEL LEVEL THREADS
KERNEL LEVEL THREADS - ISSUES
The kernel knows about and manages all threads.
No thread table in each process. Instead One thread control block
(TCB) per thread in the system.
Provide system calls to create and manage threads from user
space.
KERNEL LEVEL THREADS
Advantages
(1) Scheduler may decide to give more CPU time to a process
having a large number of threads.
(2) The kernel has full knowledge of all threads.
KERNEL LEVEL THREADS
Disadvantages
(1) Kernel manage and schedule all threads.
(2) Significant overhead and increase in kernel complexity.
Implementing user-level threads
(1) Many-to-one user-level threads:
If a process has 1,00,000 user level threads, but only one kernel thread,
then the process can only run one user-level thread at a time because
there is only one kernel-level thread associated with it.
(2) One-to-one user-level threads:
If a process has multiple kernel-level threads, then it can potentially
execute multiple instructions in parallel on a multicore machine.
This models makes thread creation ”expensive” and most
implementations restrict the number of threads supported by the
Operating system.
(3) Many-to-many user-level threads:
A process request some fixed number of kernel-level threads.
Multiple user level threads may execute in parallel, and the process
can have fine-grained control over how threads execute.
Thread based on Functionality - Examples
Java Run
MS DOS Time
Environment
UNIX Windows2000/XP,Solaris,Linux
PROCESS VS THREADS
(1) Can’t share the same memory Can share memory & Files
area {address space}
(2) Take more time to create, execution Takes less time to create &
& termination a process terminate
(3) Takes more time to switch between Takes less time to switch
two process between two threads
(4) System Call required for
Not Required
communication
(5) Process are loosely coupled Tightly coupled