Threads
Threads
#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;
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);
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;
}
while (1) {
pthread_mutex_lock(&access_seats);
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;
}
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;
}
return NULL;
}
OUTPUT:
#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};
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]);
}
}
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");
}
return 0;
}
OUTPUT:
#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: