0

I'm working on the bankers algorithm and am using a loop to create my threads. The issue is that the loop in only creating 4 threads when 5 of them should have been created. I've examined my loop and everything seems correct unless I'm missing something.

/* these may be any values >= 0 */

#define NUMBER_OF_CUSTOMERS 5
#define NUMBER_OF_RESOURCES 3

/* the available amount of each resource */
int available[NUMBER_OF_RESOURCES];

/*the maximum demand of each customer */
int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];

/* the amount currently allocated to each customer */
int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];

/* the remaining need of each customer */
int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];

int release_resources(int customer_num, int release[]){
   allocation[customer_num][NUMBER_OF_RESOURCES];
   return 1;
}

pthread_mutex_t mutex;

int main(int argc, char *argv[]){

  pthread_mutex_init(&mutex, NULL);

  pthread_t threads [3];
  int result;
  unsigned index;

  for(index = 0; index < NUMBER_OF_RESOURCES; index++){
    available[index] = strtol(argv[index+1], NULL,10);
  } 

  for(index = 0; index < NUMBER_OF_CUSTOMERS; ++index){
    printf("\nCreating thead %d", index);
    result = pthread_create(&threads[index],NULL,release_resources,1);  
  }

  //printf("Done");
  return 0;
}
Michael Albers
  • 3,603
  • 3
  • 19
  • 30
Jerum
  • 71
  • 2
  • 8
  • 1
    You should make sure you initialize `index` – bejado Mar 17 '17 at 00:11
  • 2
    Looks like it should create 5... try changing `printf("\nCreating thead %d", index);` to `printf("Creating thread %d\n", index);` (newline at the end instead of the start), or print a newline or flush the stream after the loop. – Dmitri Mar 17 '17 at 00:18
  • @bejado index is initialized to 0 once the loop begins. – Jerum Mar 17 '17 at 00:32
  • @Dmitri it didn't help unfortunately. – Jerum Mar 17 '17 at 00:33
  • Please state clearly how you are coming to your conclusion. Show any output or data that you based your conclusion on. But note that your main thread is not waiting for any of the child threads. So there is no guarantee that any of the threads will have executed when the main thread exits (taking down all child threads). Add a `pthread_exit` or `pthread_join` calls in the `main`. – kaylum Mar 17 '17 at 00:46
  • Looks like you're also trying to cram 5 `pthread_t`'s into an array of only 3... change `pthread_t threads [3];` to `pthread_t threads[NUMBER_OF_CUSTOMERS];` – Dmitri Mar 17 '17 at 00:53

1 Answers1

2

As I see, there is a bug in your main:

int main(int argc, char *argv[]){

  //...

  pthread_t threads [3];
  int result;
  unsigned index;

  //... 

  for(index = 0; index < NUMBER_OF_CUSTOMERS; ++index){
    printf("\nCreating thead %d", index);
    result = pthread_create(&threads[index],NULL,release_resources,1);  
  }

  //...
  return 0;
}

In this case, the array threads has a size of 3 items, and your index, from for loop, has a range from 0 to 4 (a size of 5 items), remember your constant #define NUMBER_OF_CUSTOMERS 5. I'm surprise that you are getting 4 thread, when it should have created 3 before an memory access violation.

You should redefine the array thread with the correct size, and using the constant NUMBER_OF_CUSTOMERS, as: pthread_t threads [NUMBER_OF_CUSTOMERS];

Jorge Omar Medra
  • 839
  • 1
  • 6
  • 16
  • 1
    The `threads` array is a stack variable. When creating the 4th array it would overflow the array and start writing into `result` and `index`. Since there is probably a lot of unused space on that page you would be able to go beyond 5 threads before an access violation. And that's most likely why he's not getting all 5. `index` is overwritten to a value larger than `NUMBER_OF_CUSTOMERS`. – Michael Albers Mar 17 '17 at 01:55
  • 1
    @MichaelAlbers, you are right. Since the `pthread_t` is a `unsigned long int` it use 32 bits and, as you said, it's probably that the next tow bytes into the memory are free. – Jorge Omar Medra Mar 17 '17 at 02:06