pthread producer consumer problem

This is a discussion on pthread producer consumer problem within the C Programming forums, part of the General Programming Boards category; hi all, I am learing pthreads. I am trying to implement producer/consumer(5producers,5consumers) problem with buffer size=10. given below is the ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Unhappy pthread producer consumer problem

    hi all,

    I am learing pthreads. I am trying to implement producer/consumer(5producers,5consumers) problem with buffer size=10.

    given below is the program written using pthreds. (os is redhat linux9)

    Code:
    #include<pthread.h>
    
    
    int counter=0;
    int max=10;
    
    
    pthread_mutex_t counter_mutex= PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t condp_mutex= PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t condc_mutex= PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t condVarProd= PTHREAD_COND_INITIALIZER;
    pthread_cond_t condVarCons= PTHREAD_COND_INITIALIZER;
    
    void *prodfun();
    void *consfun();
    
    
    main()
    {
    
    	pthread_t prothr[10],conthr[10]; 
    	int i;
    	for(i=0; i<5; i++)
    	{
    		pthread_create(&prothr[i],NULL,&prodfun,NULL);
    		pthread_create(&conthr[i],NULL,&consfun,NULL);
    	}
    	for(i=0; i<5; i++)
    	{
    		pthread_join(prothr[i],NULL);
    		pthread_join(conthr[i],NULL);
    	}
    
    }
    
    
    void * prodfun()
    {
    
    	while(1)
    	{
    		pthread_mutex_lock(&condp_mutex);
    		while(counter>=10)
    		{
    			{ 
    				pthread_cond_wait(&condVarProd,&condp_mutex); 
    			}
    		}
    		pthread_mutex_unlock(&condp_mutex);
    
    		//pthread_mutex_lock(&counter_mutex);
    		counter++;
    		pthread_cond_signal(&condVarCons); 
    		printf("I am producer %ld counter value=%d\n",pthread_self(),counter);
    		pthread_mutex_unlock(&condp_mutex);
    		//pthread_mutex_unlock(&counter_mutex);
    		//if(counter==5)
    			//sleep(1);
    	}
    }
    
    void * consfun()
    {
    	while(1)
    	{
    	        sleep(1);
    		pthread_mutex_lock(&condc_mutex);
    		while(counter<=0)
    		{
    			pthread_cond_signal(&condVarProd); 
    			pthread_cond_wait(&condVarCons,&condc_mutex); 
    		}
    	//	pthread_mutex_unlock(&condc_mutex);
    
    	//	pthread_mutex_lock(&counter_mutex);
    		if(counter>0)
    		{
    			printf("I am consumer %ld counter value=%d \n",pthread_self(),counter);
    			counter--;
    			pthread_cond_signal(&condVarProd); 
    		}
    	//	pthread_mutex_unlock(&counter_mutex);
    		pthread_mutex_unlock(&condc_mutex);
    
    	}
    }



    Output is :
    Code:
    I am producer 1082338496 counter value=1
    I am producer 1082338496 counter value=2
    I am producer 1082338496 counter value=3
    I am producer 1082338496 counter value=4
    I am producer 1082338496 counter value=5
    I am producer 1082338496 counter value=6
    I am producer 1082338496 counter value=7
    I am producer 1082338496 counter value=8
    I am producer 1082338496 counter value=9
    I am producer 1082338496 counter value=10
    I am consumer 1090726976 counter value=10
    I am consumer 1116941120 counter value=9
    I am consumer 1133718080 counter value=8
    I am consumer 1150495040 counter value=7
    I am consumer 1167272000 counter value=6
    I am producer 1082338496 counter value=6
    I am producer 1082338496 counter value=7
    I am producer 1082338496 counter value=8
    I am producer 1082338496 counter value=9
    I am producer 1082338496 counter value=10
    I am consumer 1090726976 counter value=10
    I am consumer 1116941120 counter value=9
    I am consumer 1133718080 counter value=8
    I am consumer 1150495040 counter value=7
    I am consumer 1167272000 counter value=6
    I am producer 1082338496 counter value=6
    I am producer 1082338496 counter value=7
    I am producer 1082338496 counter value=8
    I am producer 1082338496 counter value=9
    I am producer 1082338496 counter value=10


    Once the producer gets control then consumer is not given control until couter value =10 or 1 or 6.


    How to make the output more random so that the output should be more random than looking as a pattern???

    Please help.

    Thanks in advance......

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Possibly add a delay loop in each thread after it releases the mutex to give the other threads a shot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Consumer program
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-27-2006, 06:21 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Producer consumer problem
    By traz in forum C Programming
    Replies: 2
    Last Post: 11-08-2002, 08:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21