# Week 08 Laboratory Exercises

### Objectives

• Understand how rust provides fearless concurrency
• Apply ownership semantics

### Activities To Be Completed

The following is a list of all the activities available to complete this week...

• Train Game
• Channels!
• WebServer
• Send and Sync

### Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

### Getting Started

Create a new directory for this lab called lab08, change to this directory, and fetch the provided code for this week by running these commands:

mkdir lab08
cd lab08
6991 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:

1. Chunk the vec of tuples into a vec of vecs of tuples
2. Create a new thread scope
3. For each chunk (a singular vec of tuples) spawn a new thread (inside the scope)
4. Have each thread iterate through its chunk, and evaluate the expression
5. 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


You must run give before Week 9 Wednesday 21: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
• 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 (/home/cs6991/web/22T3/course/activities/channels/solutions)
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
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


You must run give before Week 9 Wednesday 21: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


You must run give before Week 9 Wednesday 21: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


You must run give before Week 9 Wednesday 21: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.

### Submission

When you are finished each exercise make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Week 9 Wednesday 21:00:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

Automarking will be run several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run you can view your results here.

#### Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

6991 classrun -sturec