Professional Documents
Culture Documents
Project Report: Alphabetic Sudoku Verifier
Project Report: Alphabetic Sudoku Verifier
Project Report: Alphabetic Sudoku Verifier
Normally a sudoku puzzle uses 9 by 9 grid in which each row and column and each of the 3 by 3
sub grids must contain 1 to 9 numbers. But we modify the puzzle to 4 by 4 grids and instead of
numbers we use alphabets. This project consists of designing a multi-threaded application that
determine whether a solution of the sudoku is valid or not.
OS Concepts
[Here write in detail mention which OS features/concepts/modules are used in your project and
how did you apply in your code.
This program reads in a sudoku board and incorporates it into a 4 by 4 matrix. The program then
creates variations of the struct for row and column information. Next the program creates pthread
and calls the pthread functions. The functions check if row, column and 2 by 2 sub grid is valid
that means each row and column contains A to D alphabets. If it is valid the function updates the
global array valid to read 1. If any row, col or grid doesn’t contain all alphabets (A-D) then it
will read 0. Once all threads are finished and joined, a loop iterates through valid to check for 1s.
If all elements are 1s, it will print out “solved” and exit. If there is a zero the it will print out” not
solved” and program ends.
1) Create the threads
pthread_t thread_rows, thread_cols, thread1, thread2, thread3,
thread4;
1
pthread_create(&thread_cols, NULL, walk_cols, (void *) param0);
pthread_create(&thread1, NULL, check_square, (void *) param1);
pthread_create(&thread2, NULL, check_square, (void *) param2);
pthread_create(&thread3, NULL, check_square, (void *) param3);
pthread_create(&thread4, NULL, check_square, (void *) param4);
Contribution
[If you are work in a group, please mention each member’s contribution to your project work.]
https://replit.com/join/fjpozfrf-irenesultana
2
Detailed Source Code:
/**
* Structure that holds the parameters passed to a thread.
* This specifies where the thread should start verifying.
*/
typedef struct
{
// The starting row.
int row;
// The starting column.
int col;
// The pointer to the board.
char (* board)[4];
} parameters;
/***************
* ENTRY POINT
**************/
int main(void)
{
// ====== Create the board =======
char board[4][4] = {
{'A','B','C','D'},
{'B','C','D','A'},
{'C','D','A','B'},
{'D','A','B','C'},
};
// ====== Create the parameter for the columns and rows check =======
3
parameters * param0 = (parameters *) malloc(sizeof(parameters));
param0->row = 0;
param0->col = 0;
param0->board = board;
// First 2x2
parameters * param1 = (parameters *) malloc(sizeof(parameters));
param1->row = 0;
param1->col = 0;
param1->board = board;
// Second 2x2
parameters * param2 = (parameters *) malloc(sizeof(parameters));
param2->row = 0;
param2->col = 2;
param2->board = board;
// Third 2x2
parameters * param3 = (parameters *) malloc(sizeof(parameters));
param3->row = 2;
param3->col = 0;
param3->board = board;
// Fourth 2x2
parameters * param4 = (parameters *) malloc(sizeof(parameters));
param4->row = 2;
param4->col = 2;
param4->board = board;
4
pthread_create(&thread_rows, NULL, walk_rows, (void *) param0);
pthread_create(&thread_cols, NULL, walk_cols, (void *) param0);
pthread_create(&thread1, NULL, check_square, (void *) param1);
pthread_create(&thread2, NULL, check_square, (void *) param2);
pthread_create(&thread3, NULL, check_square, (void *) param3);
pthread_create(&thread4, NULL, check_square, (void *) param4);
return 0;
}
/**
* Checks each row if it contains all alphabets A-D
* @param void * The parameters (pointer).
* @return void * 1 if all rows contain all alphabets A-D, 0 otherwise.
*/
void * walk_rows(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
for (int i = startRow; i < 4; ++i) {
int row[5] = {0};
for (int j = startCol; j < 4; ++j) {
int val = data->board[i][j];
5
if (row[val] == 0) {
return (void *) 1;
}
else{
row[val] = 0;
}
}
}
return (void *) 1;
}
/**
* Checks each column if it contains all alphabets A-D.
* @param void * The parameters (pointer).
* @return void * 1 if all rows contain all alphabets A-D, 0 otherwise.
*/
void * walk_cols(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
for (int i = startCol; i < 4; ++i) {
int col[5] = {0};
for (int j = startRow; j < 4; ++j) {
int val = data->board[j][i];
if (col[val] == 0) {
return (void *) 1;
}
else{
col[val] = 0;
}
}
}
return (void *) 1;
}
/**
* Checks if a square of size 2x2 contains all alphabets A-D.
* @param void * The parameters (pointer).
* @return void * 1 if all rows contain all alphabets A-D, 0 otherwise.
*/
void * check_square(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
int saved[5] = {0};
6
for (int i = startRow; i < startRow + 2; ++i) {
for (int j = startCol; j < startCol + 2; ++j) {
int val = data->board[i][j];
if (saved[val] == 0) {
return (void *) 1;
}
else{
saved[val] = 0;
}
}
}
return (void *) 1;
}