c minimax using posix threads
Question
I am writing program with POSIX threads to find minimax in integer array. My question is how can I determine how many threads do I need to and is it correct to output minimum and maximum inside the thread function? And why do would I need dynamic threads?
Here is my code:
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#define num_of_threads 1
#define size 10
int array[size];
typedef struct st_minimax {
int min;
int max;
} minimax;
void *thread_minimax(void* ptr) { //thread is searching
int i;
minimax *data;
// data = (minimax*) malloc(minimax);
data = (minimax*) ptr;
data->max = array[0];
data->min = array[0];
for (i = 0; i < size; i++) {
if (array[i] > data->max)
data->max = array[i];
else if (array[i] < data->min)
data->min = array[i];
}
printf("Thread is calculating...\n");
printf("Min: %d", data->min);
printf("Max: %d", data->max);
pthread_exit(NULL); //exit thread
}
int main(int argc, char *argv[]) {
pthread_t worker[num_of_threads];
minimax data;
int t;
int i;
int ret;
for (i = 0; i < size; i++) { //initialize the array
printf("enter value for %d: ", i);
scanf("%d", &array[i]);
}
for(t=0;t<num_of_threads;t++)
{
printf("creating threads...%d\n",t);
ret = pthread_create(&worker[t], 0, thread_minimax, (void*) &data); //creating thread
sleep(1);
if (ret) {
fprintf(stderr, "thread err %d", ret);
exit(-1);
}
}
pthread_exit(NULL); //exit thread
return 0;
}
La solution
As per conversation with the OP author, N
(1d) arrays should be created with M
elements (i.e. N
rows and M
columns) and if there are N
arrays then there should be N
thread created (for array[0], thread-0). The threads should work on the corresponding array and get the min
and max
values for that array.
Here is the code which does that!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* N is the number of threads */
/* also N is the number of arrays */
/* row */
#define N 10
/* M is the length of each array */
/* column */
#define M 10
/* the 2d array which contains N rows and M columns */
int **matrix;
void display(void)
{
int i, j;
for (i=0 ; i<N ; i++) {
for (j=0 ; j<M ; j++) {
printf("%3d ", matrix[i][j]);
}
printf("\n");
}
return;
}
void * thread_handler(void *thread_id)
{
int *id = (int *) thread_id;
int i = *id, j;
int min = matrix[i][0];
int max = matrix[i][0];
for (j=1 ; j<M ; j++) {
if (matrix[i][j] > max)
max = matrix[i][j];
if (matrix[i][j] < min)
min = matrix[i][j];
}
printf("array[%d] min[%d] max[%d]\n", i, min, max);
pthread_exit(NULL);
}
int main(void)
{
int i, j, ret;
pthread_t tids[N];
int ids[N] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
srand(time(NULL));
matrix = (int **) malloc (sizeof(int *) * N);
for (i=0 ; i<N ; i++)
matrix[i] = (int *) malloc (sizeof(int) * M);
for (i=0 ; i<N ; i++) {
for (j=0 ; j<M ; j++) {
matrix[i][j] = rand() % 500;
}
}
display();
for (i=0 ; i<N ; i++) {
printf("Creating thread #%d \n", ids[i]);
ret = pthread_create(&tids[i], NULL, thread_handler, &ids[i]);
if (ret) {
printf("unable to create thread! \n");
exit(-1);
}
}
for (i=0 ; i<N ; i++) {
pthread_join(tids[i], NULL);
}
pthread_exit(NULL);
for (i=0 ; i<N ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
The following is the output:
$ gcc dynamic_array_threads.c -lpthread -o dat
$ ./dat
441 252 474 311 468 435 376 361 273 183
482 30 99 47 23 361 118 455 233 178
457 492 346 449 27 344 201 376 153 230
375 94 482 349 257 451 136 133 164 409
317 147 291 416 46 167 129 17 474 214
47 283 206 393 232 85 90 285 461 243
16 188 189 350 389 299 153 25 432 170
286 101 169 430 369 215 449 350 84 423
417 131 59 123 25 143 61 467 429 374
210 297 63 251 499 304 50 5 330 335
Creating thread #0
Creating thread #1
array[0] min[183] max[474]
Creating thread #2
Creating thread #3
array[2] min[27] max[492]
Creating thread #4
array[1] min[23] max[482]
Creating thread #5
array[4] min[17] max[474]
Creating thread #6
array[3] min[94] max[482]
array[5] min[47] max[461]
Creating thread #7
array[6] min[16] max[432]
Creating thread #8
Creating thread #9
array[8] min[25] max[467]
array[9] min[5] max[499]
array[7] min[84] max[449]
$
Hope this helps!
Autres conseils
You'd need to specify your criteria in order to determine how many threads you "need". As currently written, you only need 1, since the thread scans the whole array.
It is OK for a thread to print from a thread; however, if you are trying to find the min & max of a single array, and break up the work amongst multiple threads, then you probably wouldn't want to (instead, have the main process the results from each thread for the overall answer).
Not sure what the question is about dynamic arrays and dynamic threads; main can allocate the space for each array dynamically (similar to what you do in the commented out code in your thread); what needs to be more dynamic in your threads?
how can I determine how many threads do I need to
The nature of this problem does not demand any multi-threaded programming.
is it correct to output minimum and maximum inside the thread function?
There is nothing wrong with this. Its totally based on what you need (in case of homework
, it is totally depends on what your professor wants!).
And how could I modify the program to allocate dynamic array and dynamic threads
To allocate arrays dynamically, you can use malloc()
and to free them, use free()
as follows
int *ptr = NULL;
if ((ptr = (int *) malloc(sizeof(int) * len)) == NULL) {
printf("unable to allocate memory \n");
return -1;
}
/* you can use the ptr to read-from/write-to the allocated memory */
free(ptr);
And, what do you mean by dynamic threads
?
OTOH, when I executed your code snippet, I found nothing wrong in that. It just worked fine!