Assignment Task
Assignment Task
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;
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
// 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];
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}
};
pthread_t threads[NUM_THREADS];
int threadCount = 0;
return 0;
}