Thread In Linux
In this tutorial, I will explain about thread which is an important topic in Linux. You must have heard about in multithreading in Linux. I will explain what the thread is and how it works. Let’s take the same example which I explained in tutorial Process in Linux.
Fig. 1: Image showing Threads in Linux
If I want to make my passport but I don’t know what the process is, I will go to the inquiry window and ask about it. Process is nothing but carrying out a task. My task is to make passport and process is how to make passport. Now you must be wondering as to where the thread is. The process of making a password is more typical and time consuming if only one member or officer is working on it. In other words if the verification of documents in a passport office is handled by only one member, it will take more time because he or she will verify the documents of users in a sequential approach. However if there are more than one member, then it will take less time because user has divided per member in parallel approach. Handling of user per member is nothing but same as thread. So, simply thread is part of process which works along with process.
Thread is similar to process. We can say that it is a light weight processwhich shares the same address space, data and attribute of process. It is single sequence stream within a process similar to a mathematical or some small function of code which runs within process. Before learning about thread, you should know about Process. You can refer to the Process in Linux tutorial.
Fig. 2: Image showing Life Cycle of Linux Processes
Processes are used to group resources together and heavy weight task. Threads are entities scheduled for execution on CPU. It is a popular way to improve application through parallel approach. CPU switches back and forth between threads rapidly so as to switch of the thread working in parallel. Each thread has its own stack, PC and several registers. These share their data with another thread within the same process and are not independent like a process.
Process Vs Thread
Fig. 3: Image showing Prcesses in Context to Threads
Thread is similar but somehow different to a process. Similarity between process and thread are following:
- Like process, thread shares CPU
- Only one thread is active at a time same as process.
- Thread can also create children like process.
- One thread is blocked when another thread runs like process.
Difference between process and thread are as follows:
- Threads are not independent unlike process
- Thread can access only address in task
Need of Thread
We already know that process and thread are mostly similar. So, why do we use thread in application? Process with multithread is very useful in great server. Switching between processes is slower than thread. Thread uses very light weight resource and context switching fast. It can share common data without the need of IPC. Two or more threads are communicated to each other without pipe or FIFO. Multithreading technique is helpful to develop effective application.
Types of Thread
- User Thread
- Kernel Thread
1. User Level Thread
User thread works at user level. It has no need of system call and hence does not need to call OS. User thread is managed by application and thread library. Kernel does not know anything about user thread. User thread has following advantages:
- No need to modify OS.
- Each thread is represented by small control block, stack, PC, register and all are stored in same process address space.
- Creating, switching, synchronization of thread is simple without intervention of kernel. User thread is not much expensive.
User thread also has following disadvantages:
- Lack of coordination between user thread and kernel
- It does not provide the protection of thread
2. Kernel Thread
It manages thread and s also schedules it. Kernel threads have thread table which keeps track of all thread in system same as process. It has full knowledge of thread but at the same time it is very slow and inefficient. It increases the kernel’s complexity and requires full thread control block.
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.