Saran-san
Saran-san

Reputation: 360

Process Scheduling from Processor point of view

I understand that the scheduling is done by the kernel. Let us suppose a process (P1) in Linux is currently executing on the processor. Since the current process doesn't know anything about the time slice and the kernel is currently not executing on the processor, how does the kernel schedule the next process to execute?

Is there some kind of interrupt to tell the processor to switch to execute the kernel or any other mechanism for the purpose?

Upvotes: 3

Views: 881

Answers (2)

rakib_
rakib_

Reputation: 142685

Few things could happen -

   a. The current process (p1) can finish up its timeslice and then the
   scheduler will check is there is any other process that could be run. 
   If there's no other process, the scheduler will put itself in the
   idle state. The scheduler will assign p1 to the CPU if p1 is a CPU hoggy
   task or p1 didn't leave the CPU voluntarily.

   b. Another possibility is - a high priority task has jumped in. On every
   scheduler tick, the scheduler will check if there's any process which
   needs the CPU badly and is likely to preempt the current task.

In other words, a process can leave the CPU in two ways - voluntarily or involuntarily. In the first case, the process puts itself to sleep and therefore releases the CPU (case a). In the other case, a process has been preempted with a higher priority task.

(Note: This answer is based on the CFS task scheduler of the current Linux kernel)

Upvotes: 1

Arun
Arun

Reputation: 2092

In brief, it is an interrupt which gives control back to the kernel. The interrupt may appear due to any reason. Most of the times the kernel gets control due to timer interrupt, or a key-press interrupt might wake-up the kernel. Interrupt informing completion of IO with peripheral systems or virtually anything that changes the system state may wake-up the kernel.

More about interrupts:

Interrupts as such are divided into top-half and bottom half. Bottom Halves are for deferring work from interrupt context.

Top-half: runs with interrupts disabled hence should be superfast, relinquish the CPU as soon as possible, usually

1) stores interrupt state flag and disables the interrupts(reset
some pin on the processor),    
2) communicates with the hardware, stores state information,
delegates remaining responsibility to bottom-half,     
3) restores the interrupt state flag and enables the interrupt((set
some pin on the processor).

Bottom-half: Handles the deferred work(delegated work by the top-half) runs with interrupts enabled hence may take a while before completion.

Two mechanisms are used to implement bottom-half processing.

1) Tasklets    
2) Work queues

.

If timer is the interrupt to switch back to kernel, is the interrupt a hardware interrupt???

The timer interrupt of interest under our context of discussion is the hardware timer interrupt,

Inside kernel, the word timer interrupt may either mean (architecture-dependent) hardware timer interrupts or software timer interrupts.

Read this for a brief overview.

More about timers

Remeber "Timers" are an advanced topic, difficult to comprehend.

is the interrupt a hardware interrupt??? if it is a hardware interrupt, what is the frequency of the timer?

Read Chapter 10. Timers and Time Management

if the interval of the timer is shorter than time slice, will kernel give the CPU back the same process, which was running early?

It depends upon many factors for ex: the sheduler being used, load on the system, process priorities, things like that. The most popular CFS doesn't really depend upon the notion of time slice for preemption! The next suitable process as picked up by CFS will get the CPU time.

The relation between timer ticks, time-slice and context switching is not so straight-forward.

Each process has its own (dynamically calculated) time slice. The kernel keeps track of the time slice used by the process.

On SMP, the CPU specific activities such as monitoring the execution time of the currently running process is done by the interrupts raised by the local APIC timer. The local APIC timer sends an interrupt only to its processor.

However, the default time slice is defined in include/linux/sched/rt.h

Read this.

Upvotes: 6

Related Questions