Title: Class 4: Once Upon a Process
Date: 2014-01-28
Category: Classes
Tags: operating systems, Rust, multi-tasking, preemptive
Author: David Evans

# Action Items

   <div class="todo">

**[Problem Set 2](|filename|../../pages/ps/ps2/ps2.md)** is now posted, and is due Sunday, 9 February.  Please don't wait to get started! 

**Read [Process API](http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-api.pdf)** and **[Direct Execution](http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf)** from [OSTEP](http://pages.cs.wisc.edu/~remzi/OSTEP/).

   </div>

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/0PUVy92eCnI?list=PLvpsxlEF9cP0wwqwprCmebP6S6OrLhU6o" frameborder="2" allowfullscreen></iframe>
</center>

## (Human) Communication Channels

Use [IRC](|filename|../../pages/tools/irc.md) for immediate responses
and conversations.  You should use the `cs4414` channel for
course-related questions, but take advantage of the expertise on the
`rust` channel also for Rust language questions.  Getting direct answers
to your questions from people working on developing the language can be
even better than having polished documentation!

Use the course site for more permanent questions and discussions.  Most
pages (including thie one) will have their own discussion at the bottom
of the page for comments relevant to that page.  For general course
discussions, use the [Forum](|filename|../../pages/discuss.md).  For
totally open discussion, use the [Open Discussion
Forum](|filename|../../pages/open.md).  It is general best if you post
using your real identity on the course discussions, but if you prefer to
post anonymously you should feel free to do that also.

Use email (_evans@virginia.edu_) for communications that don't belong in
the other places including things that should be kept private (e.g.,
issues with your project teammate) as well as messages to notify me if
you didn't get a helpful response from a question posted on the course
site within a reasonable time.

## Finding Rust Documentation

Rust is not mature enough to have extensive documentation yet, but (at
least compared to last semester) there is now quite a bit of useful
documentation.

Here are some links to documentation you may find useful (and at least one you most certainly will not):

- [Rust Manual](http://static.rust-lang.org/doc/master/rust.html) 
- [Rust Tutorials_NG](http://adridu59.github.io/rust-tuts/)
- [Rust Cheatsheet](http://static.rust-lang.org/doc/master/complement-cheatsheet.html) (answers to common _How to_ questions)
- [Rust for Rubyists](http://www.rustforrubyists.com/book/index.html) (mostly useful even if you don't speak Ruby)
- [Rust Reddit](http://www.reddit.com/r/rust)
- [Stack Overflow](http://stackoverflow.com/questions/tagged/rust) (Rust tag)
- [Strings in Rust](http://www.dorina.org/pubs/strings.html) (tutorial from last semester)
- You can use DuckDuckGo, Bing, or Google to search the rust site by adding `site:rust-lang.org` to your queries.

(If you find other useful things, please post them in the comments below.)

## Slides

<center>
<iframe src="http://www.slideshare.net/slideshow/embed_code/30550199" width="476" height="400" frameborder="2" marginwidth="0" marginheight="0" scrolling="no"></iframe><br>
</center>


</center>

## Statements and Expressions

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/lDXLmtDfPDE?list=PLvpsxlEF9cP0wwqwprCmebP6S6OrLhU6o" frameborder="2" allowfullscreen></iframe>
</center>

Hypothesized Rust grammar (the Rust manual has a grammar, but "looking
for consistency in the manual's grammar is bad: it's entirely wrong in
many places"):

>   _IfExpression_ ::= **if** _Expression_ _Block_ [ **else** _Block_ ]

>   _Block_ ::= **{** [ _Statement_* _Expr_] **}**

>   _Expression_ ::= _Block_


How is the meaning of `;` different in Rust and Java?

<div class="gap">

</div>

## Higher-Order Functions

We can make new functions directly:

>   _LambdaExpression_ ::= **|** _Parameters_ **|** _Block_

Example code:

```rust
fn double(a: int) -> int { a * 2 }

fn ntimes(f: proc(int) -> int, times: int) -> proc(int) -> int {
    proc(x: int) { 
       match times {
         0 => { x }
         _ => { ntimes(f, times - 1)(f(x))} 
       }
    }
}

fn main() {
    let quadruple = ntimes(double, 2);
    println!("quad: {:d}", quadruple(2));
}
```

(This should work, but encounters a bug with Rust 0.9.)

If you are uncomfortable with code that passes procedures as parameters,
I would (unbiasedly, of course) encourage you to read Chaters 4 and 5
from my [_Introduction to Computing: Explorations in Language, Logic,
and Machines_] book.  We won't spend more time explicitly covering it in
this class, but you will definitely need to write code that uses
higher-order procedures effectively in this class (and to be an empowered
programmer in general).

# Processes

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/1hDCouuvers?list=PLvpsxlEF9cP0wwqwprCmebP6S6OrLhU6o" frameborder="2" allowfullscreen></iframe>
</center>


What is a process?

<div class="gap">

</div>

Some terminology (not really important, but you'll hear people use
them):

- **Multiprogramming** - program gets to run until it gets stuck, then
    supervisor takes over and selects another program to run.
- **Non-pre-emptive multi-tasking** (sometimes called _co-operative
    multi-tasking_ - program gets to run until it
    voluntarily gives control back to the supervisor.  (Sometimes used
    to mean the programs are all scheduled statically to recieve a
    particular processing time slice.)
- Normal (pre-emptive) **multi-tasking** - program gets to run until
    (approximately) supervisor decides its someone else's turn.

(For historical reasons, the terms and hyphenating-conventions are
confusing, using "program", "task", and "process" to essentially
interchangably, except that the historical term _multiprogramming_ is
different from _multitasking_.)

What are some consequences of the difference between _non pre-emptive
multi-tasking_ and _pre-emptive multi-tasking_?


<div class="gap">

</div>

How can _pre-emptive_ multi-tasking even be possible with only one
processor?

<div class="gap">

</div>

<center>
<img
src="http://www.cs.virginia.edu/evans/cs4414-fall2013/static/classes/class4/interruptus.gif"
width="640" height="287" alt="Imperious Interruptus!">
</center>


How often should the kernel timer interrupt (a.k.a., "supervisor's alarm
clock") go off?
<div class="gap">

</div>

What programs should be able to change the kernel timer interrupt frequency?

<div class="gap">



</div>

Who prefers the kernel timer interrupt interval to be shorter?  Who
prefers it to be longer?

<div class="gap">




</div>

Wil Thomason's [Kernel Timer
Interrupt](https://github.com/wbthomason/rustKernelTimer) program (for
measuring the kernel timer interrupt time on your machine).  (This
worked in Rust 0.8, you may need to make some changes to update it to
work in Rust 0.9.)

### Links and Videos

[Unix History](https://en.wikipedia.org/wiki/File:Unix_history-simple.svg)

Here's the movie clip I didn't show in class about how Mac OS X got preemptive multi-tasking:

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/YsWBJ_usRck?&t=2m18s" frameborder="0" allowfullscreen></iframe>
</center>

If you missed Sorkin's earlier moving about operating systems, here's the key scene from that one:

<center>
<iframe width="480" height="360" src="//www.youtube.com/embed/-3Rt2_9d7Jg" frameborder="0" allowfullscreen></iframe>
</center> 

For a more truthful account of how Mark did in his [Operating Systems
course](http://www.eecs.harvard.edu/~mdw/course/cs161/), see Matt
Welsh's [_How I almost killed
Facebook_](http://matt-welsh.blogspot.com/2009/02/how-i-almost-killed-facebook.html).
This is [the lecture from the
movie](http://www.eecs.harvard.edu/~mdw/course/cs161/notes/vm.pdf) -
we'll get into virtual memory some later in the course, but the lecture
in the movie is pretty good if you want to get a head start. ("The
take-away from the movie is clear: nerds win. Ideas are cheap and don't
mean squat if you don't know how to execute on them. To have an impact
you need both the vision and the technical chops, as well as the
tenacity to make something real." - Matt Welsh on _The Social Network_.)




<div id="disqus_thread"></div>

<script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'rust-class'; // required: replace example with your forum shortname
	var disqus_url = 'http://www.rust-class.org/class-4-once-upon-a-process.html';

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>


