0

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...

Alexandre
  • 498
  • 2
  • 12
angyxpoo
  • 193
  • 2
  • 5
  • 16
  • If you put a `printf()` in just before the `sleep()` and print the value being used in `sleep()`, what value do you see? – Macattack Nov 07 '13 at 17:31
  • What's the value of `divide`? Mybe it's just that you sleep for too long... I would add another `printf()` with `r` before `sleep()` – Ingo Leonhardt Nov 07 '13 at 17:31
  • My divide is 100,000. And the value of r changes every time, once I got 1324 but it changes every time. – angyxpoo Nov 07 '13 at 18:10
  • @angyxpoo A value of 1324 means 22 (1324/60) minutes of sleep. I'm guessing you aren't waiting that long, with others getting longer sleeps. I'd recommend something like `rand_r(seed)%10` to limit yourself to at most 10 second sleeps. – Macattack Nov 07 '13 at 18:28

1 Answers1

1

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.

Macattack
  • 1,917
  • 10
  • 15