Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

low level programming: How does the OS start a new thread/process?

Whenever the bootloader loads the operating system there is presumably only ONE program flow active, right? This would mean, one processor holds the instruction pointer and executes the commands it founds at the position the EIP register points to. At which point and how does the system start to exploit more processes and/or threads (no userland threads, but cpu threads)?

like image 363
prinzdezibel Avatar asked Apr 12 '09 13:04

prinzdezibel


1 Answers

The OS will boot (after the BIOS and the bootloader are done) in a special role - as the first program to run it will have direct access to all the CPUs commands.

So it will setup various parts of the system - like setting up Interrupt Handlers (or Interrupt Service Routines). Having done this it has the ability to create a "scheduler".

The actual "process/thread" handling will be done by this scheduler. It decides, which threads will be run. Also it manages all the active threads. The CPU is unaware of all these things.

Once the scheduler's main-executive decides to execute Thread (or "Process") A, it copys the processes data into the registers (and stores the registers into the recently running thread's InfoBlock). It will tell the CPU / a timer to cause an interrupt in n microseconds (or other timeunit). Then it will tell the cpu to run the "program" (the only thing the CPU knows about) in the non-OS mode (so that it may not modify critical data or register own Interrupt Handlers without permission).

While Thread A is executing now, the hardware timer will run. Once it hits the desired time-offset, it will cause an Interrupt. The hardware will then stop execution of the current program, and will invoke the registred Interrupt Handler instead which. This handler will be a method of the scheduler (the main-executive again, to be precise).

This method will then again reevaluate which Thread should be scheduled and so the scheduling continues.

like image 51
Marcel Jackwerth Avatar answered Nov 16 '22 03:11

Marcel Jackwerth