* same with xv6
[mascara-docs.git] / i386 / MIT / course / lectures / all / 6.828 / 2011 / homework / xv6-sched.html
blobc4a03638caa781f0fc80e6e147abce26cfc6ba9b
1 <html><head><title>Homework: Threads and Context Switching</title> <link rel="stylesheet" href="homework.css" type="text/css" /></head><body><h1>Homework: Threads and Context Switching</h1>Read: swtch.S and proc.c (focus on the code that switches between processes, specifically <code>scheduler</code> and <code>sched</code>). Also process creation: sys_fork() and copyproc().<h2>Hand-In Procedure</h2>You are to turn in this homework during lecture. Please write up your answers to the exercises below and hand them in to a 6.828 staff member at the beginning of lecture.<h2>Introduction</h2>In this homework you will investigate how the kernel switches between two processes.<h2>Assignment</h2>Suppose a process that is running in the kernel calls <code>sched()</code>, which ends up jumping into <code>scheduler()</code>.<div class="question"><p><b>Submit</b>: Where is the stack that <code>sched()</code> executes on? Where is the stack that <code>scheduler()</code> executes on? When <code>sched()</code> calls <code>swtch()</code>, does that call to <code>swtch()</code> ever return? If so, when?</p></div>Now think back to <a href="../lec/l-x86.pdf">lecture 2</a> and the invariants that gcc expects any function, including <code>swtch</code>, to maintain. Compare these invariants with what <code>swtch</code> actually implements, and the state that our kernel maintains in a <code>struct context</code>.<div class="question"><p><b>Submit</b>: Could <code>swtch</code> do less work and still be correct? Could we reduce the size of a <code>struct context</code>? Provide concrete examples if yes, or argue for why not.</p></div>Surround the call to <code>swtch()</code> in <code>scheduler()</code> with calls to <code>cprintf()</code> like this:<pre>
2 cprintf("a");
3 swtch(&amp;cpu-&gt;scheduler, proc-&gt;context);
4 cprintf("b");
5 </pre>Similarly, surround the call to <code>swtch()</code> in <code>sched()</code> with calls to <code>cprintf()</code> like this:<pre>
6 cprintf("c");
7 swtch(&amp;proc-&gt;context, cpu-&gt;scheduler);
8 cprintf("d");
9 </pre>Rebuild your kernel and boot it on QEMU. With a few exceptions you should see a regular four-character pattern repeated over and over.<div class="question"><p><b>Submit</b>: What is the four-character pattern? The very first characters are <code>ac</code>. Why does this happen?</p></div></body>