Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use the POSIX threads library in order to create two C programs: 1. The first program creates three threads: a main thread and two player
Use the POSIX threads library in order to create two C programs: 1. The first program creates three threads: a main thread and two player threads. The player threads take turns playing a Tic-Tac-Toe game. The main thread creates the board, displays the board between player moves, decides the result of the game, and exits the program. Use a 33 board for this game. Player threads take turns randomly choosing a cell and placing their symbol (X or O) in that cell. 2. The second program creates a main thread and n "hikers" threads; the maximum value for n is 100 . The program simulates a race by hikers to reach the top of a mountain. The main thread announces the winner of the race. Details: - Tic-Tac-Toe: All three threads share a structure object, which contains the game board and a turn variable. The turn variable is used for synchronization between the three threads. The game board is a 33 array of chars. Initially all cells are empty. Player threads randomly enter their move in the board. The turn variable can have the values 0,1 , or 2 . When turn =0 only the main thread can access the game board. Otherwise, when turn =1 or 2 , only player 1 or 2 can access the board. More specifically: A global variable of type shared is needed (you can add more elements if you wish): struct shared \{ char *board; int turn; \}; Main thread: Initialize the game board // define a function Set turn to 0// no players can enter moves yet Create two player threads with ids 1 and 2 Repeat the following // infinite loop (while (1) {} ) while (turn != 0) ; // wait until turn becomes 0 Display the game board on the standard output // define a function If (there is a winner || the game board is full) game_on = false; Cancel player threads // thread_cancel () Determine and announce winner/draw // define a function Exit the game End If Else Set turn to the next player End Repeat Player thread (id): // this id is not of type pthread_t; it is an int Repeat (infinite loop) while (turn I= id); // wait for my turn Randomly generate an integer that represents a cell in the game board If the cell is taken, try again until the generated random number corresponds to an empty cell // Define a function decideMove for the previous two steps Enter my move to the shared board Set turn to 0 // Main thread's turn End Repeat Note that the thread id is passed through the create function, where playGame is a r-defined function that defines your top-level function for the thread: pthread_create (\&tid1, dattr, playGame, "1"); pthread_create (Etid2, \&attr, playGame, "2"); - Hiking Race: In the Tic-Tac-Toe game, the main thread cancels the player threads. In this program, the main thread waits until all hikers reach the mountain top. Each hiker thread carries a flag, which they can mistakenly drop along the way. They may not realize that they dropped the flag immediately. Hence, once they notice they have dropped the flag, they have to go back to where they dropped and start from that position. The distance covered is simulated by an integer. Each step taken by the hiker increases the integer by 1 . Flag dropping is simulated by tossing a coin after each step. A coin is also tossed after each step to see if the hiker should know if they dropped the flag or not. - Here too, we need a shared object that is also a structure. - The structure has at least four elements: an array of integers to keep track of the race progress: race [i]==j when hiker i has made j steps towards the finish line. - A Boolean array: flagDropped[i] is -1 signifying that the that hiker j still holding to their flag. A value p>0 signifies that the hiker dropped their flag at position p. - A Boolean variable raceFinished signaling the end of the race. The race ends when all hikers reach the finish line at the mountain top. - The fourth element is the array rank that records the rank of a hiker in the race. Threads simulate the race process by sleeping for a random number of seconds, then taking one step, decide if the flag will be dropped at this position, and decide if the hiker checks if they still have the flag. The main thread periodically displays the progress of each hiker as an arrow (clear your screen from within your C program each time you display the progress to get the illusion that these arrows are moving!): hiker0: ---**> hiker1: --> hiker2: In the above example, hiker0 dropped their flag 3 steps behind (the *). Hat is flagDropped[0] =4. Once hiker0 realizes they lost the flag, the reset to the * position (pick up the flag) and continue from there. That is, they change race [0]=4, flagDropped[0] =1, and resume the race. Hiker2 already finished the race. Hiker2 may have dropped the flag several times along the way. More specifically: Main thread: Initialize the shared object Initialize the finishLine (this integer is chosen randomly but large enough) Create hiker threads while (!raceFinished) Display the race progress // define a function If there are hikers who reached the finish line, then rank them raceFinished = true when all hikers reach the finish line End while Join all hiker threads Display the final ranking in ascending order starting with the "gold medalist" Exit Hiker thread(id): While (i
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