Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <unistd.h>
- #define BUFFER_SIZE 5
- int buffer[BUFFER_SIZE];
- int in = 0, out = 0;
- sem_t mutex, full, empty;
- void *producer(void *arg) {
- while (1) {
- int item = rand() % 100;
- sem_wait(&empty);
- sem_wait(&mutex);
- buffer[in] = item;
- printf("Produced item: %d\n", item);
- in = (in + 1) % BUFFER_SIZE;
- sem_post(&mutex);
- sem_post(&full);
- sleep(1);
- }
- }
- void *consumer(void *arg) {
- while (1) {
- int item;
- sem_wait(&full);
- sem_wait(&mutex);
- item = buffer[out];
- printf("Consumed item: %d\n", item);
- out = (out + 1) % BUFFER_SIZE;
- sem_post(&mutex);
- sem_post(&empty);
- sleep(2);
- }
- }
- int main() {
- pthread_t prod_thread, cons_thread;
- sem_init(&mutex, 0, 1);
- sem_init(&full, 0, 0);
- sem_init(&empty, 0, BUFFER_SIZE);
- pthread_create(&prod_thread, NULL, producer, NULL);
- pthread_create(&cons_thread, NULL, consumer, NULL);
- pthread_join(prod_thread, NULL);
- pthread_join(cons_thread, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement