PS2 Reference Solution

Posted: Fri 14 February 2014

The reference solution for Problem Set 2 is now available:

Even if you got everything to work yourself, I encourage you to look over the reference solution. It is about 1/3 the length of most of your submissions (although some of this may be because of the code you added to support and extra feature, most of it is because of the reference solution code being more concise and avoiding the duplication that many of your solutions needed).

If your code for handling pipes was more than 5-6 lines long and needed lots of special cases, you should definitely look at how the reference solution does it: (I've removed some comments that are in the version in the repo linked above, so you can figure out what the code is doing yourself)

let mut pipes: ~[os::Pipe] = ~[];

pipes.push(os::Pipe { input: 0, out: 0 }); 
for _ in range(0, progs.len() - 1) { pipes.push(os::pipe()); }
pipes.push(os::Pipe { input: 1, out: 1 }); 

for i in range(0, progs.len()) {
    self.run_single_cmd(progs[i], pipes[i].input, pipes[i+1].out, 2, 
                        if (i == progs.len() - 1) { bg_flag } else { true });