⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4.html

📁 介绍linux下文件和设备编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<center><A HREF="#Content">[目录]</A></center><hr><br><A NAME="I409" ID="I409"></A><center><b><font size=+2>sched.c</font></b></center><br>|schedule<br>&nbsp;&nbsp; |do_softirq // manages post-IRQ work<br>&nbsp;&nbsp; |for each task<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |calculate counter<br>&nbsp;&nbsp; |prepare_to__switch // does anything<br>&nbsp;&nbsp; |switch_mm // change Memory context (change CR3 value)<br>&nbsp;&nbsp; |switch_to (assembler)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |SAVE ESP<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |RESTORE future_ESP<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |SAVE EIP<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |push future_EIP *** push parameter as we did a call<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |jmp __switch_to (it does some TSS work)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |__switch_to()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ..<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |ret *** ret from call using future_EIP in place of call address<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new_task<p><br>/*<br>* 'sched.c' is the main kernel file. It contains scheduling primitives<br>* (sleep_on, wakeup, schedule etc) as well as a number of simple system<br>* call functions (type getpid(), which just extracts a field from<br>* current-task<br>*/<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#include<br>#define LATCH (1193180/HZ)<br>extern void mem_use(void);<br>extern int timer_interrupt(void);<br>extern int system_call(void);<br>union task_union {<br>struct task_struct task;<br>char stack[PAGE_SIZE];<br>};<br>static union task_union init_task = {INIT_TASK,};<br>long volatile jiffies=0;<br>long startup_time=0;<br>struct task_struct *current = &amp;(init_task.task), *last_task_used_math =<br>NULL;<br>struct task_struct * task[NR_TASKS] = {&amp;(init_task.task), };<br>long user_stack [ PAGE_SIZE&gt;&gt;2 ] ;<br>struct {<br>long * a;<br>short b;<br>} stack_start = { &amp; user_stack [PAGE_SIZE&gt;&gt;2] , 0x10 };<br>/*<br>* 'math_state_restore()' saves the current math information in the<br>* old math state array, and gets the new ones from the current task<br>*/<br>void math_state_restore() @@协处理器状态保存<br>{<br>if (last_task_used_math)<br>__asm__(&quot;fnsave %0&quot;::&quot;m&quot; (last_task_used_math-&gt;tss.i387));<br>if (current-&gt;used_math)<br>__asm__(&quot;frstor %0&quot;::&quot;m&quot; (current-&gt;tss.i387));<br>else {<br>__asm__(&quot;fninit&quot;::);<br>current-&gt;used_math=1;<br>}<br>last_task_used_math=current;<br>}<br>/*<br>* 'schedule()' is the scheduler function. This is GOOD CODE! There<br>* probably won't be any reason to change this, as it should work well<br>* in all circumstances (ie gives IO-bound processes good response etc).<br>* The one thing you might take a look at is the signal-handler code<br>here.<br>*<br>* NOTE!! Task 0 is the 'idle' task, which gets called when no other<br>* tasks can run. It can not be killed, and it cannot sleep. The 'state'<br>* information in task[0] is never used.<br>*/<br>void schedule(void)<br>{<br>int i,next,c;<br>struct task_struct ** p;<br>/* check alarm, wake up any interruptible tasks that have got a signal<br>*/<br>for(p = &amp;LAST_TASK ; p &gt; &amp;FIRST_TASK ; --p)<br>if (*p) {<br>if ((*p)-&gt;alarm &amp;&amp; (*p)-&gt;alarm &lt; jiffies) {<br>@@??<br>(*p)-&gt;signal |= (1&lt;&lt;(SIGALRM-1));@@14-1<br>(*p)-&gt;alarm = 0;<br>}<br>if ((*p)-&gt;signal &amp;&amp; (*p)-&gt;state==TASK_INTERRUPTIBLE)<br>(*p)-&gt;state=TASK_RUNNING;<br>}<br>@@ task 1 如何变为TASK_RUNNING??signal 如何得到,alarm如何变非0且 /* this is the<br>scheduler proper: */<br>@@操作系统最重要的函数,调度算法<br>@@这个循环要找到一个可运行的任务才能退出,会死在这吗?即如没有一个可运行<br>while (1) {<br>c = -1;<br>next = 0;<br>i = NR_TASKS;<br>p = &amp;task[NR_TASKS];<br>while (--i) {<br>if (!*--p)<br>continue;<br>if ((*p)-&gt;state == TASK_RUNNING &amp;&amp; (*p)-&gt;counter &gt; c)<br>c = (*p)-&gt;counter, next = i;<br>}<br>if (c) break; @@记数大于零<br>for(p = &amp;LAST_TASK ; p &gt; &amp;FIRST_TASK ; --p)<br>if (*p)<br>(*p)-&gt;counter = ((*p)-&gt;counter &gt;&gt; 1) +<br>(*p)-&gt;priority;<br>}<br>switch_to(next);<br>}<br>int sys_pause(void)<br>{<br>current-&gt;state = TASK_INTERRUPTIBLE; @@任务可中断<br>schedule();<br>return 0;<br>}<br>void sleep_on(struct task_struct **p)<br>{<br>struct task_struct *tmp;<br>if (!p)<br>return;<br>if (current == &amp;(init_task.task))<br>panic(&quot;task[0] trying to sleep&quot;);<br>tmp = *p;<br>*p = current;<br>current-&gt;state = TASK_UNINTERRUPTIBLE;<br>schedule();<br>if (tmp) @@激活p,什么时候回来?唤醒上次睡眠的进程<br>tmp-&gt;state=0;<br>}<br>void interruptible_sleep_on(struct task_struct **p)<br>{<br>struct task_struct *tmp;<br>if (!p)<br>return;<br>if (current == &amp;(init_task.task))<br>panic(&quot;task[0] trying to sleep&quot;);<br>tmp=*p;<br>*p=current;<br>repeat: current-&gt;state = TASK_INTERRUPTIBLE;<br>schedule();<br>if (*p &amp;&amp; *p != current) {<br>(**p).state=0;<br>goto repeat;<br>}<br>@@好象下不来<br>*p=NULL;<br>if (tmp)<br>tmp-&gt;state=0;<br>}<br>void wake_up(struct task_struct **p)<br>{<br>if (p &amp;&amp; *p) {<br>(**p).state=0; @@唤醒该进程running<br>*p=NULL; @@睡眠栈为0<br>}<br>}<br>void do_timer(long cpl) @@定时调度<br>{<br>if (cpl)<br>current-&gt;utime++; @@用户态时间加一<br>else<br>current-&gt;stime++; @@系统态时间加一<br>if ((--current-&gt;counter)&gt;0) return; @@当前记数减一<br>current-&gt;counter=0;<br>if (!cpl) return;<br>schedule();<br>}<br>int sys_alarm(long seconds)<br>{<br>current-&gt;alarm = (seconds&gt;0)?(jiffies+HZ*seconds):0;<br>return seconds;<br>}<br>int sys_getpid(void)<br>{<br>return current-&gt;pid;<br>}<br>int sys_getppid(void)<br>{<br>return current-&gt;father;<br>}<br>int sys_getuid(void)<br>{<br>return current-&gt;uid;<br>}<br>int sys_geteuid(void)<br>{<br>return current-&gt;euid;<br>}<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -