Question

C language Credit for work. Please design a multithreaded application in C with Pthreads - it...

C language

Credit for work.

Please design a multithreaded application in C with Pthreads - it determines whether the solution to a Sudoku puzzle is
valid.

Validate two puzzles

In your program, please hard-code the following two 9x9 grids (say in two variables puzzle1 and puzzle2), and
determine if each solution is valid. While each grid should be validated by multiple parallel threads, you can validate
puzzle1 and then puzzle2 in sequential order (as illustrated) by a single invocation of your program. Note you are
not allowed to invoke the program twice, one for each puzzle.

puzzle 1 puzzle 2

void sudoku(…)
{


}
int main()
{ …
sudoku(puzzle1);

sudoku(puzzle2);
return 0;
}

1,8,3,5,7,6,2,4,9,
4,6,7,1,2,9,3,5,8,
9,2,5,4,3,8,7,2,6,
8,7,2,3,5,1,6,9,4,
6,9,4,7,8,2,1,3,5,
3,5,1,6,9,4,8,7,2,
5,1,9,2,6,0,4,8,7,
7,4,6,8,1,5,9,2,3,
2,3,8,9,4,7,5,6,1
5,9,6,7,1,4,3,2,8,
2,7,3,8,5,9,1,4,6,
8,1,4,2,3,6,7,5,9,
9,6,7,4,2,8,5,1,3,
1,2,5,3,9,7,6,8,4,
4,3,8,1,6,5,9,7,2,
3,5,1,6,8,2,4,9,7,
7,8,9,5,4,3,2,6,1,
6,4,2,9,7,1,8,3,5

Identifying location and reason for an invalid puzzle
When a row thread/column thread/subgrid thread identifies any problem (so that the puzzle is not valid), the thread
must print out the problematic row/column/puzzle location and the specific error to stdout (to be included in the
screenshots). For example,


puzzle[1][2] = 3, duplicated value
puzzle[4][5] = 10, not between 1 and 9
row[6], missing value 7
column[8], missing value 9


For any given problematic cell in a puzzle, your threads should generate multiple errors (identified by row thread, and/or
column thread, and/or subgrid thread).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source code:-

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

#define number_threads 11
#define grid_size 9

typedef struct parameters{
       int row;
       int column;
        int puzzle_number;
        int id;
   }parameters;

int answer=1;//Sudoku is valid in default

int result[number_threads]={0};

pthread_t workerT[number_threads];//11 worker threads in total

