forked from andcloudio/go-concurrency-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,061 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func fun(s string) { | ||
for i := 0; i < 3; i++ { | ||
fmt.Println(s) | ||
time.Sleep(1 * time.Millisecond) | ||
} | ||
} | ||
|
||
func main() { | ||
// Direct call | ||
fun("direct call") | ||
|
||
// write goroutine with different variants for function call. | ||
|
||
// goroutine function call | ||
go fun("goroutine-1") | ||
|
||
// goroutine with anonymous function | ||
go func() { | ||
fun("goroutine-2") | ||
}() | ||
|
||
// goroutine with function value call | ||
fv := fun | ||
go fv("goroutine-3") | ||
|
||
// wait for goroutines to end | ||
fmt.Println("waiting for goroutines to complete..") | ||
time.Sleep(1 * time.Second) | ||
|
||
fmt.Println("done..") | ||
} |
29 changes: 29 additions & 0 deletions
29
01-intro-solution/01-goroutines/02-client-server/client/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
"os" | ||
) | ||
|
||
func main() { | ||
// connect to server on localhost port 8000 | ||
conn, err := net.Dial("tcp", "localhost:8000") | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
defer conn.Close() | ||
|
||
mustCopy(os.Stdout, conn) | ||
|
||
} | ||
|
||
// mustCopy - utility function | ||
func mustCopy(dst io.Writer, src io.Reader) { | ||
if _, err := io.Copy(dst, src); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
01-intro-solution/01-goroutines/02-client-server/server/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// write server program to handle concurrent client connections. | ||
listener, err := net.Listen("tcp", "localhost:8000") | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for { | ||
conn, err := listener.Accept() | ||
|
||
if err != nil { | ||
continue | ||
} | ||
go handleConn(conn) | ||
} | ||
} | ||
|
||
// handleConn - utility function | ||
func handleConn(c net.Conn) { | ||
defer c.Close() | ||
for { | ||
_, err := io.WriteString(c, "response from server\n") | ||
if err != nil { | ||
return | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
func main() { | ||
// program to print the value as 1 | ||
// deterministically. | ||
var wg sync.WaitGroup | ||
var data int | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
data++ | ||
}() | ||
wg.Wait() | ||
fmt.Printf("the value of data is %v\n", data) | ||
|
||
fmt.Println("Done..") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"sync" | ||
) | ||
|
||
// what will be output | ||
|
||
func main() { | ||
runtime.GOMAXPROCS(1) | ||
var wg sync.WaitGroup | ||
|
||
done := false | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
done = true | ||
}() | ||
wg.Wait() | ||
for !done { | ||
} | ||
fmt.Println("finished") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
//TODO: run the program and check that variable i | ||
// was pinned for access from goroutine even after | ||
// enclosing function returns. | ||
|
||
func main() { | ||
var wg sync.WaitGroup | ||
|
||
incr := func(wg *sync.WaitGroup) { | ||
var i int | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
i++ | ||
fmt.Println(i) | ||
}() | ||
return | ||
} | ||
|
||
incr(&wg) | ||
wg.Wait() | ||
fmt.Println("done..") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
func main() { | ||
var wg sync.WaitGroup | ||
|
||
// what is the output | ||
//TODO: fix the issue. | ||
|
||
for i := 0; i < 3; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
fmt.Println(i) | ||
}() | ||
} | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// What will be printed when the code below is executed? | ||
// And fix the issue to assure that `len(m)` is printed as 10. | ||
|
||
func main() { | ||
m := make(map[int]int) | ||
|
||
wg := &sync.WaitGroup{} | ||
mu := &sync.Mutex{} | ||
wg.Add(10) | ||
for i := 0; i < 10; i++ { | ||
go func() { | ||
defer wg.Done() | ||
mu.Lock() | ||
m[i] = i | ||
mu.Unlock() | ||
}() | ||
} | ||
wg.Wait() | ||
println(len(m)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
func main() { | ||
go func(a, b int) { | ||
c := a + b | ||
}(1, 2) | ||
// TODO: get the value computed from goroutine | ||
// fmt.Printf("computed value %v\n", c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
func main() { | ||
go func() { | ||
for i := 0; i < 6; i++ { | ||
// TODO: send iterator over channel | ||
} | ||
}() | ||
|
||
// TODO: range over channel to recv values | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
ch := make(chan int) | ||
|
||
go func() { | ||
defer close(ch) | ||
|
||
// TODO: send all iterator values on channel without blocking | ||
for i := 0; i < 6; i++ { | ||
fmt.Printf("Sending: %d\n", i) | ||
ch <- i | ||
} | ||
}() | ||
|
||
for v := range ch { | ||
fmt.Printf("Received: %v\n", v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
// TODO: Implement ping pong with Channel Direction | ||
|
||
func ping() { | ||
// send message on ch1 | ||
} | ||
|
||
func pong() { | ||
// recv message on ch1 | ||
// send it on ch2 | ||
} | ||
|
||
func main() { | ||
// create ch1 and ch2 | ||
|
||
// spine goroutine ping and pong | ||
|
||
// recv message on ch2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
//TODO: create channel owner goroutine which return channel and | ||
// writes data into channel and | ||
// closes the channel when done. | ||
|
||
consumer := func(ch <-chan int) { | ||
// read values from channel | ||
for v := range ch { | ||
fmt.Printf("Received: %d\n", v) | ||
} | ||
fmt.Println("Done receiving!") | ||
} | ||
|
||
ch := owner() | ||
consumer(ch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func main() { | ||
ch1 := make(chan string) | ||
ch2 := make(chan string) | ||
|
||
go func() { | ||
time.Sleep(1 * time.Second) | ||
ch1 <- "one" | ||
}() | ||
|
||
go func() { | ||
time.Sleep(2 * time.Second) | ||
ch2 <- "two" | ||
}() | ||
|
||
// TODO: multiplex recv on channel - ch1, ch2 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ch := make(chan string, 1) | ||
|
||
go func() { | ||
time.Sleep(2 * time.Second) | ||
ch <- "one" | ||
}() | ||
|
||
// TODO: implement timeout for recv on channel ch | ||
|
||
m := <-ch | ||
fmt.Println(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ch := make(chan string) | ||
|
||
go func() { | ||
time.Sleep(1 * time.Second) | ||
ch <- "one" | ||
}() | ||
|
||
// TODO: if there is no value on channel, do not block. | ||
|
||
m := <-ch | ||
fmt.Println(m) | ||
|
||
} |
Oops, something went wrong.