Multiprocessing and multithreading are core concepts of computer multitasking. First of all, computer multitasking means that a computer system enables multiple (and generally different) tasks to execute concurrently over a certain period. Processes and threads are execution instances of these tasks. However, besides having several similarities, processes and threads are technically different.
In this tutorial, we’ll have an introduction to multiprocessing and multithreading. First, we’ll have a review of threads and processes, observing how they relate to each other. Next, we’ll study the general concepts of multiprocessing and multithreading. Finally, we’ll investigate how multiprocessing and multithreading technically occur in the context of a computer system.
2. A Review of Processes and Threads
In general, we can see processes as instances of programs that execute in a computer system. Hence, computer processes have their own isolated sections of code and data. In other words, a particular computer process does not directly interfere neither in the code execution nor in the data resources of another process.
Threads are lightweight semi-processes that depend on a process. Specifically, a thread executes a task in the context of a process. In technical terms, threads demand similar resources to a process, such as a memory stack, program counter, and set of registers. These resources are particular to each thread. However, all the threads of the same process share the process’ code and data sections.
Regarding communication, processes are categorized as independent or cooperating. Independent processes don’t communicate with other processes and aren’t affected by them. Cooperating processes, in turn, exchange data with other processes during their execution. Since processes are isolated programs, data exchanging between them relies on inter-process communication mechanisms.
Threads may also need to communicate with each other. However, unlike processes, threads belonging to the same process don’t need additional mechanisms to establish inter-thread communication. In this scenario, messages are exchanged through the process’ data section.
The following figure briefly demonstrates the relationship between processes and threads:
In summary, despite being different entities, processes and threads are closely related. For instance, every process has at least one thread that executes instructions in the CPU. Threads, in turn, can only exist within a process.
3. Multiprocessing and Multithreading Basics
Multiprocessing and multithreading tackle the concurrent execution of multiple instances of, respectively, processes and threads. Thus, they are responsible for implementing multitasking in computer systems. Let’s see definitions of multiprocessing and multithreading:
- Multiprocessing refers to the possibility of simultaneous execution of more than one process in a computer system. It is relevant to note that multiprocessing systems demand multiple physical processing units in the computer system. In this way, different processes can execute at the same time.
- Multithreading refers to the possibility of executing heterogeneous tasks of a particular process concurrently. In practice, multithreading occurs when at least one process with multiple threads executes in a computer system. So, multithreading does not require multiple physical processing units, as multiprocessing does. However, the processing time of a processing unit must be assigned to the running threads according to a specific strategy.
We can analyze multiprocessing and multithreading with concrete examples. Currently, modern personal computers typically have two or more processing units, which allows multiprocessing. So, for example, we can run, at the same time, a web browser and text editor. However, in fact, we execute much more processes than the available processing units. In this case, the processes share the processing time of these units.
Observing our programs (processes), we can note that they generally execute multiple tasks concurrently. Let’s take the text editor as an example. The editor’s graphical interface is a thread; there is another thread to catch the letters typed by the users; another to save the document; and probably others that we don’t realize just using the editor. So, these multiple threads must share the time of the processing units and usually don’t execute all the time at the same time.
4. Technical Details of Multiprocessing and Multithreading
The processing units (hardware) and the operating system (software) are the main enablers of multiprocessing and multithreading. As we saw in the previous sections, the implementation of multiprocessing requires more than one processing unit. Furthermore, we need a multitasking operating system to manage multiple threads and processes. There are different categories of multitasking operational systems, but preemptive multitasking systems are usually employed.
The scheduler is a key component of a multitasking operating system. This component determines which process/thread will execute at a specific time. Another component, the dispatcher, manages the execution time slices of the processing units. In preemptive systems, the end of the time slice interrupts the execution of a process or thread.
A multitasking operating system can schedule threads of a process (kernel threads) or schedule a process that, in turn, manages the execution of its own threads (user threads). In the latter case, the scheduler is unaware of user threads, and the process must employ a thread library to coordinate their execution.
Finally, several functional characteristics of processing units and multitasking operating systems can impact the performance of multithreading and multiprocessing. Examples are the total number of physical processing units and physical threads available in these units, the operating system strategy to determine process priorities, and the operating system approach to deal with the blocked processes.
In this article, we studied multiprocessing and multithreading. First, we reviewed basic concepts of processes and threads. Thus, we analyzed the basic concepts regarding multiprocessing and multithreading. Finally, we outlined technical details on how modern computer systems implement multiprocessing and multithreading.
We can conclude that multiprocessing and multithreading are conceptually different and functionally independent. However, these techniques are complementary in the sense of improving computer systems’ performance through multitasking.