struct parameters *data[number_threads];

   int puzzle1[grid_size][grid_size]={
                           {1,8,3,5,7,6,2,4,9},
                           {4,6,7,1,2,9,3,5,8},
                           {9,2,5,4,3,8,7,2,6},
                           {8,7,2,3,5,1,6,9,4},
                           {6,9,4,7,8,2,1,3,5},
                           {3,5,1,6,9,4,8,7,2},
                           {5,1,9,2,6,0,4,8,7},
                           {7,4,6,8,1,5,9,2,3},
                           {2,3,8,9,4,7,5,6,1}
       };

   int puzzle2[grid_size][grid_size]={
                            {5,9,6,7,1,4,3,2,8},
                            {2,7,3,8,5,9,1,4,6},
                            {8,1,4,2,3,6,7,5,9},
                            {9,6,7,4,2,8,5,1,3},
                            {1,2,5,3,9,7,6,8,4},
                            {4,3,8,1,6,5,9,7,2},
                            {3,5,1,6,8,2,4,9,7},
                            {7,8,9,5,4,3,2,6,1},
                            {6,4,2,9,7,1,8,3,5}
       };


   void *check_allRowsPuzzle(void *params){
       parameters *pointer=(parameters *)params;
       int startR=pointer->row;
       int startC=pointer->column;
       int puzzle[grid_size][grid_size];
       if(pointer->puzzle_number==1){
           memcpy(puzzle,puzzle1,sizeof puzzle);
       }else{
           memcpy(puzzle,puzzle2,sizeof puzzle);
       }
       int array[9]={0};
       for(int i=startR;i<grid_size;i++){
           memset(array,0,sizeof(array));
           for(int j=startC;j<grid_size;j++){
               int integer=puzzle[i][j];
               if(integer>0){
               array[integer-1]=1;}
           }

       for(int p=0;p<9;p++){
               if(array[p]==0){
                    result[pointer->id]=0;
                     answer=0;
                    for(int d=0;d<number_threads;d++){
                    pthread_cancel(workerT[d]);
                       }

                    pthread_exit(NULL);
               }
           }
       }
       result[pointer->id]=1;

   }

   void *check_allColumnsPuzzle(void *params){

       parameters *pointer=(parameters *)params;
           int startR=pointer->row;
           int startC=pointer->column;
           int puzzle[grid_size][grid_size];
           if(pointer->puzzle_number==1){
               memcpy(puzzle,puzzle1,sizeof puzzle);
           }else{
               memcpy(puzzle,puzzle2,sizeof puzzle);
           }
           int array[9]={0};
           for(int i=startC;i<grid_size;i++){
               memset(array,0,sizeof(array));
               for(int j=startR;j<grid_size;j++){
                   int integer=puzzle[i][j];
                   if(integer>0){
                   array[integer-1]=1;}
               }

               for(int p=0;p<9;p++){
                   if(array[p]==0){
                       result[pointer->id]=0;
                       answer=0;
                       pthread_exit(NULL);
                   }
               }
           }
           result[pointer->id]=1;

   }

   void *check_SquaresPuzzle(void *params){
       parameters *pointer=(parameters *)params;
       int startR=pointer->row;
               int startC=pointer->column;
               int puzzle[grid_size][grid_size];
               if(pointer->puzzle_number==1){
                   memcpy(puzzle,puzzle1,sizeof puzzle);

               }else{
                   memcpy(puzzle,puzzle2,sizeof puzzle);
               }
               int array[9]={0};
               for(int i=startC;i<startC+3;i++){
                   for(int j=startR;j<startR+3;j++){
                       int integer=puzzle[i][j];
                       if(integer>0){
                       array[integer-1]=1;}
                   }
               }

               for(int p=0;p<9;p++){
                   if(array[p]==0){
                       result[pointer->id]=0;
                        answer=0;

                       pthread_exit(NULL);
                   }
               }
               result[pointer->id]=1;
   }

   void sudoku(int puzzle){
   for(int b=0;b<number_threads;b++){
       data[b]->puzzle_number=puzzle;
   }

   //Create threads

         for(int k=0;k<9;k++){
           pthread_create(&workerT[k], NULL,check_SquaresPuzzle, data[k]);
         }
         pthread_create(&workerT[9],NULL,check_allRowsPuzzle,data[9]);
         pthread_create(&workerT[10],NULL,check_allColumnsPuzzle,data[10]);

         //Join threads
         for(int h=0;h<number_threads;h++){
           pthread_join(workerT[h],NULL);
         }

         //Check validity of the entire puzzle

          if(answer){
              printf("Puzzle %d is valid.\n",puzzle);
          }
          else{
              printf("Puzzle %d is not valid.\n",puzzle);
          }

         printf("\n");

}


int main(void) {


    printf("Welcome to suduko play from google\n");
    printf("\n");

    //struct parameters *data[11];
    int i=0;
    int j=0;
    int index=0;
   while(i<=6){//Initialize struc elements

       while(j<=6){
       data[index]=(parameters *)malloc(sizeof(parameters));
       data[index]->row=i;
       data[index]->column=j;
       //data[index]->puzzle_number=1;
       data[index]->id=index;
       index++;
       j=j+3;
       }
        i=i+3;
       j=0;


   }
           data[9]=(parameters *)malloc(sizeof(parameters));
           data[9]->row=0;
           data[9]->column=0;
           //data[9]->puzzle_number=1;
           data[9]->id=9;

           data[10]=(parameters *)malloc(sizeof(parameters));
           data[10]->row=0;
            data[10]->column=0;
            //data[10]->puzzle_number=1;
            data[10]->id=10;


            //Print Puzzle One
            for(int q=0;q<grid_size;q++){
                   for(int w=0;w<grid_size;w++){
                       printf("%d",puzzle1[q][w]);
                   }
                   printf("\n");
                }
                printf("\n");

                sudoku(1);//Run Sudoku checker for Puzzle 1
                memset(result,0,sizeof(result));//Re-set result array get ready for 2nd Sudoku check

                //Print Puzzle Two
            for(int f=0;f<grid_size;f++){
               for(int g=0;g<grid_size;g++){
                   printf("%d",puzzle2[f][g]);
               }
               printf("\n");
            }
            printf("\n");

            sudoku(2);//Run Sudoku checker for Puzzle 2


//At the end, free memory
     for(int y=0;y<11;y++){
      free(data[y]);}


     return EXIT_SUCCESS;
}


------------------------------------------------------------------------------------------------------

Output:-

Add a comment
Know the answer?
Add Answer to:
C language Credit for work. Please design a multithreaded application in C with Pthreads - it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • * Your goal in this exercise is to practice recursion and * to see how a...

    * Your goal in this exercise is to practice recursion and * to see how a properly written recursive solution can * take care of fairly complicated tasks with a few lines * of (well thought out) code. * * We will be solving Sudoku puzzles. In case you have never * solved or seen a Sudoku, you can learn more about them * here: * * https://en.wikipedia.org/wiki/Sudoku * * Your task if to write a function that takes an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT