1. Homepage
  2. Exam
  3. [2022] COMP5426 Parallel and Distributed Computing - Final Exam Q3 Synchronization

[2022] COMP5426 Parallel and Distributed Computing - Final Exam Q3 Synchronization

This question has been solved
Engage in a Conversation

QUESTION 3 (20 Marks) CourseNana.COM

Write a pthreads program that uses mutexes and condition variables to solve a simple synchronization problem involving two children playing a toy. The problem is described as follows. CourseNana.COM


CourseNana.COM

CourseNana.COM

Two children child_0 and child_1 take turns to play with a toy in rotation. Each child plays with the toy for a specified amount of time. Afterwards, they must pass the toy to the other child. CourseNana.COM


CourseNana.COM

CourseNana.COM

Initially, child_0 plays with the toy. The children then take turns to play with the toy in the following order until “their parent” has called (i.e., the main thread cancels the child threads): child_0 g _child_1 g _child_0 g _child_1 g _child_0 CourseNana.COM


CourseNana.COM

CourseNana.COM

You only need to write a thread routine child_routine(). In the routine children take turns to play in an infinite loop. (There is no problem as the child threads will be terminated by the main thread.) CourseNana.COM


CourseNana.COM

Your child_routine() needs to insert the following print statements: CourseNana.COM

When a child is to play, print CourseNana.COM

printf("child %d: I get to play with the toy for %d units of time.\n", myid->id, play_time); CourseNana.COM

When a child is to stop playing and pass the toy to the other child, print CourseNana.COM

printf("child %d: I now give the toy to the other child.\n", myid->id); CourseNana.COM

In the above play_time is a fixed number of seconds. When a child is playing, just use sleep(play_time) function to put it into sleep for play_time seconds. CourseNana.COM


CourseNana.COM


CourseNana.COM

Get the Solution to This Question

WeChat WeChat
Whatsapp WhatsApp
COMP5426代写,Parallel and Distributed Computing代写,The University of Sydney代写,COMP5426代编,Parallel and Distributed Computing代编,The University of Sydney代编,COMP5426代考,Parallel and Distributed Computing代考,The University of Sydney代考,COMP5426help,Parallel and Distributed Computinghelp,The University of Sydneyhelp,COMP5426作业代写,Parallel and Distributed Computing作业代写,The University of Sydney作业代写,COMP5426编程代写,Parallel and Distributed Computing编程代写,The University of Sydney编程代写,COMP5426programming help,Parallel and Distributed Computingprogramming help,The University of Sydneyprogramming help,COMP5426assignment help,Parallel and Distributed Computingassignment help,The University of Sydneyassignment help,COMP5426solution,Parallel and Distributed Computingsolution,The University of Sydneysolution,