0% found this document useful (0 votes)
5 views14 pages

Threads

The document contains two main programs: a Sleeping Teaching Assistant simulation using threads and semaphores, and a Sudoku Application Validator that checks the validity of a Sudoku puzzle using multiple threads. The first program manages student interactions with a teaching assistant, allowing a limited number of students to receive help concurrently. The second program validates a Sudoku grid by checking each row, column, and subgrid for duplicates and proper values.
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)
5 views14 pages

Threads

The document contains two main programs: a Sleeping Teaching Assistant simulation using threads and semaphores, and a Sudoku Application Validator that checks the validity of a Sudoku puzzle using multiple threads. The first program manages student interactions with a teaching assistant, allowing a limited number of students to receive help concurrently. The second program validates a Sudoku grid by checking each row, column, and subgrid for duplicates and proper values.
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/ 14

THREADS AND SYNCHRONIZATION

Name: Subi Pinsha. P


Roll no: 2022115020

1)SLEEPING TEACHING ASSISTANT

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_STUDENTS 5
#define MAX_SEATS 3

sem_t students_waiting;
sem_t ta_sleep;
pthread_mutex_t access_seats;
pthread_mutex_t access_ta;

int waiting_students = 0;
int active_students = 1;

void* student(void* num);


void* ta(void* arg);

int main() {
pthread_t ta_thread;
pthread_t students[NUM_STUDENTS];
int student_ids[NUM_STUDENTS];

sem_init(&students_waiting, 0, 0);
sem_init(&ta_sleep, 0, 0);
pthread_mutex_init(&access_seats, NULL);
pthread_mutex_init(&access_ta, NULL);

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


for (int i = 0; i < NUM_STUDENTS; i++) {
student_ids[i] = i + 1;
pthread_create(&students[i], NULL, student,
(void*)&student_ids[i]);
}

for (int i = 0; i < NUM_STUDENTS; i++) {


pthread_join(students[i], NULL);
}

active_students = 0;
sem_post(&students_waiting);

pthread_join(ta_thread, NULL);

sem_destroy(&students_waiting);
sem_destroy(&ta_sleep);
pthread_mutex_destroy(&access_seats);
pthread_mutex_destroy(&access_ta);

return 0;
}

