The amateur programmer
The amateur programmer

Reputation: 1308

Operating system software task scheduling

I was wondering how does the scheduler get control back from the loaded assembly code? For example we could write a program body to which control is passed from the scheduler.

;Do whatever we need to do before calling main()
;Call main or jump to it
;Do cleanup and stuff

So how one can return the program flow from the main back to the scheduler? I have been thinking that process loader could inject jmp instructions to return the program flow back to the scheduler. Any idea could that work?

Upvotes: 1

Views: 201

Answers (4)

Martin James
Martin James

Reputation: 24877

[short version]

OS loaders do not insert any jmp or call code.

On modern preemptive OS, the kernel code is entered from interrupts, either:

'Real' hardware interrupts from peripherals like disk, NIC, KB, mouse, timer that cause a driver to run.

Software interrupts, ie. system calls.

Either type of interrupt may request a scheduler run and change the set of threads running on cores.

If there are no interrupts, the kernel does nothing because it is not entered.

Upvotes: 3

Devolus
Devolus

Reputation: 22104

Multitasking is either implemented preemptive, oder kooperative.

Most OSes uses a preemptive strategy, which mneans that the OS assigns a certain amount of time to a thread, which it is allowed to run. When the timer expires, an interrupt is generated and the OS forcibly switches to a new task (if there are some). This can not really be prevented from user space, unless there are mecahnisms provided.

In a kooperative environment, the process runs as long as it needs to. It explicitly has to call a function which allows the OS to switch to a new task. As long as the process doesn't do this, it can theoretically run forever.

In both cases, it can happen that the task executes a systemcall, which requires the task to wait, and in such a case a task switch can also happen, until the waiting resource call is satsified.

Upvotes: 2

Mikhail Krayushkin
Mikhail Krayushkin

Reputation: 311

OS is interrupted by the timer and if program time is elapsed it calls scheduler that choose next program to execute until next interrupt. If you ask about "how OS know that the program execution is finished" - Program's process make system call when it finish and OS mark that process dead and so scheduler won't choose it for execution.

Upvotes: 1

linluk
linluk

Reputation: 1670

The scheduler doesnt inject jumps or something like that. The os registered it to a interrupt service routine. (isr) of a timerinterrupt of your cpu.

Upvotes: 2

Related Questions