Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Go language Write a program that uses buffered channels to monitor and lock resources. The program will use a ComputeServer that will use a maximum
Go language
Write a program that uses buffered channels to monitor and lock resources. The program will use a "ComputeServer" that will use a maximum of three go routines for the calculation. The program will also use a "DisplayServer" making sure that input and output to the console is completed and interleaved. Use two global buffered channels as semaphores and two wait groups to wait for all routines to finish before exiting const NumRoutines3 NumRequests 1000 // global semaphore monitoring the number of routines var semRout = make (chan int, Num Routines) // global semaphore monitoring console var semDispmake (chan int, 1) // Waitgroups to ensure that main does not exit until all done var wgRout sync.WaitGroup var wgDisp sync.WaitGroup Use a structure for the compute tasks: type Task struct a, b float32 disp chan float32 Implement the following functions func solve (t *Task) A function that sleeps for a random time between 1 and 15 seconds, adds the numbers a and b and sends the result on the display channel. func handleReq (t *Task) A function that acts as intermediary between ComputeServer and solve func ComputeServer () (chan *Task) A function that uses the channel factory pattern (lambda) and listens for requests on the created channel for tasks. It calls the handleReq function. func DisplayServer (chan float32) A function that uses the channel factory pattern lambda) and listens for requests on the created channel for results to print to the console The draft main routine (to be completed) is given as follows func main) dispChan :- DisplayServer () reqChan :- ComputeServer () for var a, b float32 // make sure to use semDisp fmt.Print ("Enter two numbers:") fmt . Scanf ( " % f %f ", &a, &b) fmt . Printf("%f %f ", a, b) break // Create task and send to ComputeServer time.Sleep 1e9) // Don' t exit until all is done Example run Enter two numbers: 2.4 3 2.400000 3.000000 Enter two numbers: 8.0 1.5 8.000000 1.500000 Result: 5.400000 Enter two numbers: 00 0.000000 0.000000 Result: 9.500000 Write a program that uses buffered channels to monitor and lock resources. The program will use a "ComputeServer" that will use a maximum of three go routines for the calculation. The program will also use a "DisplayServer" making sure that input and output to the console is completed and interleaved. Use two global buffered channels as semaphores and two wait groups to wait for all routines to finish before exiting const NumRoutines3 NumRequests 1000 // global semaphore monitoring the number of routines var semRout = make (chan int, Num Routines) // global semaphore monitoring console var semDispmake (chan int, 1) // Waitgroups to ensure that main does not exit until all done var wgRout sync.WaitGroup var wgDisp sync.WaitGroup Use a structure for the compute tasks: type Task struct a, b float32 disp chan float32 Implement the following functions func solve (t *Task) A function that sleeps for a random time between 1 and 15 seconds, adds the numbers a and b and sends the result on the display channel. func handleReq (t *Task) A function that acts as intermediary between ComputeServer and solve func ComputeServer () (chan *Task) A function that uses the channel factory pattern (lambda) and listens for requests on the created channel for tasks. It calls the handleReq function. func DisplayServer (chan float32) A function that uses the channel factory pattern lambda) and listens for requests on the created channel for results to print to the console The draft main routine (to be completed) is given as follows func main) dispChan :- DisplayServer () reqChan :- ComputeServer () for var a, b float32 // make sure to use semDisp fmt.Print ("Enter two numbers:") fmt . Scanf ( " % f %f ", &a, &b) fmt . Printf("%f %f ", a, b) break // Create task and send to ComputeServer time.Sleep 1e9) // Don' t exit until all is done Example run Enter two numbers: 2.4 3 2.400000 3.000000 Enter two numbers: 8.0 1.5 8.000000 1.500000 Result: 5.400000 Enter two numbers: 00 0.000000 0.000000 Result: 9.500000Step 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