Multitasking and Multithreading: How Operating Systems Run Multiple Tasks

Modern computer running multiple applications through multitasking and multithreading.

Many activities can be performed by modern Computers simultaneously. A user can listen to music, be on the internet, download files, and even edit a file while getting notifications—without having to close one app and then open another. While this might appear to be several programs running at once, the OS is constantly allocating processor time, memory, storage and other hardware resources without you being aware of it. 

To support a number of activities, there are several closely related concepts, such as Multitasking, Multiprocessing, Multithreading, Concurrency, Scheduling, Context Switching, Synchronization, and Parallel Execution. These technologies cooperate to ensure that applications remain responsive, and computers utilize resources as efficiently as possible. It is also helpful to know their differences so that you can appreciate why they can work for any application that spanned from a simple desktop application to a demanding server application.

Computer running multiple applications through multitasking

What Is Multitasking?

Multiple programs or tasks that are able to make progress at the same time with the help of an operating system. In a single processor core based computer, the applications cannot literally execute instructions concurrently on the same processor core. Rather, the operating system quickly alternates among various tasks to be executed by the processor. These switches occur very rapidly; normally the user will perceive the computer as if several applications were running at once. If a browser is playing the music, the OS will use the processor for both the browser and the music program, based on its scheduling rules. The operating system also allocates memory and other resources to ensure that one application doesn’t unnecessarily contaminate another.

Preemptive multitasking is used by most modern operating systems, which allows the operating system to interrupt a process and allow the processor to run another process. This is important because the processor shouldn’t be able to keep indefinitely while other applications wait. A scheduler is used to select which ready task to give processor time to, typically based on its priority, responsiveness, fairness and waiting time. If the processor is waiting for user input or data from storage or from a network, it is able to perform other tasks. Multitasking can thus enhance responsiveness and resource usage by managing a lot of parallel activities.

Multitasking and the Importance of Scheduling

The scheduling is one of the key mechanisms of multitasking, as the processor can only perform a certain amount of work at any given time on each of the cores. The operating system keeps track of tasks that are ready to be run, waiting for resources, or running. This information is used by a scheduler to determine which task will be next to get processor time. While different scheduling strategies can be used in different operating systems and workloads, some shared goals are to minimize wait time, keep response time short, ensure fairness of processor use, and maximize overall throughput. Some interactive applications may need immediate response; some background applications may be able to wait longer. Scheduling enables the OS to deal with these conflicting needs instead of executing all tasks in the same manner.

The scheduler does not just pick applications in a certain order and then run them forever. Rather, the OS gives each task a share of processor time, and it is possible for the OS to preempt one task when needed. For instance, a timer interrupt may indicate to the operating system that a certain amount of time has elapsed. The scheduler can then decide if another task should be executed. The order of priorities could also be a deciding factor, as it may be appropriate to do some things first, if they are more urgent. This ongoing coordination is hidden from the user, but it is necessary for the responsive actions that are expected of a modern computer. If an application is scheduled to use a processor-intensive function, with no appropriate scheduling, another program can use the processors and make the other program appear frozen, even though it is not.

Operating system CPU scheduling multiple tasks

How to Break Up Work in an Application

A related concept is multithreading, which works inside of a process. A process is an executing program with its own resources and address space; threads are smaller execution units within the process. A single application may have several threads to execute various components of its work. For instance, in a graphical application, one thread could handle the input from the operator, another could handle the data processing, and another could handle background processing. Separating work into threads can allow an application to remain responsive while other operations are taking place. The threads of a process typically will share resources like memory, allowing them to communicate efficiently, but also can cause synchronization problems that need to be handled by the developer.

Multithreading allows an application to run multiple threads

In case of the above, multithreading refers to the simultaneous execution of more than one thread in a process/processor environment. If it is possible to get different components of an application to advance without having to wait for all components to finish sequentially, multithreading can enhance application responsiveness. But if multiple threads are created, this does not necessarily imply that all these threads will execute at the same time. Even on a single-core processor, the OS needs to swap between runnable threads. Each thread may be able to run concurrently on a different core of a multicore processor. The actual behaviour is thus a function of the software design and of the resources that the hardware provides to the system.

What Is Multiprocessing?

Multiprocessing is running work on several processors or processor cores. Many modern computers have CPUs that have multiple cores, each of which can run instructions separately. This is hardware which allows tasks to be executed in parallel with the possibility of different cores executing different tasks simultaneously. In the case of a 4-core computer, for instance, the operating system can assign appropriate workloads to the four cores instead of sending all of them through any one execution unit. The operating system keeps track of the processing resources it has and shares them among tasks that can be processed based on the operating system’s scheduling algorithm. Therefore, multiprocessing can deliver more power than a single processor system, particularly for applications which can be broken down and executed independently on each processor.

