|
-
May 4th, 2010, 10:12 AM
#1
Producer/Consumer using mutexs C
the following I am not confused over the "idea" of this producer/consumer problem, but im still confused about threads and semaphores and stuff. this particular program has 4 threads that read or consume from a shared variable. It has 1 thread that writes or produces to that shared variable. All the thread loops need to loop indefinitly which im sure i havent done. and on each loop iteration it requirs 1 second on computation without access to the shared variable.
And the data output i have in main needs to print report every second. My code has no errors, but it is exiting a 1. i definitely need some help! i tried my best to comment the code as to what is going on.
The code needs to be in C!
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#define BUFF_SIZE 100 /* total number of slots */
#define NP 1 /* total number of producers */
#define NC 4 /* total number of consumers */
#define NITERS 4 /* number of items produced/consumed */
typedef struct {
int buf[BUFF_SIZE]; /* shared var */
int in; /* buf[in%BUFF_SIZE] is the first empty slot */
int out; /* buf[out%BUFF_SIZE] is the first full slot */
sem_t full; /* keep track of the number of full spots */
sem_t empty; /* keep track of the number of empty spots */
sem_t mutex; /* enforce mutual exclusion to shared data */
} sbuf_t;
sbuf_t shared;
/* function prototypes */
void *Producer(void *arg);
void *Consumer(void *arg);
/* increments and decrements */
int produced, consumed;
int main()
{
/* producer/consumer threads */
pthread_t idP, idC;
/* loop control */
int index;
/* temp variables to comp the consumes and produces per hour */
int temp1, temp2;
/* keeps track of seconds */
int j = 1;
/* creates semaphores */
sem_init(&shared.full, 0, 0);
sem_init(&shared.empty, 0, BUFF_SIZE);
sem_inti(&shared.mutex, 0, 1);
/* creates producer thread */
for (index = 0; index < NP; index++)
{
/* Create 1 producer */
pthread_create(&idP, NULL, Producer, (void*)index);
}
/* creates 4 consumer threads */
for (index = 0; index < NC; index++)
{
pthread_create(&idC, NULL, Consumer, (void*)index);
}
while(1)
{
sleep(1);
sem_wait(&shared.mutex);
temp1 = (consumed * 3600) / j;
temp2 = (produced * 3600) / j;
printf("Statistics after %d seconds: ", j);
sem_post(&shared.mutex);
printf("So many consumed per hour: %d\n", temp1);
printf("So many produced per hour: %d\n", temp2);
printf("Ratio of consumed/produced per hour: %d", temp1 / temp2);
j++;
}
sem_destroy(&shared.mutex); // destroy semaphore
pthread_exit(NULL);
}
void *Producer(void *arg)
{
int data, index;
index = (int)arg;
printf("Thread %d in line to produce\n", index);
for (produced=0; produced < NITERS; produced++) {
/* Produce item */
data = produced;
printf("[P%d] Producing %d ...\n", index, data); fflush(stdout);
data=produce_item(shared.buf);
/* If there are no empty slots, wait */
sem_wait(&shared.empty);
/* If another thread uses the buffer, wait */
sem_wait(&shared.mutex);
/* put new item in buffer */
shared.buf[shared.in] = data;
shared.in = (shared.in+1)%BUFF_SIZE;
/* leave the critical region */
sem_post(&shared.mutex);
/* Increment the number of full slots */
sem_post(&shared.full);
/* Interleave producer and consumer execution */
if (produced % 2 == 1) sleep(1);
}
return NULL;
}
void *Consumer(void *arg)
{
int data, index;
index = (int)arg;
int consumed;
printf("[C%d] Consuming %d ...\n", index, data); fflush(stdout);
for (consumed=0; consumed < NITERS; consumed++) {
data = consumed;
/* If there are no full slots, wait */
sem_wait(&shared.full);
/* If another thread uses the buffer, wait */
sem_wait(&shared.mutex);
shared.buf[shared.out] = data;
shared.out = (shared.out+1)%BUFF_SIZE;
/* Release the buffer */
sem_post(&shared.mutex);
/* Increment the number of empty slots */
sem_post(&shared.empty);
/* Interleave producer and consumer execution */
if (consumed % 2 == 1) sleep(1);
}
return NULL;
}
-
May 5th, 2010, 12:48 PM
#2
Re: Producer/Consumer using mutexs C
 Originally Posted by pantherman34
the following I am not confused over the "idea" of this producer/consumer problem, but im still confused about threads and semaphores and stuff.
The basic idea is really simple: you have to synchronize access to any shared resource. That is any variable that can be accessed by more than one thread, where one of the threads could write to the variable.
Code:
void *Producer(void *arg)
{
int data, index;
index = (int)arg;
printf("Thread %d in line to produce\n", index);
for (produced=0; produced < NITERS; produced++) {
/* Produce item */
data = produced;
printf("[P%d] Producing %d ...\n", index, data); fflush(stdout);
data=produce_item(shared.buf);
/* ... */
}
return NULL;
}
You are accessing both produced and shared without synchronization. Same in your Consumer function.
FYI: your post would have fitted better in the Multithreading forum
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|