0% found this document useful (0 votes)
15 views18 pages

Operating System Project

The program implements a solution to coordinate the activities of a teaching assistant and students using threads, mutex locks and semaphores. The teaching assistant helps students with their programming assignments during office hours in their office which can only fit one person at a time. Students wait in the hallway if the office is occupied. The program spawns threads for the teaching assistant and each student to simulate their activities.

Uploaded by

rasigapriyas32
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)
15 views18 pages

Operating System Project

The program implements a solution to coordinate the activities of a teaching assistant and students using threads, mutex locks and semaphores. The teaching assistant helps students with their programming assignments during office hours in their office which can only fit one person at a time. Students wait in the hallway if the office is occupied. The program spawns threads for the teaching assistant and each student to simulate their activities.

Uploaded by

rasigapriyas32
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/ 18

OPERATING SYSTEM PROJECT

--THREADS AND SYNCHRONIZATION—

SUDOKO SOLUTION VALIDATOR:

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;

void *validate_row(void *param) {


parameters *data = (parameters *)param;
int row = data->row;
int col;
int valid[SIZE] = {0};

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


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

pthread_exit((void *)1);
}

void *validate_column(void *param) {


parameters *data = (parameters *)param;
int row;
int col = data->column;
int valid[SIZE] = {0};

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


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

pthread_exit((void *)1); }

void *validate_subgrid(void *param) {


parameters *data = (parameters *)param;
int start_row = data->row;
int start_col = data->column;
int valid[SIZE] = {0};
int row, col;

for (row = start_row; row < start_row + SUBGRID_SIZE; row++) {


for (col = start_col; col < start_col + SUBGRID_SIZE; col++) {
int num = sudoku[row][col];
if (num < 1 || num > 9 || valid[num - 1] == 1) {
pthread_exit((void *)0);
}
valid[num - 1] = 1;
}
}

pthread_exit((void *)1);
}

int is_valid_solution() {
pthread_t threads[SIZE + SIZE + SIZE];
int row, col, index = 0;

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


parameters *data = (parameters *)malloc(sizeof(parameters));
data->row = row;
data->column = -1;
pthread_create(&threads[index++], NULL, validate_row, data);
}
for (col = 0; col < SIZE; col++) {
parameters *data = (parameters *)malloc(sizeof(parameters));
data->row = -1;
data->column = col;
pthread_create(&threads[index++], NULL, validate_column, data);
}

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


for (col = 0; col < SIZE; col += SUBGRID_SIZE) {
parameters *data = (parameters *)malloc(sizeof(parameters));
data->row = row;
data->column = col;
pthread_create(&threads[index++], NULL, validate_subgrid, data);
}
}

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};

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


parameters *data = (parameters *)malloc(sizeof(parameters));
data->row = row;
data->column = -1;
pthread_create(&threads[index++], NULL, validate_row, data);
}

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


parameters *data = (parameters *)malloc(sizeof(parameters));
data->row = -1;
data->column = col;
pthread_create(&threads[index++], NULL, validate_column, data);
}

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


for (col = 0; col < SIZE; col += SUBGRID_SIZE) {
parameters *data = (parameters *)malloc(sizeof(parameters));
data->row = row;
data->column = col;
pthread_create(&threads[index++], NULL, validate_subgrid, data);
}
}

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;
}
}
}

printf("Invalid rows: ");


for (row = 0; row < SIZE; row++) {
if (invalid_rows[row] == 1) {
printf("%d ", row);
}
}
printf("\n");

printf("Invalid columns: ");


for (col = 0; col < SIZE; col++) {
if (invalid_cols[col] == 1) {
printf("%d ", col);
}
}
printf("\n");

printf("Invalid subgrids: ");


for (row = 0; row < SIZE / SUBGRID_SIZE; row++) {
for (col = 0; col < SIZE / SUBGRID_SIZE; col++) {
if (invalid_subgrids[row * (SIZE / SUBGRID_SIZE) + col] == 1) {
printf("(%d, %d) ", row, col);
}
}
}
printf("\n");
}

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:

A university computer science department has a teaching assistant (TA) who


helps undergraduate students with their programming assignments during
regular office hours. The TA’s office is rather small and has room for only one
desk with a chair and computer. There are three chairs in the hallway outside
the office where students can sit and wait if the TA is currently helping another
student. When there are no students who need help during office hours, the
TA sits at the desk and takes a nap. If a student arrives during office hours
and finds the TA sleeping, the student must awaken the TA to ask for help. If a
student arrives and finds the TA currently helping another student, the student
sits on one of the chairs in the hallway and waits. If no chairs are available, the
student will come back at a later time.
Using POSIX threads, mutex locks, and semaphores, implement a solution
that coordinates the activities of the TA and the students.

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


sem_t sem_students;
sem_t sem_ta;

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);
}

if (students_helped < help_limit) {


pthread_mutex_lock(&mutex);
printf("TA takes a short break.\n");
pthread_mutex_unlock(&mutex);
sem_post(&sem_ta);
sleep(ta_break_time);
}

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]);
}

pthread_create(&ta, NULL, ta_function, NULL);

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 available[3] = {4, 3, 2};

int finish[n] = {0};


int ans[n] = {0};
int idx = 0;

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;
}
}
}
}

bool flag = true;


for(int i=0; i<n; i++)
{
if(finish[i] == 0)
{
flag = false;
cout<<"System is in deadlock !!"; break;
}
}

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:

You might also like