Hello readers!! Hope you are doing well. Today we shall learn about threads. Just like a cotton thread which you use to stitch clothes threads in computer science is one of the indispensible phenomenon about which you should be aware of. Let us not waste time and learn threads.
Thread can be understood as a process is a program which executes a single thread of instruction. We can say single thread of control allows the process to perform only one task at one time. For example when we run a word processor we can say a process is running word processor which is a program and it is under control o a single thread of execution. We cannot type numbers and characters at same time and run the spell checker concurrently or simultaneously. Modern operating systems have modified the process concept to allow a process to have multiple threads of execution so that more than one task can be accomplished at the same time.
A thread comprises a thread ID, a program counter, a register set, and a stack. Thread has a sharing nature hence it can share its code section, data section, and other operating-system resources, such as open files and signals with other threads belonging to same process. Thread is considered to be a basic unit of CPU utilization.
Processes are known as traditional or heavy-weight by this we mean that process has a single thread of execution. When a process can support multiple threads it can perform multiple tasks.
Figure single threaded process and multithreaded process
There is a popular concept in programmers’ world called multithreaded programming. A multithreaded program is one which contains more than one part which can run concurrently. Each part of this type of program is called a thread. A thread defines a separate path of execution. So we can say multithreaded program is a synonym of multitasking. By multitasking we mean accomplishment of more than one task for example in your windows operating system you can listen to audio songs and program in android at the same time. Now multitasking can be broadly categorized into two types:
- Process-based multitasking
- Thread-based multitasking
We already know about processes, isn’t it!! Process-based multitasking allows two or more programs to execute concurrently as we all know process is a program in execution. A program is the smallest unit of code which can be dispatched by the scheduler in process-based multitasking.
Thread based multi-tasking implies to an environment where thread is the smallest unit of code which can be dispatched. This implies that a single program can perform more than one tasks at a time. Thread-based multitasking handles small details in a code.
Multitasking threads requires less overhead with respect to multitasking processes. Processes require their own addresses spaces. When we talk about interprocess communication it becomes limited and expensive. Context switching in between processes becomes unaffordable at times. Threads are considered as light weight as they share same address space and switching from one thread to next is less expensive.
With multithreading we can write impressive and efficient programs. They make the best use of CPU as idle time can be kept minimum. When we talk about networking this is important as idle time is very common in such environment for example rate of transmission of data packets over network might be fast slow as compared to the rate at which computer can process it.
In single threaded environment program has to wait for each of the task to complete before it can proceed to next one even though CPU is idle. With multithreading we can gain access to idle time and make use of it.
12.2.3 Models of Multithreading
Support for threads can be provided at two levels, namely-
- Kernel level
User threads are the one which are supported above the kernel and are managed without kernel support, Kernel threads are supported and managed directly by the operating system. Operating systems like Windows XP, Linux, Mac OS X, Solaris, and Tru64UNIX support kernel threads. Relationship status between user threads and kernel threads gives rise to three models and they are-
- Many-to-one model
- One-to-one model
- Many-to-many model
Let us have a brief idea about these models
18.104.22.168 Many-to-one model
In this model many user threads are supported by one kernel thread. Thread is managed by thread library in user space. It is efficient. But if one thread makes a blocking system call then entire process is blocked. Only one thread can access kernel hence so multiple threads cannot run on a multiprocessor parallel as it is supposed to be.
Figure many to one model
22.214.171.124 One to one model
In this model each user thread is mapped by one kernel thread. This model allows multiple threads to run in parallel on multiprocessors. This model provides more concurrency as if any one of the makes a system blocking call the entire process is not hampered from continuing its task. Creating one user thread corresponding to each kernel thread is an overhead for system. As creating an kernel thread can create overhead for application and in turn it will hamper the performance of application as a whole. The one-to-one model allows for greater concurrency. But the developer has to be careful not to create too many threads within an application.
Figure One to one model
12.2.3..3 Many to many model
In this model there are many user threads with respect to equal or smaller number of kernel threads. Number of kernel threads may be specific to either a particular application or a particular machine. This model allows you to create as many as user threads but true concurrency is not achieved as kernel can schedule only on kernel thread at a time. Developers are allowed to create as many user threads as necessary The corresponding kernel threads can run in parallel on a multiprocessor. If a thread performs a blocking system call then operating system can issue another kernel thread for execution.
Figure many to many model
Congratulations guysJ!! We are done with this section. Hope you learned something interesting and useful. See you in the next section. Till then keep practicing. Happy App developing!!!