Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. #include <pthread.h>
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys/shm.h>
  8. #include <sys/stat.h>
  9. #include <sys/mman.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <time.h>
  14. #include <sys/types.h>
  15. #include <unistd.h>
  16. #include <semaphore.h>
  17. char *boards[248], *boardSize[248], *token;
  18. char* boardPtr[9];
  19. int isSuccessTot = 0;
  20.  
  21.  
  22. typedef struct
  23. {
  24. int row;
  25. int col;
  26. int value;
  27. int threadNum;
  28. } boardElement;
  29.  
  30.  
  31. pthread_mutex_t mutex;
  32.  
  33.  
  34. void* validateRow(boardElement board[atoi(boardSize)]);
  35. boardElement *insertionSort(boardElement arr[], int n);
  36. int isSuccessTotal(int n);
  37. int main(int argc, char *argv[])
  38. {
  39.  
  40.  
  41. int i = 0;
  42. int counter = 0;
  43. char boardN[248];
  44. printf("Please enter the size of the board (value of n).\n");
  45. fgets(boardN, sizeof boardN, stdin);
  46. int boardSizeF = atoi(boardN) * atoi(boardN);
  47. sprintf(boardSize, "%d", boardSizeF);
  48. printf("boardSize %s\n", boardSize);
  49.  
  50. boardElement board[atoi(boardSize)][atoi(boardSize)];
  51. for (int counter = 0; counter < atoi(boardSize); counter++) {
  52.  
  53. printf("Please enter a comma seperated valid soduko board row\n");
  54. fgets(boards, sizeof boards, stdin);
  55.  
  56. token = strtok_r(boards, ",", &boardPtr[counter]);
  57. board[counter][0].value = atoi(token);
  58. int i = 1;
  59. while((token = strtok_r(NULL, ",", &boardPtr[counter])) != NULL) {
  60. board[counter][i].value = atoi(token);
  61. i++;
  62. }
  63. }
  64.  
  65. pthread_t tid[atoi(boardSize) + atoi(boardSize)];
  66.  
  67. // init the mutex's
  68. int status = pthread_mutex_init(&mutex, NULL);
  69. if (status == -1)
  70. {
  71. errno = status;
  72. perror("pthreadmutexinit failed");
  73. exit(1);
  74. }
  75.  
  76.  
  77. boardElement boardColumns[atoi(boardSize)][atoi(boardSize)];
  78. //Create the column arrays by 'inverting' the rows and columns
  79. for (int i = 0; i < atoi(boardSize); i++) {
  80. for (int j = 0; j < atoi(boardSize); j++) {
  81. boardColumns[j][i].value = board[i][j].value;
  82. }
  83. }
  84.  
  85. boardElement boardBlocks[atoi(boardSize)][atoi(boardSize)];
  86. //Create the array for the blocks
  87.  
  88. if (boardSizeF == 2) {
  89. boardBlocks[0][0].value = board[0][0].value;
  90. boardBlocks[0][1].value = board[0][1].value;
  91. boardBlocks[0][3].value = board[1][0].value;
  92. boardBlocks[0][4].value = board[1][1].value;
  93.  
  94. boardBlocks[1][0].value = board[0][2].value;
  95. boardBlocks[1][1].value = board[0][3].value;
  96. boardBlocks[1][3].value = board[1][2].value;
  97. boardBlocks[1][4].value = board[1][3].value;
  98.  
  99. boardBlocks[2][0].value = board[2][0].value;
  100. boardBlocks[2][1].value = board[2][1].value;
  101. boardBlocks[2][3].value = board[3][0].value;
  102. boardBlocks[2][4].value = board[3][1].value;
  103.  
  104. boardBlocks[3][0].value = board[2][2].value;
  105. boardBlocks[3][1].value = board[2][3].value;
  106. boardBlocks[3][3].value = board[3][2].value;
  107. boardBlocks[3][4].value = board[3][3].value;
  108. }
  109.  
  110. if (boardSizeF == 3) {
  111. for (i = 0; i < 9; i++) {
  112. boardBlocks[i][0].value = board[0 + 3 * (i / 3)][0 + 3 * (i / 3)].value;
  113. boardBlocks[i][1].value = board[0 + 3 * (i / 3)][1 + 3 * (i / 3)].value;
  114. boardBlocks[i][2].value = board[0 + 3 * (i / 3)][2 + 3 * (i / 3)].value;
  115. boardBlocks[i][3].value = board[1 + 3 * (i / 3)][0 + 3 * (i / 3)].value;
  116. boardBlocks[i][4].value = board[1 + 3 * (i / 3)][1 + 3 * (i / 3)].value;
  117. boardBlocks[i][5].value = board[1 + 3 * (i / 3)][2 + 3 * (i / 3)].value;
  118. boardBlocks[i][6].value = board[2 + 3 * (i / 3)][0 + 3 * (i / 3)].value;
  119. boardBlocks[i][7].value = board[2 + 3 * (i / 3)][1 + 3 * (i / 3)].value;
  120. boardBlocks[i][8].value = board[2 + 3 * (i / 3)][2 + 3 * (i / 3)].value;
  121. }
  122. }
  123. //Check rows
  124. for (int i = 0; i < atoi(boardSize); i++) {
  125. int rc = pthread_create(&tid[i], NULL, &validateRow, board[i]);
  126. if (rc != 0) {
  127. errno = rc;
  128. perror("child: pthread create");
  129. exit(1);
  130. }
  131. }
  132.  
  133.  
  134. //Check columns
  135. for (int i = 0; i < atoi(boardSize); i++) {
  136. int rc = pthread_create(&tid[atoi(boardSize) + i], NULL, &validateRow, boardColumns[i]);
  137. if (rc != 0) {
  138. errno = rc;
  139. perror("child: pthread create");
  140. exit(1);
  141. }
  142. }
  143.  
  144. //Check blocks
  145. for (int i = 0; i < atoi(boardSize); i++) {
  146. int rc = pthread_create(&tid[atoi(boardSize) + atoi(boardSize) + i], NULL, &validateRow, boardColumns[i]);
  147. if (rc != 0) {
  148. errno = rc;
  149. perror("child: pthread create");
  150. exit(1);
  151. }
  152. }
  153. printf("waiting for threads to join\n");
  154. for (int j = 0; j < (atoi(boardSize) + atoi(boardSize) + atoi(boardSize)); j++) {
  155. void *status;
  156. int rc = pthread_join(tid[j], &status);
  157. if (rc != 0) {
  158. errno = rc;
  159. perror("child: pthread join");
  160. exit(1);
  161. }
  162. }
  163. if ( isSuccessTot == atoi(boardSize) + atoi(boardSize) + atoi(boardSize))
  164. printf("Valid board\n");
  165. else {
  166. printf("Invalid board\n");
  167. }
  168. printf("Reached end\n");
  169. exit(0);
  170. }
  171.  
  172. void* validateRow(boardElement boards[atoi(boardSize)]) {
  173. //Sort the Board Elements
  174.  
  175. pthread_mutex_lock(&mutex);
  176.  
  177. boardElement *row = insertionSort(boards, atoi(boardSize));
  178.  
  179. //Check the sorted list matches the solution list
  180. int isSuccess = 1;
  181. for (int i = 0; i < atoi(boardSize); i++) {
  182. if (row[i].value != i + 1) {
  183. isSuccess = 0;
  184. }
  185. }
  186. int uploadSuccess = isSuccessTotal(isSuccess);
  187. pthread_mutex_unlock(&mutex);
  188.  
  189. return NULL;
  190.  
  191. }
  192.  
  193. /* Insertion Sort */
  194. boardElement *insertionSort(boardElement arr[], int n)
  195. {
  196. int i, key, j;
  197. for (i = 1; i < n; i++) {
  198. key = arr[i].value;
  199. j = i - 1;
  200.  
  201. while (j >= 0 && arr[j].value > key) {
  202. arr[j + 1].value = arr[j].value;
  203. j = j - 1;
  204. }
  205. arr[j + 1].value = key;
  206. }
  207.  
  208. return arr;
  209. }
  210.  
  211. int isSuccessTotal(int n) {
  212. isSuccessTot = isSuccessTot + n;
  213. return isSuccessTot;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement