Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package main import ( fmt math / rand os strconv sync time ) func main ( ) {
package main
import
fmt
"mathrand
os
"strconv"
sync
"time"
func main
rand.SeedtimeNowUnixNano
if lenosArgs
fmtPrintlnPlease provide an integer argument."
return
num, err : strconv.AtoiosArgs
if err nil
fmtPrintlnInvalid input type."
return
buffered : makechan int, num buffered channel
var wait syncWaitGroup
wait.Add
go producer&wait, buffered, num
go consumerbuffered
wait.Wait
func producerwait syncWaitGroup, out chan int, num int
defer wait.Done
for x :; x num; x
value : rand.Intnnum
fmtPrintfProduced: v
value
out value
closeout
func consumerin chan int
for v : range in
fmtPrintfConsumed: v
v
simulate a producer and a consumer, each as a goroutine:
the producer produces a given number of random integers in a given range and sends each to the consumer via an unbuffered channel unidirectional or bidirectional
the producer goroutine outputs each number before it sends it
the consumer receives each integer from the producer and consumes it
the consumer goroutine outputs each number after it receives it
modify your program from part so that a buffered channel of a given size is used This is part of the program should have an output similar to Produced
Produced
Produced
Produced
Consumed
Consumed
Consumed
Consumed
Produced
Produced
Produced
Produced
Produced
Consumed
Consumed
Consumed
Consumed
Consumed
Produced
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started