Operating Systems: Project Reports
Operating Systems: Project Reports
Project Reports
Class : SE
P1 P2 P3 P4 Total
College of Mathematics and Computer Science
Content
Project 1.................................................................................................................................................. 1
Project 2.................................................................................................................................................. 2
Source code
#include <stdio.h>
#include <stdlib.h>
int board[9][9]; //The sudoku file is imported
into this board
/* You will need to 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" in the
textbook's project description) */
int valid[11]; //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 subgridCt = 2; //used to track through valid
array sections for the 3x3 subgrids
/* You will need a structure to store the
information to be passed to each thread (see
"Passing Parameters to Each Thread" in the
textbook's project description)*/
typedef struct{
int row;
int column;
}parameters;
//declare the threads
pthread_t col_thread, row_thread,
first_thread, second_thread, third_thread,
fourth_thread, fifth_thread, sixth_thread,
seventh_thread, eighth_thread, ninth_thread;
/* 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);
int main(int argc, char *argv[])
{
int j = 0; //used in for loop to read in file
elements to board array
int i = 0; //used in for loop to read in file
elements to board array
int k = 0; //used in while loop to check
valid array
}
printf("The Sudoku Puzzle is solved. \n");
return 0;
}
/*thread code for child checking all columns*/
void *column_worker(void *params)
{
int i, j;
parameters * c_worker = (parameters *)
params;
int beginCol = c_worker->column;
int beginRow = c_worker->row;
/*sorting 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;*/
}
/*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; */
else{
valid[0] = 0;
pthread_exit(0);
}
}
}
valid[0] = 1;
pthread_exit(0);
}
/*thread code for child checking all rows*/
void *row_worker(void *params){
int i, j;
else{
valid[1] = 0;
pthread_exit(0);
}
}
}
valid[1] = 1;
pthread_exit(0);
}
/*thread code for child checking all 3x3
subgrids*/
void *square_worker(void *params){
int i, j;
parameters * worker = (parameters *)
params;
int beginRow = worker->row;
int beginCol = worker->column;
else{
valid[subgridCt] = 0;
subgridCt++;
pthread_exit(0);
}
}
}
valid[subgridCt] = 1;
subgridCt++;
pthread_exit(0);
}
Source code
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 8
#define N 1024
clock_t start;
float execution_time;
pthread_t main_thread_id;
pthread_attr_t thread_attr; // thread attributes
seedArray(unsorted, N, 0, 50);
start = clock();
// get default attributes
pthread_attr_init(&thread_attr);
free(main_thread_params);
return 0;
}
// Merger Thread
void *node(void *param) {
//printf("Node Thread entered\n");
int i = 0;
while(m > 1) {
//printf("Making child params from: m = %d, n = %d,
start_index = %d\n", m, n, start);
child_params[i] = (parameters *) malloc(sizeof(parameters));
child_params[i]->m = m/2;
child_params[i]->n = n/2;
child_params[i]->start_index = n/2 + start; // (n/m)*(m/2) =
n/2
// create the thread
if (m > 2) {
//printf("Creating Node Thread: m = %d, n = %d, st_ind =
%d\n", child_params[i]->m, child_params[i]->n,
child_params[i]->start_index);
pthread_create(&child_thread_id[i], &thread_attr, node,
child_params[i]);
}
else { // last thread created should be leaf
//printf("Creating Leaf Thread: m = %d, n = %d, st_ind =
%d\n", child_params[i]->m, child_params[i]->n,
child_params[i]->start_index);
pthread_create(&child_thread_id[i], &thread_attr, leaf,
child_params[i]);
}
m/=2;
n/=2;
i++;
}
// int i will be equal to # of child threads created
pthread_exit(0);
}
// Sorter Thread
void *leaf(void *param) {
//printf("Leaf Thread entered\n");
pthread_exit(NULL);
}
}
// size here is size of each array
void mergeSortedLists(int *arr1, int *arr2, int *mergedArr, int
size) {
int i = 0;
int j = 0;
int k = 0;