0% found this document useful (0 votes)
36 views4 pages

Untitled Document

This C code implements a producer-consumer problem using semaphores to synchronize threads that produce and consume letters of the alphabet. There is one producer thread that generates letters and one consumer thread that processes them using a shared buffer of size one. The semaphores lock access to the buffer and ensure only one thread can access it at a time to prevent race conditions. The code runs until it processes the entire alphabet from a to z.

Uploaded by

api-694008769
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views4 pages

Untitled Document

This C code implements a producer-consumer problem using semaphores to synchronize threads that produce and consume letters of the alphabet. There is one producer thread that generates letters and one consumer thread that processes them using a shared buffer of size one. The semaphores lock access to the buffer and ensure only one thread can access it at a time to prevent race conditions. The code runs until it processes the entire alphabet from a to z.

Uploaded by

api-694008769
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

/*

Name: Kevin Wical


Date: 8/2/2022
Title: Semaphore lab6 - sychonization
Description: This code demonstrates the Producer-Consumer problem using semaphores.
The semaphores act as locks to ensure mutual exclusion between the threads as they produce
and consume the alphabet.
The code uses 1 producer and 1 consumer with a buffer size of 1 as well. .
His GitHub is https://gist.gethub.com/navin-mohan and I have repurposed his working example
of the producer-consumer with
semaphores to use the alphabet instead of randomly generated numbers.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

/*
use the pthread flag with gcc to compile this code
~$ gcc -pthread lab6.o -o lab6
alread included in makefile
*/

pthread_t *producers;
pthread_t *consumers;

sem_t buf_mutex,empty_count,fill_count;

int buf_pos=-1,prod_count,con_count,buf_len;
char *buf;
int j = 0; // global counter exit condition when 'z' is consumed

char alphabet[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',


'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z' };

char produce(pthread_t self){


int i = 0;
//char letter = alphabet[rand() % 26]; // unused, was selecting a character at random
char letter = alphabet[j];
while(!pthread_equal(*(producers+i),self) && i < prod_count){
i++;
}

printf("Producer %d produced %c \n",i+1,letter);

return letter;
}

void consume(char p,pthread_t self){


int i = 0;
while(!pthread_equal(*(consumers+i),self) && i < con_count){
i++;
}

printf("Buffer:");
for(i=0;i<=buf_pos;++i)
printf("%c ",*(buf+i));
printf("\nConsumer %d consumed %c \n",i,p);

j++;
if(p == 'z') // exit condition
{
exit(0);
}

void* producer(void *args){

while(1){
int p = produce(pthread_self());
sem_wait(&empty_count);
sem_wait(&buf_mutex);
++buf_pos; // critical section
*(buf + buf_pos) = p;
sem_post(&buf_mutex);
sem_post(&fill_count);
sleep(1); // shows the threads in action
}

return NULL;
}
void* consumer(void *args){
int c;
while(1){
sem_wait(&fill_count);
sem_wait(&buf_mutex);
c = *(buf+buf_pos);
consume(c,pthread_self());
--buf_pos;
sem_post(&buf_mutex);
sem_post(&empty_count);
}
return NULL;
}

int main(void){

int i,err;

sem_init(&buf_mutex,0,1);
sem_init(&fill_count,0,0);

///////////// hard coded 1 for # of producers, consumers, and buffer length


prod_count = 1;
producers = (pthread_t*) malloc(prod_count*sizeof(pthread_t));

con_count = 1;
consumers = (pthread_t*) malloc(con_count*sizeof(pthread_t));

buf_len = 1;
buf = (char*) malloc(buf_len*sizeof(int));

sem_init(&empty_count,0,buf_len);

for(i=0;i<prod_count;i++){
err = pthread_create(producers+i,NULL,&producer,NULL);
if(err != 0){
printf("Error creating producer %d: %s\n",i+1,strerror(err));
}else{
printf("Successfully created producer %d\n",i+1);
}
}

for(i=0;i<con_count;i++){
err = pthread_create(consumers+i,NULL,&consumer,NULL);
if(err != 0){
printf("Error creating consumer %d: %s\n",i+1,strerror(err));
}else{
printf("Successfully created consumer %d\n",i+1);
}
}

for(i=0;i<prod_count;i++){
pthread_join(*(producers+i),NULL);
}
for(i=0;i<con_count;i++){
pthread_join(*(consumers+i),NULL);
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy