Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/shm.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <errno.h>
- #include <time.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <semaphore.h>
- char *boards[248], *boardSize[248], *token;
- char* boardPtr[9];
- int isSuccessTot = 0;
- typedef struct
- {
- int row;
- int col;
- int value;
- int threadNum;
- } boardElement;
- pthread_mutex_t mutex;
- void* validateRow(boardElement board[atoi(boardSize)]);
- boardElement *insertionSort(boardElement arr[], int n);
- int isSuccessTotal(int n);
- int main(int argc, char *argv[])
- {
- int i = 0;
- int counter = 0;
- char boardN[248];
- printf("Please enter the size of the board (value of n).\n");
- fgets(boardN, sizeof boardN, stdin);
- int boardSizeF = atoi(boardN) * atoi(boardN);
- sprintf(boardSize, "%d", boardSizeF);
- printf("boardSize %s\n", boardSize);
- boardElement board[atoi(boardSize)][atoi(boardSize)];
- for (int counter = 0; counter < atoi(boardSize); counter++) {
- printf("Please enter a comma seperated valid soduko board row\n");
- fgets(boards, sizeof boards, stdin);
- token = strtok_r(boards, ",", &boardPtr[counter]);
- board[counter][0].value = atoi(token);
- int i = 1;
- while((token = strtok_r(NULL, ",", &boardPtr[counter])) != NULL) {
- board[counter][i].value = atoi(token);
- i++;
- }
- }
- pthread_t tid[atoi(boardSize) + atoi(boardSize)];
- // init the mutex's
- int status = pthread_mutex_init(&mutex, NULL);
- if (status == -1)
- {
- errno = status;
- perror("pthreadmutexinit failed");
- exit(1);
- }
- boardElement boardColumns[atoi(boardSize)][atoi(boardSize)];
- //Create the column arrays by 'inverting' the rows and columns
- for (int i = 0; i < atoi(boardSize); i++) {
- for (int j = 0; j < atoi(boardSize); j++) {
- boardColumns[j][i].value = board[i][j].value;
- }
- }
- boardElement boardBlocks[atoi(boardSize)][atoi(boardSize)];
- //Create the array for the blocks
- if (boardSizeF == 2) {
- boardBlocks[0][0].value = board[0][0].value;
- boardBlocks[0][1].value = board[0][1].value;
- boardBlocks[0][3].value = board[1][0].value;
- boardBlocks[0][4].value = board[1][1].value;
- boardBlocks[1][0].value = board[0][2].value;
- boardBlocks[1][1].value = board[0][3].value;
- boardBlocks[1][3].value = board[1][2].value;
- boardBlocks[1][4].value = board[1][3].value;
- boardBlocks[2][0].value = board[2][0].value;
- boardBlocks[2][1].value = board[2][1].value;
- boardBlocks[2][3].value = board[3][0].value;
- boardBlocks[2][4].value = board[3][1].value;
- boardBlocks[3][0].value = board[2][2].value;
- boardBlocks[3][1].value = board[2][3].value;
- boardBlocks[3][3].value = board[3][2].value;
- boardBlocks[3][4].value = board[3][3].value;
- }
- if (boardSizeF == 3) {
- for (i = 0; i < 9; i++) {
- boardBlocks[i][0].value = board[0 + 3 * (i / 3)][0 + 3 * (i / 3)].value;
- boardBlocks[i][1].value = board[0 + 3 * (i / 3)][1 + 3 * (i / 3)].value;
- boardBlocks[i][2].value = board[0 + 3 * (i / 3)][2 + 3 * (i / 3)].value;
- boardBlocks[i][3].value = board[1 + 3 * (i / 3)][0 + 3 * (i / 3)].value;
- boardBlocks[i][4].value = board[1 + 3 * (i / 3)][1 + 3 * (i / 3)].value;
- boardBlocks[i][5].value = board[1 + 3 * (i / 3)][2 + 3 * (i / 3)].value;
- boardBlocks[i][6].value = board[2 + 3 * (i / 3)][0 + 3 * (i / 3)].value;
- boardBlocks[i][7].value = board[2 + 3 * (i / 3)][1 + 3 * (i / 3)].value;
- boardBlocks[i][8].value = board[2 + 3 * (i / 3)][2 + 3 * (i / 3)].value;
- }
- }
- //Check rows
- for (int i = 0; i < atoi(boardSize); i++) {
- int rc = pthread_create(&tid[i], NULL, &validateRow, board[i]);
- if (rc != 0) {
- errno = rc;
- perror("child: pthread create");
- exit(1);
- }
- }
- //Check columns
- for (int i = 0; i < atoi(boardSize); i++) {
- int rc = pthread_create(&tid[atoi(boardSize) + i], NULL, &validateRow, boardColumns[i]);
- if (rc != 0) {
- errno = rc;
- perror("child: pthread create");
- exit(1);
- }
- }
- //Check blocks
- for (int i = 0; i < atoi(boardSize); i++) {
- int rc = pthread_create(&tid[atoi(boardSize) + atoi(boardSize) + i], NULL, &validateRow, boardColumns[i]);
- if (rc != 0) {
- errno = rc;
- perror("child: pthread create");
- exit(1);
- }
- }
- printf("waiting for threads to join\n");
- for (int j = 0; j < (atoi(boardSize) + atoi(boardSize) + atoi(boardSize)); j++) {
- void *status;
- int rc = pthread_join(tid[j], &status);
- if (rc != 0) {
- errno = rc;
- perror("child: pthread join");
- exit(1);
- }
- }
- if ( isSuccessTot == atoi(boardSize) + atoi(boardSize) + atoi(boardSize))
- printf("Valid board\n");
- else {
- printf("Invalid board\n");
- }
- printf("Reached end\n");
- exit(0);
- }
- void* validateRow(boardElement boards[atoi(boardSize)]) {
- //Sort the Board Elements
- pthread_mutex_lock(&mutex);
- boardElement *row = insertionSort(boards, atoi(boardSize));
- //Check the sorted list matches the solution list
- int isSuccess = 1;
- for (int i = 0; i < atoi(boardSize); i++) {
- if (row[i].value != i + 1) {
- isSuccess = 0;
- }
- }
- int uploadSuccess = isSuccessTotal(isSuccess);
- pthread_mutex_unlock(&mutex);
- return NULL;
- }
- /* Insertion Sort */
- boardElement *insertionSort(boardElement arr[], int n)
- {
- int i, key, j;
- for (i = 1; i < n; i++) {
- key = arr[i].value;
- j = i - 1;
- while (j >= 0 && arr[j].value > key) {
- arr[j + 1].value = arr[j].value;
- j = j - 1;
- }
- arr[j + 1].value = key;
- }
- return arr;
- }
- int isSuccessTotal(int n) {
- isSuccessTot = isSuccessTot + n;
- return isSuccessTot;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement