How does multithreading take place on a computer with a single CPU ?
Share
Talk Programming , Career, Mental Health, Talk Personal Finance ❤️ Post a query and receive responses ✅
Post a query and receive responses. Ask anything, Ask Mitra ❤️
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
you can do multithreading on a single processor system.
In multi-processor system , multiple threads execute , simultaneously on different cores. Eg- If there are two threads and two cores , then each thread would run on individual core.
In a single-processor system, multiple threads execute , one after the other or wait until one thread finishes or is preempted by the OS , depending on the thread priority and the OS policy.But the running threads , gives an illusion that they run simultaneous , relative to the required application response time of the User space application.
Time Comparison(Example):
if two threads take 10us each to execute, then on a 2 processor system , the net time take is 10us
if two threads take 10us each to execute, then on a 1 processor system , the net time take is 20us
The microprocessors back in the 2000s could execute only one thread per core now they have expanded to two.
The threading is said to execute multiple programs at a time. But on the hardware level, there is only one data bus, only one address bus, one memory controller, and the CPU executes only one instruction at a time. The CPU may take a lot of time executing an instruction and holds it during processing and acquiring the next instruction. This switching process is too fast and it seems as running parallel. But it’s not.(PCs at our homes or Vonn Neumann Architecture).
Multithreading shares the same physical location. The threads seem like a process but are different as processes might not share the same physical location.
Multithreading codes are inefficient, hard to manage, and prone to deadlocks and data races. It is better if anyone could avoid multi-threaded patterns for single-threaded ones.
The operating system’s task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.