having trouble with implementing producer consumer problem

48 Views Asked by At

I am trying to implement the consumer producer problem, butI am running in a problem, I think I get stuck in an infinite loop, but I have no idea where.

Here is my code:

#define MAX_NUM 30 // borne sup des valeurs du tableau
#include <pthread.h>
#define MAX_SIZE 100 // taille maximale du tableau
#include <stdio.h>
#include <stdlib.h>
 
int tab [MAX_SIZE];
int size_tab ;
int Veut_entrer[2];
int Tour=0;
//1 equals to true and 0 equals to false
typedef unsigned int uint; //on declare le type variable globale
 
void* lock(int index)
{
Veut_entrer[Tour]=1;
Tour=1-index;
while(Veut_entrer[1-index]==1 && Tour==1-index)
        {
            //on attend 
        }
 
 }
 
 void unlock(Tour)
 {
    Veut_entrer[Tour]=0;
    
 }
 void * produce ( int index ) {
    uint count = 0;
    while ( count < 5) {
 debut section critique
    lock(Tour);
    //printf("Oui ou non %d  et %d" ,Veut_entrer[index],Tour);
    tab [size_tab ++] = rand() % MAX_NUM ;
    printf ("produce : %d\n", tab[size_tab-1]);
 fin section critique
    unlock(Tour);
    //printf("hein");
    count ++;
    //printf("ici");
    return (NULL);}
                            }
 int compare ( const void* a , const void* b ) {
    int const* pa = a ;
    int const* pb = b ;
    return (*pb - *pa );
 }
 
 void* consume ( int index ) {
    uint count = 0;
    printf("stuck");
 
 
    //printf("Oui ou non %d " ,Veut_entrer[0]);
    while ( count != 5) {
 if ( count < size_tab ) {
 printf("stuck");
 debut section critique
 lock(index);
     //printf("Oui ou non %d  et %d" ,Veut_entrer[index],Tour);
 qsort(tab ,MAX_SIZE / sizeof(int) ,sizeof(int) ,compare);//algortiheme qui permet de trier le tableau tant que la taille du tableau est inférieur a 5=count
 printf("consume : sort [%d elements] =>" ,size_tab);
 for(uint i = 0; i < size_tab ; i ++)
 printf ( " %d" , tab[i]);
    printf ( "\n" );
    count = size_tab ;
 fin section critique
    unlock(index);
                                }
                        }
     
    return(NULL);                       }
 
 
 int init()
 {
    Veut_entrer[0]=0;
    Veut_entrer[1]=0;   
 }
 int main (){
    int index_prod=0;
    int index_cons=1;
    pthread_t thread1 ;
    pthread_t thread2 ;
 char* m1 = " mon parametre " ;
 char* m2="truc";
    //printf("ici erreur");
    init();
    //printf("Oui ou non %d " ,Veut_entrer[0]);
    //produce(index);
    //consume(index);
    //printf("erreu");
    pthread_create (&thread1 , NULL ,produce(index_prod),( void *) m1);// The pthread_create() function starts a new thread in the calling process.
    //printf("erreur");
    //pthread_join (thread1 ,NULL );//The pthread_join() function shall suspend execution of the calling thread until the target thread terminates,
    printf("herr");
    pthread_create (&thread2,NULL,consume(index_cons),( void *) m2);
    //pthread_join(thread2,NULL);
    //pthread_join (thread1 ,NULL );
    return 0;
 }
 

The expected output is:

produce : 13
 consume : sort [1 elements ] = > 13
 produce : 16
 consume : sort [2 elements ] = > 16 13
 produce : 27
 consume : sort [3 elements ] = > 27 16 13
 produce : 25
 consume : sort [4 elements ] = > 27 25 16 13
 produce : 23
 consume : sort [5 elements ] = > 27 25 23 16 13

So each time I produce, meaning tab has at lease 1 element, I consume.

0

There are 0 best solutions below