Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please implement your program by using the second approach explained above( i.e by using a global variable to indicate that all threads finished their job).

Please implement your program by using the second approach explained above( i.e by using a global variable to indicate that all threads finished their job). Note that you will need to ensure proper synchronizarion mechanism between the threads so that this approach works correctly.You will be using either Pthreads mutex or Windows Mutex objects depending on your developmt

image text in transcribed

2. (50 points) Sudolcu Solution Validator. A sudou puzzle uses a 9x grid in which each columns and row, as well as each of the nine 3x3 sub-grids must contain a f the digits l 9. Figure 2 below presents an example of a valid Sudoku puzzle. You are required to write the program m C Figure 2 This project consists of designing a multithreaded application that determines whether the solution to a Sudoku puzzle is valid or not There are several . A tread to check one column of the grid contains the digits 1 through 9, ie., nine separate threads to check all columns different ways of multithreading this application. One suggested strategy is to create threads that check the following criteria: 2. A thread to check one row of the gird contains the digits1 through 9. ie., nine separate threads to check all rows. 3. A thread to check that one of the 3x3 sub-grids contains the digits 1 through 9,i.e, nine threads to check all sub-grids. With the above strategy there are 27 worker threads (plus a main thread) to check whether a given Sudoku grid is valid or not Your program should ask the user to enter 81 digits from the console (in row-major order, i.e., first the numbers in the first row are entered, then the numbers in the second row, etc.), and the main thread ofyour program should create the worker threads, passing each worker thread the locations within the grid that it must check. Note that the specific cells to be checked by column validators, row validators, and sub-grid validators are different from each other. You should design your worker threads according to their duties, ensure correct measures on how to obtain the location information from the main thread. In any case this will require passing several parameters to each thread. The simplest approach is to create a data structure using a struct construct of C. For example, a structure to pass the row and column where a thread must begin its validation would appear as follows: structure for passing data to threads typedef struct int ro int column; l parameters: Both Pthreads and Windows programs can create worker threads using a strategy similar to that shown below parameters *data = data->column1; (parameters *) malloc(ggaea,/parameters)??data->row= 1; No create the thread passing it data as parameter / The data pointer will be passed to either the pthreadcaate Pthreads) function or the Createead0 (Windows) function, which in tum will pass it as a parameter to the function to run as a separate thread. Each worker thread is assigned the task of determining the validity of a paticulr region of the Sudoku puzzle. Once a worker thread has performed this check, it must pass its results back to the parent. One way to handle this is to create a global array of integer values that is visible to each thread. The index in this array corresponds to the worker thread. If a worker thread sets its corresponding value to 1, it is indicating that its region of the Sudoku puzzle is valid. A value of 0 would indicate otherwise. When all worker threads havre completed, the main thread can check each entry in the result aray to determine if the Sudoku puzzle is valid or not. After printing the result to the console, the program should exit. It is apparent firom the above discussion that the main thread should wait for all threads to finish their duties before reaching a final decision. This means that the main thread should wait for all the worker threads to exit, and then makes its final decision. You can implement this with the ptbread.iain function of the Pthreads library (Linux, MAC OS) or via the (Windows) fiunction from the Win32 API, as we have seen during our lectures. Altermatively, you can achieve this step va another global integer variable, say rea A R -Rtea, mitally set to zero, and ncreme ted by every thread when that thread finishes its job, and before it exits. When the value ofthis variable reaches 27, it means that every thread finished its job, and the main thread can finalize its decision, output the result, and exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions