os_thread
os_thread
The process can be split down into so many threads. For example, in a browser, many tabs
can be viewed as threads. MS Word uses many threads - formatting text from one thread,
processing input from another thread, etc.
Need of Thread:
o It takes far less time to create a new thread in an existing process than to create a
new process.
o Threads can share the common data, they do not need to use Inter- Process
communication.
Types of Threads
In the operating system, there are two types of threads.
2. User-level thread.
User-level thread
The operating system does not recognize the user-level thread. User threads can be easily
implemented and it is implemented by the user. If a user performs a user-level thread
blocking operation, the whole process is blocked. The kernel level thread does not know
nothing about the user level thread. The kernel-level thread manages user-level threads as if
they are single-threaded processes?examples: Java thread, POSIX threads, etc.
1. The user threads can be easily implemented than the kernel thread.
2. User-level threads can be applied to such types of operating systems that do not
support threads at the kernel-level.
6. User-level threads representation is very simple. The register, PC, stack, and mini
thread control blocks are stored in the address space of the user-level process.
7. It is simple to create, switch, and synchronize threads without the intervention of the
process.
1. User-level threads lack coordination between the thread and the kernel.
The kernel thread recognizes the operating system. There is a thread control block and
process control block in the system for each thread and process in the kernel-level thread.
The kernel-level thread is implemented by the operating system. The kernel knows about all
the threads and manages them. The kernel-level thread offers a system call to create and
manage the threads from user-space. The implementation of kernel threads is more difficult
than the user thread. Context switch time is longer in the kernel thread. If a kernel thread
performs a blocking operation, the Banky thread execution can continue. Example: Window
Solaris.
Advantages of Kernel-level threads
2. The scheduler may decide to spend more CPU time in the process of threads being
large numerical.
3. The kernel-level thread is good for those applications that block the frequency.
Components of Threads
1. Program counter
2. Register set
3. Stack space
Benefits of Threads
o Enhanced throughput of the system: When the process is split into many threads,
and each thread is treated as a job, the number of jobs done in the unit time
increases. That is why the throughput of the system also increases.
o Effective Utilization of Multiprocessor system: When you have more than one
thread in one process, you can schedule more than one thread in more than one
processor.
o Faster context switch: The context switching period between threads is less than the
process context switching. The process context switch means more overhead for the
CPU.
o Responsiveness: When the process is split into several threads, and when a thread
completes its execution, that process can be responded to as soon as possible.
o Resource sharing: Resources can be shared between all threads within a process,
such as code, data, and files. Note: The stack and register cannot be shared between
threads. There is a stack and register for each thread.
Processor Handling : The processor can execute only one instruction at a time, but it
switches between different threads so fast that it gives the illusion of simultaneous
execution.
Thread Synchronization : Each thread is like a separate task within a program. They
share resources and work together smoothly, ensuring programs run efficiently.
Efficient Execution : Threads in a program can run independently or wait for their
turn to process, making programs faster and more responsive.
Lifecycle of a Thread
There are various stages in the lifecycle of a thread. Following are the stages a thread goes
through in its whole life.
New: The lifecycle of a born thread (new thread) starts in this state. It remains in this
state till a program starts.
Waiting : While waiting for another thread to perform a task, the currently running
thread goes into the waiting state and then transitions back again after receiving a
signal from the other thread.
Timed Waiting: A runnable thread enters into this state for a specific time interval
and then transitions back when the time interval expires or the event the thread was
waiting for occurs.
Terminated (Dead) : A thread enters into this state after completing its task.
Drawbacks of Multithreading
Multithreading is complex and many times difficult to handle. It has a few drawbacks. These
are:
If you don’t make use of the locking mechanisms properly, while investigating data
access issues there is a chance of problems arising like data inconsistency and dead-
lock.
If many threads try to access the same data, then there is a chance that the situation
of thread starvation may arise. Resource contention issues are another problem that
can trouble the user.
Display issues may occur if threads lack coordination when displaying data.
Benefits of Multithreading