Multitasking and multiprocessing are similar, but not synonymous. Multitasking refers to the operating system’s capacity to run several tasks concurrently in the same time frame, and multiprocessing is related to the presence and utilization of several processing units. A single-core computer can multitask with fast switching between operations and a multicore computer can multitask and can also perform parallel operations. The OS can run multiple processes or threads on a multicore system, and still switch tasks as workloads change. This distinction is important because a computer might seem to be running a lot of processes simultaneously while some of the processes are actually running on the same core, and other processes are actually running on different cores.

Concurrency Versus Parallelism

Concurrency is a system’s capacity to run several processes that advance concurrently, but not simultaneously. Concurrency can be achieved by the single processor by rapidly switching between tasks. Assume a task is waiting for data from a network and another task is ready to do the calculation. The operating system could permit the second task to run while the network request is being processed. These two activities are running concurrently in the same time frame, even though the processor is executing their instructions at different times. Concurrency is particularly valuable in interactive programs and systems which regularly wait for external operations.

Unlike parallelism it is a case of many operations running in parallel. True parallel processing requires having multiple threads or processes running in parallel on a multicore processor. This can drastically cut down the time needed for workloads which can be split up into independent pieces. For example, a large data-processing job could be divided in several portions, each portion processed by a different processor core. Not all workloads are created equal, however, when it comes to parallelism. Some operations are reliant on the outcome of others, and some operations need regular interactions between threads. To make parallel software effective, it is necessary for it to be well-designed to identify which portions of a workload can be executed safely and effectively in parallel.

Context Switching

The switching of the processor’s current execution between two tasks or two threads is called context switching. The operating system must save important information concerning the current task before switching over to another one that will run. This information can contain the registers of the processor, the program counter and other processor execution state information. The operating system then resumes the state of another task that was suspended to resume from where it left off. This process takes place very rapidly and the individual switches are not usually perceptible to the user. But, switching contexts uses processor time and other system resources and can make things less efficient if you switch too many times.

Switching between tasks has to be balanced with responsiveness of the operating system. If the context switching is very frequent, then interactive applications can respond rapidly, since no single task will have to wait for too long for the processor to be available. However, if the system switches too rapidly, it may be wasting a lot of processing time dealing with switching and not much else. The scheduling policies, priorities, interrupts and other such features in the operating system are used to decide when it is appropriate to switch. The challenge becomes more complex if many applications each have more than one thread: the scheduler can have many more runnable execution units to manage, and across all the processor cores.

Thread Synchronization

If two or more threads use the same data, they may interfere with each other if they access or update the same data at the wrong time. To coordinate these activities and to ensure consistent data, thread synchronization offers mechanisms to coordinate them. A common issue is the race condition, which is an issue where the final result is determined by the random timing of several threads accessing shared data. For instance, two threads may try to modify the same variable at this time, and the modification by one thread may be overwritten by another without proper coordination in the program. Synchronization mechanisms are used to manage access to shared resources and to guarantee that the operations are executed in a secure and predictable sequence.

Typically, the same synchronization methods are used as in the case of mutexes, locks, semaphores, condition variables, etc., which are other methods of coordination. A mutex could be used to ensure that there is only one thread modifying a shared piece of code at a time. A semaphore can be used to coordinate access to some limited resource, and can be a useful construct if multiple threads must be given controlled access to some pool of resources. Nevertheless, it is important to design synchronization carefully. If a locking is not properly designed, it can result in deadlocks, in which two or more threads are waiting for each other to release resources that they can’t release because they are waiting for one another. But, overly synchronized can decrease performance as threads may wait too long for other threads to finish their tasks, instead of doing something useful.

Multicore processor executing tasks in parallel

The Way Operating Systems Allocate Memory in the Process of Running Multiple Tasks

Processor scheduling is just one aspect of multitasking. The operating system must also manage memory – several processes and threads have to have memory where they store instructions and data. Normally each process runs in its own virtual address space, thereby isolating each application. Virtual memory enables the operating system to provide programs with an abstraction of memory — one that is larger and/or structured differently than the computer’s physical RAM. Operating system controls mapping of virtual memory to physical memory, and memory protection mechanisms assist in preventing the direct interference of one process with another process’s protected memory.

When multiple applications are running concurrently at the same time, the management of memory becomes critical. If physical RAM is fully used up, the operating system may be required to control what information is easily accessible in RAM and what information can be transferred to secondary storage. Memory stress may cause the storage to become more active and increase storage response time lag, as storage is typically slower than RAM. The efficient use of memory therefore complements scheduling to keep the system efficient. If a computer has a large processor speed but not enough memory, it can get slow if a lot of programs are being used concurrently. Multi-tasking is then a coordinated activity, CPU scheduling, memory management, storage operations and controlling hardware resources.