void* student(void* num) {


int id = *(int*)num;

while (1) {

printf("Student %d is programming.\n", id);


sleep(rand() % 5 + 1);

pthread_mutex_lock(&access_seats);

if (waiting_students < MAX_SEATS) {


waiting_students++;
printf("Student %d is waiting. Waiting students: %d\n", id,
waiting_students);

sem_post(&students_waiting);
pthread_mutex_unlock(&access_seats);

sem_wait(&ta_sleep);
pthread_mutex_lock(&access_ta);
printf("Student %d is getting help from the TA.\n", id);
sleep(rand() % 3 + 1);
printf("Student %d is done getting help.\n", id);
pthread_mutex_unlock(&access_ta);
} else {
printf("Student %d found no available seats and will try
later.\n", id);
pthread_mutex_unlock(&access_seats);
}

if (rand() % 10 < 2) {
printf("Student %d has finished all programming tasks.\n",
id);
break;
}
}

return NULL;
}

void* ta(void* arg) {


while (1) {
sem_wait(&students_waiting);

pthread_mutex_lock(&access_seats);
if (waiting_students > 0) {
waiting_students--;
sem_post(&ta_sleep);
pthread_mutex_unlock(&access_seats);

pthread_mutex_lock(&access_ta);
printf("TA is helping a student.\n");
pthread_mutex_unlock(&access_ta);
sleep(rand() % 3 + 1);
printf("TA is done helping a student.\n");
} else {
pthread_mutex_unlock(&access_seats);

if (!active_students) {
break;
}

printf("TA is taking a nap.\n");


}
}

return NULL;
}
OUTPUT:

root@Subi:~/xv6-public# gcc sleeping.c -o sleeping


root@Subi:~/xv6-public# ./sleeping
Student 1 is programming.
Student 3 is programming.
Student 2 is programming.
Student 4 is programming.
Student 5 is programming.
Student 4 is waiting. Waiting students: 1
TA is helping a student.
Student 4 is getting help from the TA.
Student 3 is waiting. Waiting students: 1
Student 2 is waiting. Waiting students: 2
TA is done helping a student.
Student 4 is done getting help.
Student 4 is programming.
TA is helping a student.
Student 3 is getting help from the TA.
Student 1 is waiting. Waiting students: 2
Student 5 is waiting. Waiting students: 3
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
Student 2 is getting help from the TA.
Student 3 is waiting. Waiting students: 3
Student 4 found no available seats and will try later.
Student 4 is programming.
Student 2 is done getting help.
Student 2 has finished all programming tasks.
TA is helping a student.
TA is done helping a student.
TA is helping a student.
Student 1 is getting help from the TA.
Student 4 is waiting. Waiting students: 3
TA is done helping a student.
Student 1 is done getting help.
Student 1 has finished all programming tasks.
TA is helping a student.
Student 5 is getting help from the TA.
Student 5 is done getting help.
Student 5 is programming.
TA is done helping a student.
TA is helping a student.
Student 3 is getting help from the TA.
Student 5 is waiting. Waiting students: 2
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
TA is helping a student.
Student 4 is getting help from the TA.
TA is done helping a student.
Student 3 is waiting. Waiting students: 1
Student 4 is done getting help.
Student 4 is programming.
TA is helping a student.
Student 5 is getting help from the TA.
TA is done helping a student.
Student 4 is waiting. Waiting students: 1
Student 5 is done getting help.
Student 5 is programming.
TA is helping a student.
Student 3 is getting help from the TA.
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
TA is helping a student.
Student 4 is getting help from the TA.
TA is done helping a student.
Student 4 is done getting help.
Student 4 is programming.
Student 5 is waiting. Waiting students: 1
TA is helping a student.
Student 5 is getting help from the TA.
TA is done helping a student.
Student 5 is done getting help.
Student 5 is programming.
Student 3 is waiting. Waiting students: 1
TA is helping a student.
Student 3 is getting help from the TA.
Student 5 is waiting. Waiting students: 1
Student 4 is waiting. Waiting students: 2
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
TA is helping a student.
Student 5 is getting help from the TA.
Student 3 is waiting. Waiting students: 2
TA is done helping a student.
Student 5 is done getting help.
Student 5 is programming.
TA is helping a student.
Student 4 is getting help from the TA.
Student 4 is done getting help.
Student 4 has finished all programming tasks.
Student 5 is waiting. Waiting students: 2
TA is done helping a student.
TA is helping a student.
Student 3 is getting help from the TA.
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
TA is helping a student.
Student 5 is getting help from the TA.
Student 3 is waiting. Waiting students: 1
TA is done helping a student.
Student 5 is done getting help.
Student 5 is programming.
TA is helping a student.
Student 3 is getting help from the TA.
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
Student 3 is waiting. Waiting students: 1
TA is helping a student.
Student 3 is getting help from the TA.
Student 5 is waiting. Waiting students: 1
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
TA is helping a student.
Student 5 is getting help from the TA.
Student 3 is waiting. Waiting students: 1
TA is done helping a student.
Student 5 is done getting help.
Student 5 is programming.
TA is helping a student.
Student 3 is getting help from the TA.
Student 5 is waiting. Waiting students: 1
Student 3 is done getting help.
Student 3 is programming.
TA is done helping a student.
TA is helping a student.
Student 5 is getting help from the TA.
Student 5 is done getting help.
Student 5 is programming.
TA is done helping a student.
Student 3 is waiting. Waiting students: 1
TA is helping a student.
Student 5 is waiting. Waiting students: 1
Student 3 is getting help from the TA.
Student 3 is done getting help.
Student 3 is programming.
TA is done helping a student.
TA is helping a student.
Student 5 is getting help from the TA.
Student 3 is waiting. Waiting students: 1
Student 5 is done getting help.
Student 5 is programming.
TA is done helping a student.
TA is helping a student.
Student 3 is getting help from the TA.
Student 5 is waiting. Waiting students: 1
Student 3 is done getting help.
Student 3 is programming.
TA is done helping a student.
TA is helping a student.
Student 5 is getting help from the TA.
TA is done helping a student.
Student 5 is done getting help.
Student 5 is programming.
Student 3 is waiting. Waiting students: 1
TA is helping a student.
Student 3 is getting help from the TA.
Student 3 is done getting help.
Student 3 is programming.
Student 5 is waiting. Waiting students: 1
TA is done helping a student.
TA is helping a student.
Student 5 is getting help from the TA.
Student 3 is waiting. Waiting students: 1
Student 5 is done getting help.
Student 5 is programming.
TA is done helping a student.
TA is helping a student.
Student 3 is getting help from the TA.
Student 5 is waiting. Waiting students: 1
TA is done helping a student.
Student 3 is done getting help.
Student 3 is programming.
TA is helping a student.
Student 5 is getting help from the TA.
TA is done helping a student.
Student 3 is waiting. Waiting students: 1
Student 5 is done getting help.
Student 5 is programming.
TA is helping a student.
Student 3 is getting help from the TA.
Student 3 is done getting help.
Student 3 has finished all programming tasks.
TA is done helping a student.
Student 5 is waiting. Waiting students: 1
TA is helping a student.
Student 5 is getting help from the TA.
Student 5 is done getting help.
Student 5 has finished all programming tasks.
TA is done helping a student.

2)Sudoku Application Validator:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS 11
#define GRID_SIZE 9
#define SUBGRID_SIZE 3

