0

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;
}
4

3 に答える 3

1
  1. 「必要な」スレッドの数を決定するには、基準を指定する必要があります。現在書かれているように、スレッドは配列全体をスキャンするため、必要なのは1つだけです。

  2. スレッドがスレッドから印刷しても問題ありません。ただし、単一の配列の最小値と最大値を見つけて、複数のスレッド間で作業を分割しようとしている場合は、おそらく(代わりに、メインプロセスに各スレッドからの結果を全体的な答えとして使用させる)ことは望ましくありません。 )。

  3. 動的配列と動的スレッドについての質問が何であるかわからない。mainは、各配列に動的にスペースを割り当てることができます(スレッドのコメントアウトされたコードで行うのと同様です)。スレッドでより動的にする必要があるものは何ですか?

于 2012-01-04T15:18:41.470 に答える
1

必要なスレッド数をどのように判断できますか

この問題の性質上、マルチスレッドプログラミングは必要ありません。

スレッド関数内に最小値と最大値を出力するのは正しいですか?

これには何の問題もありません。それは完全にあなたが必要とするものに基づいています(の場合homework、それはあなたの教授が何を望んでいるかに完全に依存します!)。

そして、動的配列と動的スレッドを割り当てるようにプログラムを変更するにはどうすればよいですか?

アレイを動的に割り当てるには、次のようmalloc()に使用して解放します。free()

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);

そして、どういう意味dynamic threadsですか?

OTOH、私があなたのコードスニペットを実行したとき、私はその中に何も悪いことを発見しませんでした。それはうまくいきました!

于 2012-01-04T15:18:55.000 に答える
1

OP 作成者との会話によると、N(1d) 配列はM要素 (N行とM列) を使用して作成する必要があり、N配列がある場合はNスレッドを作成する必要があります (配列 [0]、スレッド 0 の場合)。スレッドは対応する配列で動作し、その配列の値minmax値を取得する必要があります。

これがそれを行うコードです!

#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;
}

出力は次のとおりです。

$ 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]
$  

お役に立てれば!

于 2012-01-04T16:13:08.560 に答える