Question

So i'm working on this code that is a producer-consumer code. It runs completely through the program, it just never actually executes anything in the critical section because it never really wakes up from sleep! I've added print statements everywhere to try to figure out where the code is executing, and it enters both producer and consumer functions, but never enters any portion of it after the sleep() function.

Here is my main:

 /* 1. Get command line arguments argv[1], argv[2], argv[3] */

 /* n1=arg[2], n2=arg[3]
 /* 2. Initialize buffer, mutex, semaphores, and other global vars */

 /*create the mutex lock */
 /* create the semaphore and initialize it to 3 */
 /*creating full semaphore and initializing it to 0 */




/*** critical section ***/


 /* get the default attribute */
 pthread_attr_init(&attr);
 /* 3. Create producer thread(s) */
 while (count < n1)
   {
     pthread_t tid;
     /* create a new thread */
     pthread_create(&tid, &attr, producer, NULL);
     count++;
   }
     printf("OUTSIDE OF PRODUCER CREATION\n");
 /* 4. Create consumer thread(s) */
 count = 0;
 while(count < n2)
   {
     pthread_t tid2;
     /* create a new thread */
     pthread_create(&tid2, &attr, consumer, NULL);
     count++;
   }

 printf("after the crit section \n");
 /* 5. Sleep */
 /* 6. Realease resources, e.g destroy mutex and semaphores */

i've included for the most part just the code that I know i'm having a problem with and the rest are comments. and here is the code for my producer. consumer is basically the same:

void *producer(void *param) {
    buffer_item rand;
    unsigned int *seed;
    seed = (unsigned int *)malloc(sizeof(unsigned int));
    *seed = 10;
    while (1) {
        printf("Inside producer function\n");
        /* Sleep for a random period of time */
        r = (rand_r(seed))/divide;
        sleep(r);
        printf("producer slept\n");
        //wait(empty)
       //wait(mutex)
       printf("producer locked mutex\n");
       //crit section - add item to buffer
       /*Generate a random number */
       /*insert random number*/
       printf("producer inserted item \n");
       if (resp < 0)
           printf("Producer error condition\n"); //Report error condition
       //signal mutex
       //signal empty
    }
}

so when i run a.out 4 4 4, i get this as my output:

Inside producer function
Inside producer function
Inside producer function
OUTSIDE OF PRODUCER CREATION
Inside producer function
inside consumer function
inside consumer function
inside consumer function
after the crit section 
inside consumer function

I'm not sure if its normal that things look like they are running out of order... it is executing 4 times but as you can see if never hits that print statement that happens after my sleep function (for both producer and consumer)

This is homework so i'm just looking for a bit more direction on this...

Was it helpful?

Solution

From the discussion, your error was that your sleep() values were too large, one you quoted coming out to 22 minutes. Rather than division(/) to limit the values, modulo(%) will put your values within a range. I recommend %10, or some other value which will limit the sleeps to a reasonable range.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top