Operating System Project
Operating System Project
A Sudoku puzzle uses a 9 × 9 grid in which each column and row, as well as
each of the nine 3 × 3 sub - grids, must contain all of the digits 1 · · · 9. Figure
4.19 presents an example of a valid Sudoku puzzle. This project consists of
designing a multithreaded application that determines whether the solution to
a Sudoku puzzle is valid.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#define SIZE 9
#define SUBGRID_SIZE 3
int sudoku[SIZE][SIZE];
typedef struct {
int row;
int column;
} parameters;
pthread_exit((void *)1);
}
pthread_exit((void *)1); }
pthread_exit((void *)1);
}
int is_valid_solution() {
pthread_t threads[SIZE + SIZE + SIZE];
int row, col, index = 0;
int valid = 1;
intptr_t status;
for (int i = 0; i < SIZE + SIZE + SIZE; i++) {
pthread_join(threads[i], (void **)&status);
if (status == 0) {
valid = 0;
break;
}
}
return valid;
}
void print_invalid_indices() {
pthread_t threads[SIZE + SIZE + SIZE];
int row, col, index = 0;
int invalid_rows[SIZE] = {0};
int invalid_cols[SIZE] = {0};
int invalid_subgrids[SIZE] = {0};
intptr_t status;
for (int i = 0; i < SIZE + SIZE + SIZE; i++) {
pthread_join(threads[i], (void **)&status);
if (status == 0) {
if (i < SIZE) {
invalid_rows[i] = 1;
} else if (i < 2 * SIZE) {
invalid_cols[i - SIZE] = 1;
} else {
invalid_subgrids[i - 2 * SIZE] = 1;
}
}
}
void input_sudoku() {
printf("Enter the Sudoku puzzle (9x9 grid):\n");
for (int i = 0; i < SIZE; i++) {
printf("Row %d: ", i + 1);
for (int j = 0; j < SIZE; j++) {
scanf("%d", &sudoku[i][j]);
}
}
}
int main() {
input_sudoku();
if (is_valid_solution()) {
printf("Sudoku solution is valid.\n");
} else {
printf("Sudoku solution is invalid.\n");
printf("Invalid indices:\n");
print_invalid_indices();
}
return 0;
}
The program is tested with two different input values for the sudoku entry.
For the first input, the input value is not valid, since 10 is entered in 3 rd row 5th
column, which will return invalid result since 10 is out of bound from the fixed range.
And hence, it will return the row number and column number.
For the second input set, the entered input values are correct and hence, it returned
the output as valid one.
OUTPUT:
SLEEPING TEACHING ASSISTANT:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <time.h>
#define NUM_STUDENTS 5
#define CHAIRS 3
int waiting_students = 0;
int office_hours = 30;
int help_limit = 2;
int ta_break_time = 3; int student_patience[NUM_STUDENTS];
int office_hours_over = 0;
void *student_function(void *id) {
int student_id = *((int *)id);
srand(time(NULL) + student_id);
while (1) {
printf("Student %d is programming.\n", student_id);
sleep(rand() % 5 + 1);
pthread_mutex_lock(&mutex);
if (waiting_students < CHAIRS) {
waiting_students++;
printf("Student %d is waiting in the hallway.\n", student_id);
pthread_mutex_unlock(&mutex);
sem_post(&sem_students);
pthread_mutex_lock(&mutex);
waiting_students--;
} else {
printf("Student %d will come back later.\n", student_id);
pthread_mutex_unlock(&mutex);
}
pthread_mutex_unlock(&mutex);
}
}
void *ta_function() {
int remaining_office_hours = office_hours;
while (remaining_office_hours > 0) {
sem_wait(&sem_ta);
pthread_mutex_lock(&mutex);
printf("TA is helping a student.\n");
pthread_mutex_unlock(&mutex);
sleep(rand() % 3 + 1);
int students_helped = 0;
while (waiting_students > 0 && students_helped < help_limit) {
sem_post(&sem_students);
pthread_mutex_lock(&mutex);
printf("TA is helping another student.\n");
waiting_students--;
printf("TA: Excellent progress! Keep up the good work!\n
students_helped++;
pthread_mutex_unlock(&mutex);
sleep(rand() % 3 + 1);
}
remaining_office_hours--;
}
pthread_mutex_lock(&mutex);
printf("Office hours are over. TA leaves.\n");
pthread_mutex_unlock(&mutex);
office_hours_over = 1;
sem_post(&sem_students);
sem_post(&sem_students);
exit(0);
}
void input_parameters() {
printf("Enter the duration of office hours in seconds: ");
scanf("%d", &office_hours);
printf("Enter the maximum number of students the TA can help
simultaneously: ");
scanf("%d", &help_limit);
printf("Enter the duration of TA's break time in seconds: ");
scanf("%d", &ta_break_time);
}
int main() {
pthread_t students[NUM_STUDENTS];
pthread_t ta;
int student_ids[NUM_STUDENTS];
sem_init(&sem_students, 0, 0);
sem_init(&sem_ta, 0, 0);
input_parameters();
srand(time(NULL));
for (int i = 0; i < NUM_STUDENTS; i++) {
student_ids[i] = i + 1;
student_patience[i] = rand() % 10 + 1;
pthread_create(&students[i], NULL, student_function, &student_ids[i]);
}
sem_post(&sem_ta);
pthread_join(ta, NULL);
for (int i = 0; i < NUM_STUDENTS; i++) {
pthread_join(students[i], NULL);
}
sem_destroy(&sem_students);
sem_destroy(&sem_ta);
return 0;
}
OUTPUT:
BANKERS PROBLEM:
CODE:
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k;
int n = 4;
int m = 3;
int allocation[4][3] = {{2, 1, 0},
{1, 2, 2},
{0, 2, 0},
{3, 0, 1}};
int max[4][3] = {{8, 6, 3},
{9, 4, 3},
{5, 3, 3},
{4, 2, 3}};
int need[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m; j++)
{
need[i][j] = max[i][j] - allocation[i][j];
}
}
int y = 0;
for (int k=0;k<4;k++)
{
for(int i=0;i<n;i++)
{
if(finish[i]==0)
{
int flag = 0;
for(int j=0;j<m;j++)
{
if(need[i][j]>available[j])
{
flag = 1; break;
}
}
if(flag==0)
{
ans[idx++] = i;
for(int y=0;y<m;y++)
{
available[y] += allocation[i][y];
}
finish[i] = 1;
}
}
}
}
if(flag==true)
{
cout<<"System is in safe state and following is the safe sequence: ";
for(int i=0;i<n-1;i++)
{
cout<<"P"<<ans[i]<<" -> ";
}
cout<<"P"<<ans[n-1]<<endl;
}
return 0;
}
OUTPUT: