next up previous contents
Next: Linux & Cluster Up: Shared Memory I; Processes, Previous: Threads   Contents


Exercise: Incomplete thread code segment

The following code segment is for finding the minimum of a list of integers. Complete the program, compile and execute.
1   #include <pthread.h> 
2   void *find_min(void *list_ptr); 
3   pthread_mutex_t minimum_value_lock; 
4   int minimum_value, partial_list_size; 
5 
6   main() { 
7       /* declare and initialize data structures and list */ 
8       minimum_value = MIN_INT; 
9       pthread_init(); 
10      pthread_mutex_init(&minimum_value_lock, NULL); 
11 
12      /* initialize lists, list_ptr, and partial_list_size */ 
13      /* create and join threads here */ 
14  } 
15 
16  void *find_min(void *list_ptr) { 
17      int *partial_list_pointer, my_min, i; 
18      my_min = MIN_INT; 
19      partial_list_pointer = (int *) list_ptr; 
20      for (i = 0; i < partial_list_size; i++) 
21          if (partial_list_pointer[i] < my_min) 
22              my_min = partial_list_pointer[i]; 
23      /* lock the mutex associated with minimum_value and 
24      update the variable as required */ 
25      pthread_mutex_lock(&minimum_value_lock); 
26      if (my_min < minimum_value) 
27          minimum_value = my_min; 
28      /* and unlock the mutex */ 
29      pthread_mutex_unlock(&minimum_value_lock); 
30      pthread_exit(0); 
31  }


Cem Ozdogan 2009-01-05