typedef struct {
int row;
int column;
} parameters;

int sudoku[GRID_SIZE][GRID_SIZE];
int results[NUM_THREADS] = {0};
parameters error_positions[NUM_THREADS];
void *check_row(void *param) {
parameters *p = (parameters *) param;
int row = p->row;
int digits[GRID_SIZE] = {0};

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


int num = sudoku[row][col];
if (num < 1 || num > 9 || digits[num - 1] == 1) {
results[row] = 0;
error_positions[row].row = row;
error_positions[row].column = col;
pthread_exit(NULL);
}
digits[num - 1] = 1;
}
results[row] = 1;
pthread_exit(NULL);
}

void *check_column(void *param) {


parameters *p = (parameters *) param;
int column = p->column;
int digits[GRID_SIZE] = {0};

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


int num = sudoku[row][column];
if (num < 1 || num > 9 || digits[num - 1] == 1) {
results[GRID_SIZE + column] = 0;
error_positions[GRID_SIZE + column].row = row;
error_positions[GRID_SIZE + column].column = column;
pthread_exit(NULL);
}
digits[num - 1] = 1;
}
results[GRID_SIZE + column] = 1;
pthread_exit(NULL);
}

void *check_subgrid(void *param) {


parameters *p = (parameters *) param;
int row_start = p->row;
int col_start = p->column;
int digits[GRID_SIZE] = {0};

for (int i = 0; i < SUBGRID_SIZE; i++) {


for (int j = 0; j < SUBGRID_SIZE; j++) {
int num = sudoku[row_start + i][col_start + j];
if (num < 1 || num > 9 || digits[num - 1] == 1) {
int index = GRID_SIZE * 2 + (row_start / 3) * 3 +
(col_start / 3);
results[index] = 0;
error_positions[index].row = row_start + i;
error_positions[index].column = col_start + j;
pthread_exit(NULL);
}
digits[num - 1] = 1;
}
}
int index = GRID_SIZE * 2 + (row_start / 3) * 3 + (col_start / 3);
results[index] = 1;
pthread_exit(NULL);
}

int main() {
pthread_t threads[NUM_THREADS];
parameters *data[NUM_THREADS];
printf("Enter the Sudoku puzzle (9x9 grid), each number separated
by spaces and rows by new lines:\n");
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
scanf("%d", &sudoku[i][j]);
}
}

for (int i = 0; i < GRID_SIZE; i++) {


data[i] = (parameters *) malloc(sizeof(parameters));
data[i]->row = i;
data[i]->column = 0;
pthread_create(&threads[i], NULL, check_row, (void *) data[i]);
}

