0% found this document useful (0 votes)
22 views

Assignment Task

Uploaded by

Ayesha Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Assignment Task

Uploaded by

Ayesha Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <stdio.

h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>

int main() {
struct timeval start_time, end_time, before_switch, after_switch;
int num_context_switches = 0;
double waiting_time = 0.0;

gettimeofday(&start_time, NULL); // Record submission time

pid_t pid = fork();

if (pid < 0) {
printf("Fork failed\n");
return 1;
} else if (pid == 0) {
gettimeofday(&before_switch, NULL); // Record time before context switch
sched_yield(); // Yield to let the parent continue
gettimeofday(&after_switch, NULL); // Record time after context switch
exit(0);
} else {
gettimeofday(&before_switch, NULL); // Record time before waiting for child
wait(NULL); // Wait for child process to finish
gettimeofday(&after_switch, NULL); // Record time after child process finished
}
gettimeofday(&end_time, NULL); // Record completion time

// Calculate waiting time in milliseconds


waiting_time = (double)(after_switch.tv_sec - before_switch.tv_sec) * 1000.0 +
(double)(after_switch.tv_usec - before_switch.tv_usec) / 1000.0;

// Calculate number of context switches


num_context_switches = (after_switch.tv_sec - before_switch.tv_sec) * 1000000 +
(after_switch.tv_usec - before_switch.tv_usec);

// Print results
printf("Submission Time: %ld seconds, %ld milliseconds\n", start_time.tv_sec/1000000000,
start_time.tv_usec / 1000);
printf("Completion Time: %ld seconds, %ld milliseconds\n", end_time.tv_sec/1000000000,
end_time.tv_usec / 1000);
printf("Number of Context Switches: %d\n", num_context_switches);
printf("Waiting Time: %f milliseconds\n", waiting_time);

return 0;
}

Task no 2:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define SIZE 9
#define NUM_THREADS 27

int sudoku[SIZE][SIZE];

// Structure to hold thread arguments


typedef struct {
int row;
int col;
} ThreadArgs;

// Function to check if a given row is valid


void *isRowValid(void *param) {
ThreadArgs *args = (ThreadArgs *)param;
int row = args->row;
int frequency[SIZE] = {0};

for (int col = 0; col < SIZE; col++) {


int num = sudoku[row][col];
if (frequency[num - 1] == 1 || num < 1 || num > 9) {
pthread_exit(NULL);
}
frequency[num - 1] = 1;
}
pthread_exit((void *)1);
}
// Function to check if a given column is valid
void *isColValid(void *param) {
ThreadArgs *args = (ThreadArgs *)param;
int col = args->col;
int frequency[SIZE] = {0};

for (int row = 0; row < SIZE; row++) {


int num = sudoku[row][col];
if (frequency[num - 1] == 1 || num < 1 || num > 9) {
pthread_exit(NULL);
}
frequency[num - 1] = 1;
}
pthread_exit((void *)1);
}

// Function to check if a given 3x3 subgrid is valid


void *isSubgridValid(void *param) {
ThreadArgs *args = (ThreadArgs *)param;
int startRow = args->row;
int startCol = args->col;
int frequency[SIZE] = {0};

for (int row = startRow; row < startRow + 3; row++) {


for (int col = startCol; col < startCol + 3; col++) {
int num = sudoku[row][col];
if (frequency[num - 1] == 1 || num < 1 || num > 9) {
pthread_exit(NULL);
}
frequency[num - 1] = 1;
}
}
pthread_exit((void *)1);
}

int main() {
// Sudoku grid
int inputSudoku[9][9] = {
{6, 2, 4, 5, 3, 9, 1, 8, 7},
{5, 1, 9, 7, 2, 8, 6, 3, 4},
{8, 3, 7, 6, 1, 4, 2, 9, 5},
{1, 4, 3, 8, 6, 5, 7, 2, 9},
{9, 5, 8, 2, 4, 7, 3, 6, 1},
{7, 6, 2, 3, 9, 1, 4, 5, 8},
{3, 7, 1, 9, 5, 6, 8, 4, 2},
{4, 9, 6, 1, 8, 2, 5, 7, 3},
{2, 8, 5, 4, 7, 3, 9, 1, 6}
};

// Copy Sudoku to sudoku array


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
sudoku[i][j] = inputSudoku[i][j];
}
}

pthread_t threads[NUM_THREADS];
int threadCount = 0;

// Create threads to check each row


for (int i = 0; i < SIZE; i++) {
ThreadArgs *args = (ThreadArgs *)malloc(sizeof(ThreadArgs));
args->row = i;
pthread_create(&threads[threadCount++], NULL, isRowValid, (void *)args);
}

// Create threads to check each column


for (int i = 0; i < SIZE; i++) {
ThreadArgs *args = (ThreadArgs *)malloc(sizeof(ThreadArgs));
args->col = i;
pthread_create(&threads[threadCount++], NULL, isColValid, (void *)args);
}

// Create threads to check each subgrid


for (int i = 0; i < SIZE; i += 3) {
for (int j = 0; j < SIZE; j += 3) {
ThreadArgs *args = (ThreadArgs *)malloc(sizeof(ThreadArgs));
args->row = i;
args->col = j;
pthread_create(&threads[threadCount++], NULL, isSubgridValid, (void
*)args);
}
}

// Join all threads


for (int i = 0; i < NUM_THREADS; i++) {
void *status;
pthread_join(threads[i], &status);
if (status == NULL) {
printf("Sudoku solution is invalid.\n");
return 1;
}
}

printf("Sudoku solution is valid.\n");

return 0;
}

You might also like