<br> [MAS.865](../../index.html) > [Programming](../index.html) > Multitasking ##Multitasking Most operating systems appear to allow multiple programs or threads to execute at the same time. This is called multi-tasking. In reality, each processor core can only be running a single program at any given point in time. A part of the operating system called the scheduler is responsible for deciding which program to run when, and provides the illusion of simultaneous execution by rapidly switching between each program.<br> ####Threads A thread of execution is a small sequence of instructions managed by an operating system.<br> Threads share the process's resources, but are able to execute independently.<br> An issue can emerge when an illegal operation performed by a thread crashes the entire process.<br> Normally threads allow for faster execution, responsiveness and parallelization of subtasks.<br> ####Multithreading Is the ability of a CPU to execute multiple processes at the same time.<br> Is mainly found in multitasking operating systems.<br> Systems with a single processor generally implement multithreading by time slicing where the CPU switches between different software threads.<br> Systems with multiple processors can execute threads in parallel, with every processor executing a separate thread simultaneously.<br> <p align="center"> <img src="CPU-and-GPU.png" width="50%" height="50%"> <br> </p> ##Events Multitasking systems must manage sharing data and hardware resources among multiple tasks. It is usually unsafe for two tasks to access the same specific data or hardware resource simultaneously. In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected. In embedded systems the same may be achieved using hardware interrupts instead of a constantly running main loop. <p align="center"> <img src="event-driven.jpg" width="50%" height="50%"> <br> </p>