Greetings!We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at email@example.com.
In this post, we will go through introduction to threads in operating system. We will see – what is thread?, why do we use it?, Difference between thread and process, advantages and disadvantages of threads in operating system etc.
What is Thread?
Thread is a smallest unit of process. It always exist within a process. A single process can contain multiple threads.
Use of Thread in a Process
Suppose you need to do work on printing, scanning and editing a document. So, in order to accomplish these task, you have to create 3 process for each work item. Then, there will be context switching between them. Since only one process can execute at a time, you will have to hold other processes for some time. For example – when you are doing scanning, then, you will have to hold printing and editing.
Here comes the role of thread. So, what you will do is – create one process and this process will create threads. There will be multiple threads for printing, scanning and editing. Thus, single process can do multiple action using thread and no need to hold process.
A thread is also called a light weight process.
- In one process, there will be multiple threads.
- Threads can share resources communication and data with each other.
- Threads provide a way to improve application performance through sharing resources. Threads don’t exist outside any process.
- Threads represent separate flow of control.
Difference Between Thread and Process
Below are the differences between process and thread –
|1.||Process is a program is in execution.||A thread is a light weight process.|
|2.||Processes run in separate memory spaces.||Thread in a single process shared memory space with each other.|
|3.||Process contain multiple threads.||Thread can’t contain processes.|
|4.||It takes more time for creation.||It takes less time for creation.|
|5.||Process can exist individually.||Threads can’t exist individually. It is attached to a process.|
Process With Single Thread
– A process with single thread share data section, code section, register for store temporary data, counter, stack and code.
– Single process with multiple threads share code section and data section.
Process With Multiple Threads
In data section, global variables are shared among threads. In some cases, code section is not shareable. Apart from these section, each thread maintain their own stack. Local variables are stored on the stack. Each thread has its own register that are loaded when thread is active and saved. Each thread also maintains own program counter which helps to execute next instruction.
Advantages of Threads
- When we are working with threads, context switching will be fast. The reason is that we have to save and/or restore SP, PC and registers.
- A process with multiple threads generate a great server like printer server. Because threads share common data. They don’t need to IPC (interprocess communication).
- Threads need only stack and register for storage. Therefore, threads are cheap to create.
Disadvantages of Threads
- Multiple threads can interfere with each other when sharing hardware resources like caches.
- Threads use many library functions. If one thread crash, whole application will crash. It is
totally depend on operating system.
- Threads, if executes serially, may increase time complexity.
That’s end of tutorial on introduction to threads in operating system.