Its quite easy to get confused between multitasking and multithreading specially for application level developers who have never worked with low-level languages like C, C++ or have never worked on applications which require complex processing.
Here I have attempted to explain both in a simple manner. This explanation is independent of any particular platform or operating system. First of all, we need to define our terms.
Task – A Task can be understood as a single task or a process or program executing on a computer. A Task comprises of
- Its own memory address space in which the code and data of the task are stored
- System resources like files and devices
- At least one thread which executes the code
Thread – A thread is a single sequential flow of control within a program. A thread is a sequence of executing instructions that can run independently of other threads yet can directly share data with other threads. A Thread comprises of
- A processor state including current instruction pointer
- A stack to store current process state/data
So how are Tasks and Threads related to each other? The figure below explains this.
What it means is that a computer can run various tasks and each task may have multiple threads within it.
MULTITASKING AND MULTITHREADING
Multiple tasks share common system resources. With a multitasking OS, you can simultaneously run multiple applications. Multitasking refers to the ability of the OS to quickly switch between each computing task to give the impression the different applications are executing multiple actions simultaneously.
Multitasking is the ability of a computer to run more than one program, or task , at the same time. Multitasking contrasts with single-tasking, where one process must entirely finish before another can begin. On a single-processor multitasking system, multiple processes don’t actually run at the same time since there’s only one processor. Instead, the processor switches among the processes that are active at any given time. Because computers are so fast compared with people, however, it appears to the user as though the computer is executing all of the tasks at once. Multitasking also allows the computer to make good use of the time it would otherwise spend waiting for I/O devices and user input–that time can be used for some other task that doesn’t need I/O at the moment.
Multitasking on a multiple-processor system still involves the processors switching between tasks because there are almost always more tasks to run than there are processors. Note, however, that there can be as many tasks running simultaneously as there are processors in the system.
Extends the concept of multitasking into applications, so you can subdivide specific operations
within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different tasks, but also among each thread within a task.
Multithreading provides a way to have more than one thread executing in the same process while allowing every thread access to the same memory address space. This allows very fast communication among threads. Threads are also easier to create than processes since they don’t require a separate address space.
Inter-process or inter-task communication is very expensive and context switching from one process to another process is costly since they are running in different address spaces. In the case of multithreading, each thread is a lightweight process and can share same address space and interthread communication overhead is less.
WHAT IS CONTEXT SWITCHING?
At any given time, a processor (CPU) is executing in a specific context. This context is made up of the contents of its registers and the memory (including stack, data, and code) that it is addressing. When the processor needs to switch to a different task, it must save its current context (so it can later restore the context and continue execution where it left off) and switch to the context of the new task. This process is called context switching .