Programming for Parallel Execution on Multicore Systems

Parallel execution means that various sections of a workload can be executed concurrently on multiple processor cores. Applications that execute in parallel can break down large problems into smaller pieces that can be executed separately. For instance, image processing, scientific calculations, simulations, video processing, and certain types of data analysis can easily be broken down into smaller chunks of work. The resulting threads or processes are executed by the operating system on available CPU cores. If the workload is parallelizable, each of the cores may be able to process parts of the task at the same time, which might lead to better performance than if they were all working on the same task one by one on a single core.

Thread synchronization and context switching in operating systems

There are challenges also when using parallel execution as not all parts of a program are suitable for parallelization. Some operations must be carried out in a specific order because the calculation for one operation is based on the result of the other. Shared data may be needed for other operations, thus necessitating the synchronisation of threads. There is also a cost to the communication between parallel tasks, and if the work is not divided evenly, some of the processor cores will end up working while others are not. Thus, adding more threads does not necessarily correspond to better proportional performance improvement. A good parallel application needs to carefully divide the workload, synchronize data, manage communication and take into account the architecture of the computer systems on which the application will be executed.

Multitasking Is Compared to Multithreading and Multiprocessing

Multitasking vs Multithreading vs Multiprocessing

Multitasking, multithreading and multiprocessing are three levels of computer support for multiple activities. Multitasking is mainly a concept of operating system that is concerned with the management of multiple processes or tasks to enable them to advance in the same time. Multithreading deals with multiple threads of execution inside a process that enable an application to split up its work into smaller chunks. Multiprocessing is the use of a number of processors / CPU cores which can perform work independently and concurrently. Concurrency is the overlap of progress by tasks, and parallelism refers to actually running tasks simultaneously. These ideas are often combined in contemporary systems. In a multicore computer, multiple processes can be executed at the same time, each of them can have multiple threads, and the operating system can allocate the threads to different processor cores.

Knowing these differences helps to better understand how the user’s interaction effects as they open multiple applications. The operating system can have several processes, every one of which has one or more threads. Scheduler decides which runnable threads get processor time, and memory management provides processes with their resources. With a multicore CPU, multiple threads can run concurrently, and other threads can remain waiting for the CPU to become available or for an operation, like access to storage or network input or output. Context switching is used to switch between runnable tasks, and synchronization is used to avoid corruption of shared data by threads. This creates a coordinated system where numerous independent and related activities can proceed without having to run sequentially.

Why Are These Technologies Important for Today’s Applications?

Because applications are required to run many different operations simultaneously, the multi-tasking, multithreading, concurrency and multiprocessing have made them vital to modern computing. A web browser can process user actions, draw graphics, access the Internet, play media, and run background tasks simultaneously. Video editor may process frames while showing a preview and taking user commands. A server can serve requests from numerous users and interact with different services, while executing database operations. These workloads involve the need to partition the work and to schedule the resources and coordinate shared information effectively between software and operating systems. If these mechanisms were not in place, applications would be less responsive and computers would be unable to make efficient use of today’s multi-core hardware.

The technologies also reveal that “simultaneous activity” is not a one-way technology but a set of carefully orchestrated technologies. Rapid scheduling and context switching can give the illusion of concurrent execution on a single-core processor. Multicore processors can run multiple cores in parallel, multitasking and concurrency. In each application, there may be many threads sharing their work and thus they may need to coordinate their activities to avoid conflict, but there may also be many threads handling the same resources, which can be coordinated by dividing their responsibilities so they are not all handling the same resources. Process isolation, memory management, scheduling policies and hardware support complement these mechanisms in the development of an environment that can support multiple activities. This structure gives a better understanding of how modern computers are able to remain responsive when undertaking complex workloads.

Conclusion

The term multitasking and multithreading refer to basic technologies that enable the modern computer to perform many tasks efficiently. Multitasking provides an operating system with the ability to run multiple processes and applications, and multithreading facilitates a single application to break its tasks into multiple execution units. Multiprocessing gives multiple processor cores which can run multiple different workloads concurrently, and concurrency states that multiple tasks can make progress over overlapping time periods. 

Context switching allows the OS to switch between tasks, scheduling decides what to do and synchronization ensures consistency of shared data when multiple threads operate on it. These mechanisms enable the computer to continue functioning even when it is running tasks that require a lot of computing power. The operating system is constantly juggling processors, memory, threads, processes and other resources to keep users happy during simultaneous activity, but that’s what it does.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
0
Would love your thoughts, please comment.x
()
x