Results 1 to 5 of 5
Hi everybody! I have written this producer-consumer code. The producer creates random integers, and the consumer prints them. And it works. Only, not in the way I expected!!! If you ...
- 06-23-2010 #1Just Joined!
- Join Date
- Jun 2010
- Posts
- 11
Producer-Consumer problem
Hi everybody! I have written this producer-consumer code. The producer creates random integers, and the consumer prints them. And it works. Only, not in the way I expected!!! If you compile (don't forget gcc -lpthread option!) and run it, you see that generally (not always) the producter produces everything, and then the consumer prints everything. But I did put the pthread_cond_signal in the producer's code!!! But at that point, the consumers continues to sleep

Any ideas??
PHP Code:#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include <time.h>
#define true 1
#define N 5
struct node {
int info;
struct node * next;
};
static struct node * head=NULL;
static int i=0;
void inserisci (struct node * p)
{
struct node * aux=head;
head=p;
p->next=aux;
/*
if (head==NULL) head=p;
else{
while (aux->next!=NULL)
aux=aux->next;
aux->next=p;
p->next=NULL;
}*/
}
struct node * produci(void)
{
struct node *point=malloc(sizeof(struct node));
point->info=rand() % 100;
point->next=NULL;
return(point);
}
struct node * estrai(void)
{
struct node * aux,*aux2;
aux=head;
if (head==NULL)
{printf("Lista vuota (non dovrebbe succedere)\n"); return(NULL);}
else{
if (aux->next==NULL)
{
head=NULL;
return(aux);
}
else{
while(aux->next!=NULL)
{aux2=aux;
aux=aux->next;}
aux2->next=NULL;
}
}
return(aux);
}
static pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
void Pthread_mutex_lock(pthread_mutex_t *mtx)
{ int err;
if ( ( err=pthread_mutex_lock(mtx)) != 0 ) {
errno=err;
perror("lock");
pthread_exit((void *)17); /* maybe */
printf("Printf errore lock\n");
}
else printf("locked\n");}
void Pthread_mutex_unlock(pthread_mutex_t *mtx) {
int err;
if ( ( err=pthread_mutex_unlock(mtx)) != 0 ) {
errno=err;
perror("lock");
pthread_exit((void *)18); /* maybe */
printf("Printf errore unlock\n");
}
/* as above */
else printf("unlocked\n");
}
static void* consumer (void*arg) {
struct node * p;
printf("Sono il consumatore\n");
while(true) {/*printf("Ciclo while consumatore\n");*/
Pthread_mutex_lock(&mtx);
while (head == NULL){
printf("Mi metto ad aspettare\n");
pthread_cond_wait(&cond, &mtx);
printf("Waken up!\n"); fflush(stdout);
}
/*printf("Sto per estrarre\n");*/
p=estrai();
/*printf("Ho estratto\n");*/
Pthread_mutex_unlock(&mtx);
/* elaborazione p ... ... */
printf("%d.Sono il consumatore: il valore che mi è stato passato è: %d\n",i,p->info);
}
return (void*)0;}
static void* producer (void*arg) {
struct node * p;
for (i=0; i<N; i++) {
/*printf("Sto per produrre\n");*/
p=produci();
/*printf("Ho prodotto\n");*/
Pthread_mutex_lock(&mtx);
/*printf("Sto per inserire\n");*/
inserisci(p);
printf("Ho inserito, e risveglio il consumer\n");
pthread_cond_signal(&cond);
Pthread_mutex_unlock(&mtx);
}
return (void*)0;
}
int main (void){
pthread_t t1,t2;
int status1,status2;
printf("Ora creo il consumer\n");
pthread_create(&t2,NULL,&consumer,NULL);
printf("Creato il consumer\n");
printf("Ora creo il producer\n");
pthread_create(&t1,NULL,&producer,NULL);
printf("Creato il producer\n");
/* pthread_create(&t2,NULL,&consumer,NULL);*/
sleep(2);
pthread_join(t1,(void*) &status1);
/* pthread_cancel(t2);
pthread_join(t2,(void*) &status2);*/
/* elab return status */
return 0;
}
- 06-23-2010 #2
please edit your post and put the code inside of code tags, it makes it much easier to read
- 06-23-2010 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
School work? Sorry, not allowed on the forums.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-24-2010 #4Just Joined!
- Join Date
- Jun 2010
- Posts
- 11
No. I have to write a message server, and I'm trying to decompose it into simpler problems. Anyway, thank you for your nice answer.
- 07-04-2010 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Well, I've been looking at your code today and have to say that, in my opinion, it is not particularly clearly written, making analysis difficult. I'll try to get back to it a bit later for a better response and critique of the code. Sorry for the delay, but I've been traveling on business this past week.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote