Lab4_OS
Lab4_OS
#define BUFFER_SIZE 5
#define PRODUCE_COUNT 20
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
buffer[in] = item;
printf("Producer produced: %d at index %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full); // increase count of full slots
pthread_mutex_unlock(&mutex);
sem_post(&empty); // increase count of empty slots
int main() {
pthread_t prod, cons1, cons2;
int id1 = 1, id2 = 2;
// Create threads
if (pthread_create(&prod, NULL, producer, NULL) != 0) {
perror("Failed to create producer thread");
exit(EXIT_FAILURE);
}
if (pthread_create(&cons1, NULL, consumer, &id1) != 0) {
perror("Failed to create consumer 1 thread");
exit(EXIT_FAILURE);
}
if (pthread_create(&cons2, NULL, consumer, &id2) != 0) {
perror("Failed to create consumer 2 thread");
exit(EXIT_FAILURE);
}
return 0;
}
I52:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
#define PRODUCE_COUNT 10
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
if (sem_wait(&empty) != 0) {
perror("sem_wait failed in producer");
pthread_exit(NULL);
}
if (pthread_mutex_lock(&mutex) != 0) {
perror("mutex lock failed in producer");
pthread_exit(NULL);
}
buffer[in] = item;
printf("Producer %d produced: %d at index %d\n", id, item, in);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
pthread_exit(NULL);
}
if (pthread_mutex_lock(&mutex) != 0) {
perror("mutex lock failed in consumer");
pthread_exit(NULL);
}
pthread_mutex_unlock(&mutex);
sem_post(&empty);
pthread_exit(NULL);
}
int main() {
pthread_t prod1, prod2, cons;
int id1 = 1, id2 = 2;
// Create threads
if (pthread_create(&prod1, NULL, producer, &id1) != 0 ||
pthread_create(&prod2, NULL, producer, &id2) != 0 ||
pthread_create(&cons, NULL, consumer, NULL) != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
// Cleanup
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
I53:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
#define PRODUCE_COUNT 10
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
if (sem_wait(&empty) != 0) {
perror("sem_wait failed in producer");
pthread_exit(NULL);
}
if (pthread_mutex_lock(&mutex) != 0) {
perror("mutex lock failed in producer");
pthread_exit(NULL);
}
buffer[in] = item;
printf("Producer %d produced: %d at index %d\n", id, item, in);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
usleep(100000);
}
pthread_exit(NULL);
}
if (pthread_mutex_lock(&mutex) != 0) {
perror("mutex lock failed in consumer");
pthread_exit(NULL);
}
pthread_mutex_unlock(&mutex);
sem_post(&empty);
usleep(150000);
}
pthread_exit(NULL);
}
int main() {
pthread_t prod1, prod2, cons1, cons2;
int id1 = 1, id2 = 2;
if (sem_init(&empty, 0, BUFFER_SIZE) != 0 ||
sem_init(&full, 0, 0) != 0 ||
pthread_mutex_init(&mutex, NULL) != 0) {
perror("Initialization failed");
exit(EXIT_FAILURE);
}
// Create threads
if (pthread_create(&prod1, NULL, producer, &id1) != 0 ||
pthread_create(&prod2, NULL, producer, &id2) != 0 ||
pthread_create(&cons1, NULL, consumer, &id1) != 0 ||
pthread_create(&cons2, NULL, consumer, &id2) != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
// Join threads
pthread_join(prod1, NULL);
pthread_join(prod2, NULL);
pthread_join(cons1, NULL);
pthread_join(cons2, NULL);
// Cleanup
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}