2012-11-30 2 views
0

이 코드를 작성하기 위해 주요 기능 만 작동하고 코드 0을 종료하는 것으로 간주하여 일부 튜토리얼을 확인하고 사용했습니다. 내 발자국이 생겼습니까? 그들은 어떻게 든 내 "일하는"기능에 연결하지 못했습니다. PThread 기본 프로그램,이 코드의 이상한 점

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <unistd.h> 
#include <math.h> 


struct node { // std linked list node 
    int value; 
    int worker; 
    struct node *next; 
}; 
// pthread_t* w_thread; 

int slots = 3; // only 3 threads are allowed to access the list 
int availableCheck(){ // check if thread can acces the list 
    if(slots < 3) return 0; 
    else return -1; 
} 

pthread_mutex_t mutp = PTHREAD_MUTEX_INITIALIZER; //condvar mutex 
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER; //condvar 

void * worker(void *i){ 
    long index = (long)i; 
    printf(" * I am worker # %lu : \n",pthread_self()); 
    // pthread_mutex_lock(&mutp); 
    // if(availableCheck() < 0){ 
     // printf(" ^^^ List not available yet... \n"); 
     // pthread_cond_wait(&condvar, &mutp); 
    printf("* Got data: %lu \n", index); 
    // pthread_cond_signal(&condvar); // 
    // pthread_mutex_unlock(&mutp); 

    return NULL; 
    // } 
} 

int main(int argc, char *argv[]){ 
    if (argc != 3){ 
     printf("Programm must be called with \n NR of elements and NR of workers! \n "); 
     exit(1); 
    } 

    int i,listSize,workersSize; 
    struct node *root; 
    struct node *iterator; 

//prepare list for task 
    listSize = atoi(argv[1]); 
    root = malloc(sizeof(struct node)); 

    root->value = rand() % 100; 
    // printf(">>> %d\n", root->value); 
    root->worker = 0; 

    iterator = malloc(sizeof(struct node)); 
    iterator = root; 

    for(i=1; i<listSize; i++){ 
     iterator->next = malloc(sizeof(struct node)); 
     iterator = iterator->next; 
     iterator->value = rand() % 100; 
     iterator->worker = i; 
     printf("node #%d worker: %d value: %d\n", i, iterator->worker,iterator->value); 
    } 
    printf("? List got populated\n"); 

// Create all threads to parse the link list 
    int ret;  
    pthread_t w_thread; 
    int nrWorkers = atoi(argv[2]); 
    pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread)); 
    for(i=0; i<listSize; i++){ 
     int *id = malloc(sizeof(int)); 
     *id = i; 
     ret = pthread_create (&w_threads[i], NULL, worker, (void *) &id); 
     if(ret) { 
      perror("Thread creation fail"); 
      exit(2);  
     } 
    } 
    int j; 
    for (j = 0; i < nrWorkers; j++){ 
     pthread_join(w_threads[j],NULL); 
    }  
} 

주석 기능의 일부

은/변수는 구현/사용하려고하지만,이 시점에서 내가 스레드가

+0

프로그램을 어떻게 실행하고 콘솔에 무엇을 인쇄합니까? – pmod

+0

'iterator = root;'왜 iterator'를 malloc'ing 한 후에 덮어 씁니까? –

+0

이 목록에서 반복을 시작할 수 있습니다. –

답변

2

내가 루프에있는 당신이해야 스레드를 생성하는 것으로 의심 기대 have :

for(i=0; i<nWorkers; i++){ 

또한 입력 인수에 체크/프린트를 추가하는 것이 좋습니다.

그리고 또 하나의 문제 : 그 다음,

void * worker(void *p_int){ 
    assert(p_int != NULL) 
    int index = *(int*)p_ind; 

* 빈에서 캐스팅 정확 int로 포인터의 경우 포인터를 참조 해제해야하므로, 근로자()에 포인터를 전달하고!

그리고 마지막으로 한 가지. 당신은 단지 한 줄의 코드에 배열을 할당 할 수 있으며 루트에 대한 할당 할 필요가 없습니다, 대신

for(i=1; i<listSize; i++){ 
    iterator->next = malloc(sizeof(struct node)); 
    iterator = iterator->next; 
    ... 

: 당신이 listSize을 알고 있다면, 그것은 왜이 링크 된 목록을 만들려고 나를 위해 분명하지 않다 : 보시다시피

p_list = (struct node*)malloc(listSize*sizeof(struct node)); 
assert(p_list); 
for(i=0; i<listSize; i++){ 
    p_list[i].value = ... 
    ... 

, 여기 다음 포인터가 필요하지 않습니다.

+0

링크 된 목록을 사용해야합니다 : \ –