Find the answer to your Linux question:
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 ...
  1. #1
    Just 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)) != ) {
          
    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)) != ) {
          
    errno=err;
          
    perror("lock");
          
    pthread_exit((void *)18); /* maybe */
          
    printf("Printf errore unlock\n");
       }
       

      
    /* as above */
       
    else printf("unlocked\n");
    }


    static 
    voidconsumer (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 
    voidproducer (void*arg) {
      
    struct node p;
      for (
    i=0i<Ni++) {
        
    /*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;


  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    please edit your post and put the code inside of code tags, it makes it much easier to read

  3. #3
    Linux Guru Rubberman's Avatar
    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!

  4. #4
    Just 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.

  5. #5
    Linux Guru Rubberman's Avatar
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...