for (int i = 0; i < GRID_SIZE; i++) {


data[GRID_SIZE + i] = (parameters *)
malloc(sizeof(parameters));
data[GRID_SIZE + i]->row = 0;
data[GRID_SIZE + i]->column = i;
pthread_create(&threads[GRID_SIZE + i], NULL, check_column,
(void *) data[GRID_SIZE + i]);
}

for (int i = 0; i < SUBGRID_SIZE; i++) {


for (int j = 0; j < SUBGRID_SIZE; j++) {
data[GRID_SIZE * 2 + i * 3 + j] = (parameters *)
malloc(sizeof(parameters));
data[GRID_SIZE * 2 + i * 3 + j]->row = i * 3;
data[GRID_SIZE * 2 + i * 3 + j]->column = j * 3;
pthread_create(&threads[GRID_SIZE * 2 + i * 3 + j], NULL,
check_subgrid, (void *) data[GRID_SIZE * 2 + i * 3 + j]);
}
}

for (int i = 0; i < NUM_THREADS; i++) {


pthread_join(threads[i], NULL);
}

int valid = 1;
for (int i = 0; i < NUM_THREADS; i++) {
if (results[i] == 0) {
valid = 0;
printf("Error found at row %d, column %d\n",
error_positions[i].row + 1, error_positions[i].column + 1);
}
}

if (valid) {
printf("The Sudoku puzzle is valid.\n");
} else {
printf("The Sudoku puzzle is not valid.\n");
}

for (int i = 0; i < NUM_THREADS; i++) {


free(data[i]);
}

return 0;
}

OUTPUT:

root@Subi:~/xv6-public# gcc sudoku.c -o sudoku


root@Subi:~/xv6-public# ./sudoku
Enter the Sudoku puzzle (9x9 grid), each number separated by spaces and
rows by new lines:
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
The Sudoku puzzle is valid.

3) Producer Consumer Problem

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#define BUFFER_SIZE 3
#define NUM_PRODUCERS 2
#define NUM_CONSUMERS 2
int in = 0, out = 0;
sem_t mutex, empty, full;
void *producer(void *arg) {
int id = *(int*)arg;
for (int i = 0; i < 3; i++) {
int item = rand() % 100;
sem_wait(&empty);
sem_wait(&mutex);
buffer[in] = item;
printf("Producer %d produced item %d\n", id, item);
in = (in + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&full);
}
return NULL;
}
void *consumer(void *arg) {
int id = *(int*)arg;
for (int i = 0; i < 3; i++) {
sem_wait(&full);
sem_wait(&mutex);
int item = buffer[out];
printf("Consumer %d consumed item %d\n", id, item);
out = (out + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&empty);
}
return NULL;
}
int main() {
pthread_t producer_threads[NUM_PRODUCERS],
consumer_threads[NUM_CONSUMERS];
int producer_ids[NUM_PRODUCERS], consumer_ids[NUM_CONSUMERS];
sem_init(&mutex, 0, 1);
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
for (int i = 0; i < NUM_PRODUCERS; ++i) {
producer_ids[i] = i + 1;
pthread_create(&producer_threads[i], NULL, producer,
&producer_ids[i]);
}
for (int i = 0; i < NUM_CONSUMERS; ++i) {
consumer_ids[i] = i + 1;
pthread_create(&consumer_threads[i], NULL, consumer,
&consumer_ids[i]);
}
for (int i = 0; i < NUM_PRODUCERS; ++i) {
pthread_join(producer_threads[i], NULL);
}
for (int i = 0; i < NUM_CONSUMERS; ++i) {
pthread_join(consumer_threads[i], NULL);
}
sem_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}

OUTPUT:

root@Subi:~/xv6-public# gcc producer.c -o producer


root@Subi:~/xv6-public# ./producer
Producer 1 produced item 83
Producer 1 produced item 77
Consumer 1 consumed item 83
Consumer 2 consumed item 77
Producer 1 produced item 15
Producer 2 produced item 86
Producer 2 produced item 93
Consumer 1 consumed item 15
Consumer 2 consumed item 86
Consumer 1 consumed item 93
Producer 2 produced item 35
Consumer 2 consumed item 35

You might also like