Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views3 pages

PCcode

The document contains a C program that implements a producer-consumer problem using threads, semaphores, and mutexes. It allows a user to specify the number of producer and consumer threads, which operate on a fixed-size circular buffer. The program initializes synchronization primitives, creates threads for production and consumption, and cleans up resources before exiting.

Uploaded by

shraddha.clg0905
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)
6 views3 pages

PCcode

The document contains a C program that implements a producer-consumer problem using threads, semaphores, and mutexes. It allows a user to specify the number of producer and consumer threads, which operate on a fixed-size circular buffer. The program initializes synchronization primitives, creates threads for production and consumption, and cleans up resources before exiting.

Uploaded by

shraddha.clg0905
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/ 3

CODE-

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

#include <semaphore.h>

#define BUFFER_SIZE 5 // fixed buffer size

sem_t full;

sem_t empty;

pthread_mutex_t mutex;

int in = 0;

int out = 0;

int item = 0;

int buffer[BUFFER_SIZE];

void *producer(void *arg) {

while (1) {

int produced_item = ++item; // produce new item

sem_wait(&empty); // wait for empty slot

pthread_mutex_lock(&mutex); // enter critical section

buffer[in] = produced_item;

printf("Producer %lu produced: %d at buffer[%d]\n",

pthread_self(), produced_item, in);

in = (in + 1) % BUFFER_SIZE; // circular increment

pthread_mutex_unlock(&mutex); // exit critical section

sem_post(&full); // signal that buffer has item

sleep(1); // slow down output

return NULL;
}

void *consumer(void *arg) {

while (1) {

sem_wait(&full); // wait for full slot

pthread_mutex_lock(&mutex); // enter critical section

int consumed_item = buffer[out];

printf("Consumer %lu consumed: %d from buffer[%d]\n",

pthread_self(), consumed_item, out);

out = (out + 1) % BUFFER_SIZE; // circular increment

pthread_mutex_unlock(&mutex); // exit critical section

sem_post(&empty); // signal empty slot available

sleep(1); // slow down output

return NULL;

int main() {

int n;

printf("Enter number of producers/consumers: ");

scanf("%d", &n);

sem_init(&empty, 0, BUFFER_SIZE);

sem_init(&full, 0, 0);

pthread_mutex_init(&mutex, NULL);

pthread_t *produce = malloc(n * sizeof(pthread_t));

pthread_t *consum = malloc(n * sizeof(pthread_t));

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

pthread_create(&produce[i], NULL, producer, NULL);


pthread_create(&consum[i], NULL, consumer, NULL);

// Let them run for some time (infinite otherwise)

sleep(8);

printf("Main thread exiting...\n");

free(produce);

free(consum);

sem_destroy(&empty);

sem_destroy(&full);

pthread_mutex_destroy(&mutex);

return 0;

You might also like