DB Project Document
DB Project Document
ACKNOWLEDGMENT: ............................................................................................................... 2
ABSTRACT:................................................................................................................................... 3
1 METHODOLOGY: ................................................................................................................. 4
2 BACKGROUND: .................................................................................................................. 11
3 PHASE: I ............................................................................................................................... 11
4 PASSING PARAMETERS TO EACH THREAD:............................................................... 11
5 RETURNING RESULTS TO THE PARENT THREAD: .................................................... 11
6 CONDITIONS: ...................................................................................................................... 11
7 PHASE: II .............................................................................................................................. 16
8 CONCLUSION: .................................................................................................................... 22
ACKNOWLEDGMENT:
All praises to Almighty Allah, for making all of this happen and serving as an inspiration for future
endeavors.
A study or a project of this volume can never be the outcome of a single person or just a mere
group of dedicated students, so I express my profound sense of gratitude to those who extended
their whole-hearted help and support to me in completing my project because successful
completion of any work requires guidance and help from a number of people.
Firstly, it gives me an immense pleasure to acknowledge my institute National University of
Computer and Emerging Sciences Islamabad for providing me an opportunity in developing a
project on Sudoku by using multi-threading in Operating Systems. In addition, I wish to express
my deep sense of gratitude to Dr. Hassan Mujtaba, Associate Professor for permitting me to carry
out this project work and for his guidance and support.
I give sincere thanks to Mr. Mehran Khan and Ms. Sehr Bano, my Lab Instructors, for their
special concerns and providing sufficient information related to the topic, which helped me in
completing the project work in time and their timely guidance had been a source of inspiration in
the conduct of my project work.
Last but not the least, we extend our whole-hearted gratitude for the invaluable contribution of our
parents for their blessings and earnest affection and all those persons 'behind the veil' for their
necessary support, which enabled us to complete this project.
ABSTRACT:
This program reads in a sudoku board from a text file and incorporates it into a 9x9 matrix. The
program then creates 11 variations of the struct for row and column information. Next the program
creates the pthreads and calls the pthread functions. The functions check if each row, column and
3x3 subgrid (as determined in the struct) is valid (contains all numbers 1-9). If it is valid, the
function updates the global array valid to read 1. If any row, column or 3x3 subgrid is not valid
then the function updates the global array valid to read 0. Once all threads are finished and joined,
a loop iterates through valid to check for 1s. If all elements are 1s, print out the "is Solved"
statement and exit. If there is a 0, program prints out the "NOT Solved" statement and ends the
program.
SUDOKU SOLUTION VALIDATOR
1 METHODOLOGY:
i. The sudoku file is imported into the board of 9X9.
ii. We then declare an array of integer values that is visible to each thread. The value in
the array (0 or 1) indicates whether the worker thread's number is valid (see "Returning
Results to the Parent Thread" for more information).
int board [9][9];
iii. When the threads are joined, if that thread is a valid part of a sudoku puzzle, it returns
1 to valid [], else it returns 0.
int valid [11];
iv. We need a checker that is used to track through valid array sections for the 3x3 sub
grids.
v. We will need a structure to store the information to be passed to each thread (see
"Passing Parameters to Each Thread" for more information).
typedef struct {
int row;
int column;
} parameters;
vii. We then Declare the thread that checks columns, rows and 3x3 subgrids.
void *column_worker (void *param);
void *row_worker (void *param);
void *square_worker (void *param);
viii. We then assign values to the structure variable. Then we create multiple worker
threads by passing the information using the structure variable:
ix. We then make value returners that is used for pthread join:
void * rows void * cols void * first_square
void * second_square void * third_square void * fourth_square
void * fifth_square void * sixth_square void * seventh_square
void * eighth_square void * ninth_square
x. We then create pthreads as follows:
pthread_create (&col_thread, NULL, column_worker, (void *) checkCols);
pthread_create (&row_thread, NULL, row_worker, (void *) checkRows);
pthread_create (&first_thread, NULL, square_worker, (void *) first3by3);
pthread_create(&second_thread, NULL, square_worker, (void *) second3by3);
pthread_create (&third_thread, NULL, square_worker, (void *) third3by3);
pthread_create (&fourth_thread, NULL, square_worker, (void *) fourth3by3);
pthread_create (&fifth_thread, NULL, square_worker, (void *) fifth3by3);
pthread_create (&sixth_thread, NULL, square_worker, (void *) sixth3by3);
pthread_create (&seventh_thread, NULL, square_worker, (void *) seventh3by3);
pthread_create (&eighth_thread, NULL, square_worker, (void *) eighth3by3);
pthread_create (&ninth_thread, NULL, square_worker, (void *) ninth3by3);
xi. We then call pthread_join () for each child thread so that the parent thread waits.
pthread_join (col_thread, &cols);
pthread_join (row_thread, &rows);
pthread_join (first_thread, &first_square);
pthread_join (second_thread, &second_square);
pthread_join (third_thread, &third_square);
pthread_join (fourth_thread, &fourth_square);
pthread_join (fifth_thread, &fifth_square);
pthread_join (sixth_thread, &sixth_square);
pthread_join (seventh_thread, &seventh_square);
pthread_join (eighth_thread, &eighth_square);
pthread_join (ninth_thread, &ninth_square);
xii. Finally, after all the children returns, we check the status array that is visible to
everyone and see if it is valid. We then print out the final checking result.
xiii. Here if the thread is valid (contains all numbers 1-9) then the value of valid[k] will be
1, continue to check all elements of valid until either the array ends (k = 11) or the
value of valid[k] is 0. If the loop terminates on its own (k = 11), then print the "is
Solved" statement and * exit the program. If the value is 0 then there is a thread that
did was not a valid part of a sudoku puzzle and the whole puzzle is invalid. Print the
"NOT Solved" statement and end the program.
while (k < 11) {
if(valid[k] == 1) {
k++;
}
else {
printf ("The Sudoku Puzzle is NOT solved. \n");
exit (0);
}
}
printf ("The Sudoku Puzzle is solved. \n");
return 0;
}
xiv. Now we make thread code for child that will check all the columns.
a. For that will shall sort the sudoku column. This will put the column into a sorted
order and check for duplicates if there are duplicates, this section is not valid
and returns 0 for the column_worker.
b. if the corresponding array element for the value is 0, it has not been seen and
we will input this value to the sorted column array.
c. if the value is not 0, then the value is a duplicate and the sudoku puzzle is not
solved or valid so the value of column_worker in valid is 0.
valid [0] = 0;
pthread_exit (0);
}
}
}
valid [0] = 1;
pthread_exit (0);
}
xv. Now we make thread code for child that will check all the rows.
a. For that will shall sort the sudoku column. This will put the column into a sorted
order and check for duplicates if there are duplicates, this section is not valid
and returns 0 for the row_worker.
b. if the corresponding array element for the value is 0, it has not been seen and
we will input this value to the sorted column array.
c. if the value is not 0, then the value is a duplicate and the sudoku puzzle is not
solved or valid so the value of column_worker in valid is 0.
valid [0] = 0;
pthread_exit (0);
}
}
}
valid [0] = 1;
pthread_exit (0);
}
xvi. Now, we make the thread code for child checking all the 3X3 subgrids.
a. Here we will be sorting sudoku subgrids. This will put the 3x3 subgrid, as
determined by the beginRow and beginCol parameters into a sorted order and
check for duplicates. If there are duplicates, this section is not valid and returns
0 for the square_worker.
b. If the corresponding array element for the value is 0, and it has not been seen
then we will input this value to the sorted column array.
c. If the value is not 0, then the value is a duplicate and the sudoku puzzle is not
solved or valid so the value of square_worker in valid is 0.
int i, j;
else {
valid[subgridCt] = 0;
subgridCt++;
pthread_exit (0);
}
}
}
valid[subgridCt] = 1;
subgridCt++;
pthread_exit (0);
}
2 BACKGROUND:
`A Sudoku puzzle uses a 9 × 9 grid in which each column and
row, as well as each of the nine 3 × 3 sub grids, must contain
all of the digits 1 ··· 9. Figure presents an example of a valid
Sudoku puzzle. This project consists of designing a
multithreaded application that determines whether the
solution to a Sudoku puzzle is valid. There are several different
ways of multithreading this application. One suggested
strategy is to create threads that check the following criteria:
3 PHASE: I
• A thread to check that each column contains the digits
1 through 9.
• A thread to check that each row contains the digits 1 through 9.
• Nine threads to check that each of the 3 × 3 sub grids contains the digits 1 through 9 columns,
you could create nine separate threads and have each of them check one column.
6 CONDITIONS:
• Threads are not allowed to update other part of the array. Other threads can only read the
value from the array. (For example, an i thread can only write on ith location, for other
parts it can only read from the array.)
• When thread returns values to Main thread and it is writing on array, no other thread can
read array value.
• Thread will return the value to Main thread.
• Here you must synchronize the task and apply mutex lock.
• Thread can cancel the other thread, if found an invalid entry. But you must set the criteria
for cancelation. (For which thread you need to set accept cancelation or not.)
• Storing the invalid entries and their indexes
7 PHASE: II
Phase 1 of this project is about the validation performed on
the matrix (9 x 9). In phase 2, you must initialize a matrix
with at least 2 invalid entries and provide its solution. You
can store the matrix in a file. Each thread is assigned to each
row and column and change the corresponding values if found
erroneous. (Same as followed in phase 1). Solution of the
sudoku puzzle, threads attributes must be initialized in start.
So that Thread cancelation can be achieved. (As in solution
thread has to swap the values with other threads, you need to
keep track of changes. If a thread
change another threads value, thread don’t allow to change its
value it will cancel the other thread.)
When a thread wants to swap the position of digits, it sends the signal to corresponding thread
(Optional). Each thread must have a criterion to allow a thread to swap its position with other
threads. Limited threads will be running using semaphores to balance the overhead and security
issues, e.g. while updating the matrix no other thread will read the data. Threads should be created
efficiently. After resolving the invalid entries, you must have to validate the solution and display
the result.
8 CONCLUSION: