Lab7
Lab7
Problem Statement:
Problem A:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
sem_post(&mutex);
sem_post(&full);
sleep(1);
}
pthread_exit(NULL);
}
int main() {
pthread_t producers[1], consumers[1];
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
pthread_join(producers[0], NULL);
pthread_join(consumers[0], NULL);
sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);
return 0;
}
Output:
Problem B:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define BUFFER_SIZE 5
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
int main() {
pthread_t producers[1], consumers[1];
pthread_join(producers[0], NULL);
pthread_join(consumers[0], NULL);
return 0;
}
Output: