0

Someone told me that Linux can run on CPUs which do not support multiple tasks.

But if you have one core only which doesn't support multitasking, how does Linux implement multitasking?

Let's say I want to run two programs which need 1h to terminate each. How does Linux pause the first program after ~100ms to switch to the second program?

Here are 2 scenarios I can think of:

  • It's not possible to run multiple tasks at once.
  • Linux uses a hardware timer to interrupt running processes.
zomega
  • 972

1 Answers1

5

It’s not clear to me what you mean by a CPU that supports multiple tasks. I guess you’re thinking of multi-core CPUs, but they aren’t a pre-requisite for multitasking; multitasking operating systems pre-date multiple-CPU systems (let alone multi-core CPUs).

However as far as pausing a process to run another is concerned, Linux uses a variety of techniques, including hardware timers (indirectly). Essentially, any time the kernel is invoked, it can decide to change the running process. See the following posts for details:

Stephen Kitt
  • 434,908
  • What is unclear about CPUs not supporting multitasking? I mean what it is. I know that there are single core CPUs supporting multitasking. – zomega Mar 04 '24 at 14:45
  • @zomega what would a CPU not supporting multitasking look like? – Stephen Kitt Mar 04 '24 at 14:47
  • Well very old CPUs for example or micro processors like Arduino. – zomega Mar 04 '24 at 14:48
  • CPUs used to run Arduino support multitasking just fine. What old CPUs don’t support multitasking? – Stephen Kitt Mar 04 '24 at 14:50
  • No, Arduinos don't have multitasking. At least the one I have. – zomega Mar 04 '24 at 14:51
  • @zomega I didn’t say Arduino supports multitasking; I said that the CPUs used to run Arduino support multitasking. – Stephen Kitt Mar 04 '24 at 14:52
  • 2
    @zomega a "task" is an OS concept. a CPU doesn't know what a "task" is. CPU just runs machine code. The only thing that's required from the CPU is a timer interrupts. Without this it cannot run Unix/Linux. When the CPU receives the interrupt, it returns to a specific code at the kernel that would then switch the CPU from one task to another. If the CPU can run an operating system, then by extension it support multitasking. – aviro Mar 04 '24 at 15:12
  • @StephenKitt, is it necessary to name an example CPU to answer the question? – zomega Mar 04 '24 at 16:07
  • @aviro That's completely wrong. On x86 the CPU has a task register (TR) and the instruction LTR stands for load task register. https://cdrdv2.intel.com/v1/dl/getContent/671200 – zomega Mar 04 '24 at 16:07
  • 2
    @zomega that was added to help support tasks on x86 CPUs, but the concept is still an OS concept rather than a CPU concept, and an OS can define tasks without relying on that kind of CPU feature (in fact, task support on x86 turned out to not be a great idea). All multi-tasking (and many single-tasking) operating systems define tasks, but many of them run on CPUs which don’t define tasks at all. – Stephen Kitt Mar 04 '24 at 16:20
  • 1
    @zomega “is it necessary to name an example CPU to answer the question?” — you seem to have one in mind, I’m curious to know what it is. – Stephen Kitt Mar 04 '24 at 16:20
  • 1
    @zomega, pretty much all you need to switch tasks is the ability to cleanly save and replace the CPU registers (including the stack pointer and instruction pointer). Most CPUs have push/pop and call/return/jump instructions, so it's not actually that easy to name one that doesn't support multitasking. It's not too hard to implement switching on an ATmega either. Memory protection of course is an entirely different issue. – ilkkachu Mar 04 '24 at 17:40
  • @zomega It may be necessary to name an example CPU in order to answer the question. The Aruduino software does not support multi-tasking but the underlying ARM processors do. Given that you are asking how Linux manages multi-tasking on CPUs with certain specs, it would really be necessary to discuss those specific CPUs. – doneal24 Mar 04 '24 at 19:48