Linux Kernel Learning-5 Task Scheduler (Follow Sina Weibo: Lonely Years of erosion (more than 4,000 technology sharing))

Source: Internet
Author: User

void Schedule (void)
105 {
106 int i,next,c;
107 struct Task_struct * * p; Pointer to the task structure pointer.
108
109/* Check alarm, wake up any interruptible the tasks that has got a signal */
/* Detects alarm (process alarm timing value), wakes up any interruptible task that gets signaled */
110
Starts the detection of alarm from the last task in the task array.
111 for (p = &last_task; p > &first_task;--p)
if (*p) {
If the alarm time of the task has expired (alarm<jiffies), place the SIGALRM signal in the signal bitmap and clear alarm.
Jiffies is the number of ticks (10ms/ticks) from the start of the system. defined on line 139th of Sched.h.
113 if ((*p)->alarm && (*p)->alarm < jiffies) {
(*p)->signal |= (1<< (SIGALRM-1));
(*p)->alarm = 0;
116}
If there are other signals in the signal bitmap to remove the blocked signal and the task is in a interruptible state, the task is in the ready state.
where ' ~ (_blockable & (*p)->blocked) ' is used to ignore blocked signals, but Sigkill and Sigstop cannot be blocked.
117 if (((*p)->signal & ~ (_blockable & (*p)->blocked)) &&
118 (*P)->state==task_interruptible)
119 (*P)->state=task_running; To the Ready (executable) state.
120}
121
122/* This is the scheduler proper: */
/* Here is the main part of the scheduler */
123
124 while (1) {
c =-1;
126 next = 0;
127 i = Nr_tasks;
p = &task[NR_TASKS];
This code also starts looping through the last task in the task array and skips the task slots without tasks. Compare each ready
The counter of the status task (the descending tick count of the task run time) value, which is large and not running long, next
The task number to which to point.
129 while (-I.) {
if (!*--p)
131 continue;
if (*p)->state = = task_running && (*p)->counter > C)
133 C = (*p)->counter, next = i;
134}
If you compare a result with a counter value greater than 0, exit the loop starting with line 124 and perform a task switch (141 rows).
135 if (c) break;
Otherwise, update the counter value of each task according to the priority value of each task, and then return to the 125 row to re-compare.
The counter value is calculated as Counter = COUNTER/2 + priority. [Right counter=0??]
136 for (p = &last_task; p > &first_task;--p)
137 if (*p)
More e-book tutorials download Please login Http://down.zzbaike.com/ebook
This site provides e-book tutorials are online collection, if the tutorial involves or infringes on your copyright, please contact us.
5th Chapter Kernel Code linux/kernel/
103
138 (*p)->counter = ((*p)->counter >> 1) +
139 (*P)->priority;
140}
141 switch_to (next); Switch to task number for next task and run it.
142}
143
Pause () system call. Converts the status of the current task to an interruptible wait state and dispatches it again.
144 int sys_pause (void)
145 {
146 current->state = task_interruptible;
147 Schedule ();
148 return 0;
149}
150
Set the current task to a non-interruptible wait state, and point the sleep queue header pointer to the current task.
Returns only if it is explicitly awakened. This function provides a synchronization mechanism between the process and the interrupt handler.
The function parameter *p is the queue header pointer that places the wait task. (See the description after the list).
151 void sleep_on (struct task_struct **p)
152 {
153 struct Task_struct *tmp;
154
If the pointer is not valid, exit. (The object that the pointer refers to can be null, but the pointer itself is not 0).
155 if (!p)
156 return;
157 if (current = = & (Init_task.task))//(impossible!) If the task is task 0.
158 Panic ("task[0] trying to sleep");
159 TMP = *P; Let TMP point to the task (if any) that is already waiting on the queue.
*p = current; Points the wait pointer for the sleep queue header to the current task.
161 current->state = task_uninterruptible; Resets the current task to a non-interruptible wait state.
162 Schedule (); Re-Dispatch.
The scheduler only returns to this place when the wait task is awakened, indicating that it is explicitly awakened.
163 if (TMP)//If there is a waiting task, it is also set to the ready state.
164 tmp->state=0;
165}
166
Resets the current task to an interruptible wait state and puts it into the waiting queue specified by *p. See the description of sleep_on () after the list.
167 void interruptible_sleep_on (struct task_struct **p)
168 {
169 struct task_struct *tmp;
170
171 if (!p)
172 return;
173 if (current = = & (Init_task.task))
174 panic ("task[0] trying to sleep");
175 tmp=*p;
176 *p=current;
177 repeat:current->state = task_interruptible;
178 Schedule ();
If the waiting queue has a wait task, and the queue header pointer points to a task other than the current task, the wait task is set to
The ready state to run and re-execute the scheduler. When the pointer *p is not pointing to the current task, the current task is placed
After entering the queue, new tasks are inserted into the wait queue, so since this task is interruptible, you should first perform all
Other waiting tasks.
179 if (*p && *p! = current) {
More e-book tutorials download Please login Http://down.zzbaike.com/ebook
This site provides e-book tutorials are online collection, if the tutorial involves or infringes on your copyright, please contact us.
5th Chapter Kernel Code linux/kernel/
104
(**p). state=0;
181 goto Repeat;
182}
The following code is wrong, should be *p = tmp, let the queue head pointer to the rest of the waiting task, otherwise before the current task inserted
The tasks waiting on the queue are erased. See Figure 4.3.
183 *p=null;
184 if (TMP)
185 tmp->state=0;
186}
187
Wakes the specified task *p.
188 void wake_up (struct task_struct **p)
189 {
if (P && *p) {
191 (**P). state=0; Ready-to-run status.
192 *p=null;
193}
194}

Linux Kernel Learning-5 Task Scheduler (Follow Sina Weibo: Lonely Years of erosion (more than 4,000 technology sharing))

Related Article

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.