This work should be completed before the exercise on Friday 13th November.
Samtliga uppgifter på kursen ska lämnas in på
organisationen dd1396ht20 på KTH GitHub.
Repos för de tre övningarna heter username-ovnx
.
Användaren nisse
hittar alltså sitt repo för övning 2 på
https://gits-15.sys.kth.se/dd1396ht20/nisse-ovn2.
Gör (minst) en fil per uppgift. Utgå från mallarna i /pallinda20/ovn0/. Lösningar skall vara inlämnade innan övningen börjar.
- Vid övningen ska du vara beredd att muntligt presentera och diskutera dina lösningar och din programkod.
- Uppgifter märkta med HANDIN ska ockå lämnas in skriftligt innan övningens start.
Study the following course literature:
- Read the following from the Step-by-step guide to concurrency
Explain what is wrong in the code below, and then fix the code so that all data really passes through the channel and gets printed.
package main
import "fmt"
// I want this program to print "Hello world!", but it doesn't work.
func main() {
ch := make(chan string)
ch <- "Hello world!"
fmt.Println(<-ch)
}
See: bug01.go for source code to modify.
package main
import "fmt"
// This program should go to 11, but sometimes it only prints 1 to 10.
func main() {
ch := make(chan int)
go Print(ch)
for i := 1; i <= 11; i++ {
ch <- i
}
close(ch)
}
// Print prints all numbers sent on the channel.
// The function returns when the channel is closed.
func Print(ch <-chan int) {
for n := range ch { // reads from channel until it's closed
fmt.Println(n)
}
}
See: bug02.go for source code to modify.
The program many2many.go contains four producers that together send 32 strings over a channel. At the other end there are two consumers that receive the strings. Describe what happens, and explain why it happens, if you make the following changes in the program. Try first to reason your way through, and then test your hypothesis by changing and running the program.
- What happens if you switch the order of the statements
wgp.Wait()
andclose(ch)
in the end of themain
function? - What happens if you move the
close(ch)
from themain
function and instead close the channel in the end of the functionProduce
? - What happens if you remove the statement
close(ch)
completely? - What happens if you increase the number of consumers from 2 to 4?
- Can you be sure that all strings are printed before the program stops?
Finally, modify the code by adding a new WaitGroup that waits for all consumers to finish.
The code in oracle.go contains the outline for a program that will answer 'questions'.
Complete the Oracle
function. You should not modify the main
function or other function signatures.
Note that answers should not appear immediately; instead there should be a delay or pause for thought.
Also, the Oracle will still print helpful predictions even if there are not any questions.
You may structure your solution into multiple functions.
Your program should contain two channels: One channel for questions, and one for answers and predictions.
In the Oracle
function you should start three indefinite go-routines.
- A go-routine that receives all questions, and for each incoming question, creates a separate go-routine that answers that question
- A go-routine that generates predictions
- A go-routine that receives all answers and predictions, and prints then to stdout
Whilst the Oracle
function is the most important of the assignment, you may also want to improve the answer-algorithm.