The following is a list of all the markedactivities
available to complete this week...
Train Game
Channels!
WebServer
Send and Sync
The following practice activities are optional
and are not marked, or required
to be completed for the week.
Obelisk - using web frameworks
Preparation
Before attempting the weekly exercises
you should re-read the relevant lecture slides
and their accompanying examples.
Getting Started
Create a new directory for this week's exercises called lab08,
change to this directory,
and fetch the provided code for this week
by running these commands:
mkdir lab08cd lab086991 fetch lab 08
Or, if you're not working on CSE,
you can download the provided code
as a tar file.
Exercise:
Train Game
In Sydney, all train carraiges have an identifiable
four digit number.
For example, in the above image, the train carriage number is 3592.
A popular game amongst Sydney train passengers is to find some arrangement
of the numbers, and four mathematical operators (+, -, x, /), that will give a
result of 10.
For example, in the above image, the numbers 3 5 9 2 can be arranged as
3 x 2 - 5 + 9 = 10
In this exercise, you are given starter code that currently takes in a
FIVE digit number, and prints out all possible arrangements
of the numbers, and four mathematical operators (+, -, x, /), that will give a
result of 10.
It does this by generating all possible combinations into a vec of tuples.
Each tuple is a combination of the numbers and operators:
(<Vec<i32>, Vec<char>>), where the first
element is a vector of the digits, and the second element is a vector of the
operators.
It then iterates through the vec of tuples, evaluates the expression
left to right, without order of operations, and prints out the
expression if it evaluates to 10.
Your task is to modify the code to take advantage of rust's
fearless concurrency to speed up the program.
To do this, you will need to roughly follow the following steps:
Chunk the vec of tuples into a vec of vecs of tuples
Create a new thread scope
For each chunk (a singular vec of tuples) spawn a new thread (inside the scope)
Have each thread iterate through its chunk, and evaluate the expression
Have each thread print out the expression if it evaluates to 10
When you think your program is working,
you can use autotest
to run some simple automated tests:
6991 autotest
When you are finished working on this exercise, you must submit your work by running give:
6991 give-crate
The recommended due date for this exercise is
Week 9 Wednesday 21:00:00.
You must run give
before Week 10 Friday 17:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give must be entirely your own.
Exercise:
Channels!
In order to get started, you will need to create a Cargo project
You can do this by running the cargo new command in your terminal
6991 cargo new channels
Created binary (application) `channels` package
Last exercise, we refactored
a single threaded set of calculations to use
multiple threads.
Our threads all printed out the specific calculations
that they were able to find that met the requirements,
but, we have no way of knowing how many calculations each
thread found!
In this exercise you will be making use of
Rust's
channels
to communicate between workers.
To do this, it is assumed that you have completed the previous
exercise, and have a working solution that:
chunks the work into a set of smaller vectors
creates a thread scope
spawns a thread for each sub task/vector
does the calculation (and prints) in each thread
You should modify your code, such that:
Before the new thread scope is created, you create a channel
Each thread sends the number of calculations it found to the channel
After all the calculations are done, you receive the number of calculations
from each thread, and print out the total number of calculations
Your output should look something like this:
6991 cargo run -- 12345
cr -- 12345
Compiling either v1.8.0
Compiling itertools v0.10.5
Compiling channels v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 1.74s
Running `target/debug/channels 12345`
There are 2880 potential combinations
2 / 4 + 3 - 1 * 5 = 10
// -- CUT FOR BREVITY, YOUR PROGRAM WILL OUTPUT MORE --
2 * 4 - 3 + 5 / 1 = 10
Thread 0 found 5 combinations
Thread 3 found 11 combinations
Thread 4 found 19 combinations
Thread 2 found 4 combinations
Thread 5 found 9 combinations
Thread 1 found 11 combinations
Total: 59
When you think your program is working,
you can use autotest
to run some simple automated tests:
6991 autotest
When you are finished working on this exercise, you must submit your work by running give:
6991 give-crate
The recommended due date for this exercise is
Week 9 Wednesday 21:00:00.
You must run give
before Week 10 Friday 17:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give must be entirely your own.
Exercise:
WebServer
You have been given a simple web server - a piece of code that listens
to a port for web connections, and serves back a simple HTML page.
This html page two major features:
Some text that looks exactly like:
{{{ counter }}}
A button that will send a POST request to /counter
The server will be started by running:
6991 cargo run -- 12345
Server running on port 12345
This will start the server on port 12345.
You can then connect to it with your browser by visiting:
http://localhost:12345
The server will be listening for connections on that port, and will
respond to requests with the HTML page.
The HTML page looks something similar to this:
http://localhost:12345
Hello World!!
{{{ counter }}}
You will notice that the text in the form is currently set to
{{{ counter }}}.
This is a placeholder, and should be replaced with the actual value of the
counter, which will be 0 initially.
When you click the button, the server will receive a POST request to /counter.
This currently does nothing! You should make it so that the server increments
the counter by one, and then returns the new HTML page with the new value of the counter.
Your task is to complete the implementation of the server, such
that it will:
Spawn a thread per connection
Make State shared across threads, by using a locking primitive
Increment the counter in state when a POST request is received
Replace the {{{ counter }}} placeholder with the
actual value of the counter
To test your code, you can run the server, and then visit the page in your
browser. Marks will be determined based off 6991 cargo
test
correctly running.
The given 6991 autotest will simply call 6991 cargo test.
When you think your program is working,
you can use autotest
to run some simple automated tests:
6991 autotest
When you are finished working on this exercise, you must submit your work by running give:
6991 give-crate
The recommended due date for this exercise is
Week 9 Wednesday 21:00:00.
You must run give
before Week 10 Friday 17:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give must be entirely your own.
Exercise:
Send and Sync
You have been asked by a colleague to explain
how Rust provides static guarantees about
Concurrency.
They know some basic rust (equiv to up to week
7 of the course), but are not familiar with
the details of the Send and Sync traits (etc).
Your task is to write answers
to their questions, which can be found in the
starter code questions.md.
You will find this useful as revision for the
final exam.
When you are finished working on this exercise, you must submit your work by running give:
give cs6991 lab08_send_sync questions.md
The recommended due date for this exercise is
Week 9 Wednesday 21:00:00.
You must run give
before Week 10 Friday 17:00:00
to obtain the marks for this exercise.
Note that this is an individual exercise;
the work you submit with give must be entirely your own.