Annotation of the original Linux kernel code sched. c

Source: Internet
Author: User
Comments of the original Linux kernel code sched. c-Linux general technology-Linux programming and kernel information. The following is a detailed description. /*
* 'Sched. c' is the main kernel file. It contains scheduling primitives
* (Sleep_on, wakeup, schedule etc) as well as a number of simple system
* Call functions (type getpid (), which just extracts a field from

* Current-task
*/
# Include
# Include
# Include
# Include
# Include
# Include
# Include

# Define LATCH (1193180/HZ)

Extern void mem_use (void );

Extern int timer_interrupt (void );
Extern int system_call (void );

Union task_union {
Struct task_struct task;
Char stack [PAGE_SIZE];
};

Static union task_union init_task = {INIT_TASK ,};

Long volatile jiffies = 0;
Long startup_time = 0;
Struct task_struct * current = & (init_task.task), * last_task_used_math =
NULL;

Struct task_struct * task [NR_TASKS] ={& (init_task.task ),};

Long user_stack [PAGE_SIZE> 2];

Struct {
Long *;
Short B;
} Stack_start = {& user_stack [PAGE_SIZE> 2], 0x10 };
/*
* 'Math _ state_restore () 'saves the current math information in
* Old math state array, and gets the new ones from the current task
*/
Void math_state_restore () @ coprocessor status save
{
If (last_task_used_math)
_ Asm _ ("fnsave % 0": "m" (last_task_used_math-> tss. i387 ));
If (current-> used_math)
_ Asm _ ("frstor % 0": "m" (current-> tss. i387 ));
Else {
_ Asm _ ("fninit "::);
Current-> used_math = 1;
}
Last_task_used_math = current;
}

/*
* 'Schedule () 'is the scheduler function. This is good code! There
* Probably won't be any reason to change this, as it shocould work well
* In all circumstances (ie gives IO-bound processes good response etc ).

* The one thing you might take a look at is the signal-handler code
Here.
*
* NOTE !! Task 0 is the 'idle' task, which gets called when no other
* Tasks can run. It can not be killed, and it cannot sleep. The 'state'

* Information in task [0] is never used.
*/
Void schedule (void)
{
Int I, next, c;
Struct task_struct ** p;

/* Check alarm, wake up any interruptible tasks that have got a signal
*/

For (p = & LAST_TASK; p> & FIRST_TASK; -- p)
If (* p ){
If (* p)-> alarm & (* p)-> alarm signal | = (1 alarm = 0;
}
If (* p)-> signal & (* p)-> state = TASK_INTERRUPTIBLE)
(* P)-> state = TASK_RUNNING;
}
@ How does task 1 change to TASK_RUNNING ?? How to get signal, how to change alarm to non-0 and state = TASK_RUNNING & (* p)-> counter> c)
C = (* p)-> counter, next = I;
}
If (c) break; @ the count is greater than zero
For (p = & LAST_TASK; p> & FIRST_TASK; -- p)
If (* p)
(* P)-> counter = (* p)-> counter> 1) +
(* P)-> priority;
}
Switch_to (next );
}

Int sys_pause (void)
{
Current-> state = TASK_INTERRUPTIBLE; @ the task can be interrupted
Schedule ();
Return 0;
}

Void sleep_on (struct task_struct ** p)
{
Struct task_struct * tmp;

If (! P)
Return;
If (current ==& (init_task.task ))
Panic ("task [0] trying to sleep ");
Tmp = * p;
* P = current;
Current-> state = TASK_UNINTERRUPTIBLE;
Schedule ();
If (tmp) @ activate p. When will I return? Wake up the last sleep process
Tmp-> state = 0;
}

Void interruptible_sleep_on (struct task_struct ** p)
{
Struct task_struct * tmp;

If (! P)
Return;
If (current ==& (init_task.task ))
Panic ("task [0] trying to sleep ");
Tmp = * p;
* P = current;
Repeat: current-> state = TASK_INTERRUPTIBLE;
Schedule ();
If (* p & * p! = Current ){
(** P). state = 0;
Goto repeat;
}
@ Seems to be unavailable
* P = NULL;
If (tmp)
Tmp-> state = 0;
}

Void wake_up (struct task_struct ** p)
{
If (p & * p ){
(** P). state = 0; @ wake up the process running
* P = NULL; @ sleep stack is 0
}
}

Void do_timer (long cpl) @ scheduled Scheduling
{
If (cpl)
Current-> utime ++; @
Else
Current-> stime ++; @ system state time plus one
If (-- current-> counter)> 0) return; @ current count minus one
Current-> counter = 0;
If (! Cpl) return;
Schedule ();
}

Int sys_alarm (long seconds)
{
Current-> alarm = (seconds> 0 )? (Jiffies + HZ * seconds): 0;
Return seconds;
}

Int sys_getpid (void)
{
Return current-> pid;
}

Int sys_getppid (void)
{
Return current-> father;
}

Int sys_getuid (void)
{
Return current-> uid;
}

Int sys_geteuid (void)
{
Return current-> euid;
}

Int sys_getgid (void)
{
Return current-> gid;
}

Int sys_getegid (void)
{
Return current-> egid;
}

Int sys_nice (long increment)
{
If (current-> priority-increment> 0)
Current-> priority-= increment;
Return 0;
}

Int sys_signal (long signal, long addr, long restorer)
{
Long I;

Switch (signal ){
Case SIGHUP: case SIGINT: case SIGQUIT: case SIGILL:
Case SIGTRAP: case SIGABRT: case SIGFPE: case SIGUSR1:
Case SIGSEGV: case SIGUSR2: case SIGPIPE: case SIGALRM:
Case SIGCHLD:
I = (long) current-> sig_fn [signal-1];
Current-> sig_fn [signal-1] = (fn_ptr) addr;
Current-> sig_restorer = (fn_ptr) restorer;
Return I;
Default: return-1;
}
}

Void sched_init (void)
{
Int I;
Struct desc_struct * p;

Set_tss_desc (gdt + FIRST_TSS_ENTRY, & (init_task.task.tss); @ init task tss

Set_ldt_desc (gdt + FIRST_LDT_ENTRY, & (init_task.task.ldt); @ init ldt
P = gdt + 2 + FIRST_TSS_ENTRY;
For (I = 1; ia = p-> B = 0;
P ++;
P-> a = p-> B = 0;
P ++;
}
Ltr (0); @ call the tss of task 0
Lldt (0); @ call the ldt of task 0
Outb_p (0x36, 0x43);/* binary, mode 3, LSB/MSB, ch 0 */
Outb_p (LATCH & 0xff, 0x40);/* LSB */
Outb (LATCH> 8, 0x40);/* MSB */
Set_intr_gate (0x20, & timer_interrupt); @ irq 0 clock interruption
Outb (inb_p (0x21 )&~ 0x01,0x21 );
Set_system_gate (0x80, & system_call );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.