2016-04-20 27 views
0

我目前正在使用C語言中使用pthreads的自定義線程調度程序項目。我一直在用它的概念掙扎,但終於得到了我期望看到的行爲,除了分段錯誤。使用子線程發生分段錯誤

我的工作是註冊五個子線程,並根據存儲在數組中的ID順序來安排每個子線程。我所做的就是撥打pthread_mutex_lock,並告訴哪一個不安排的子線程先等待。我做了一些東西到我的櫃檯,以跟蹤下一個孩子應該安排在什麼時候,並且在一個子線程增加計數器到五之後,它應該喚醒其他線程並且執行與主循環定義一樣多的次數。

所有變量:

#define NTHREADS   5    /* Number of child threads  */ 
#define NUM_LOOPS 10    /* Number of local loops   */ 
#define SCHEDULE_INTERVAL 1   /* thread scheduling interval  */ 

#define errexit(code,str) fprintf(stderr,"%s: %s\n",(str),strerror(code));exit(1); 

int schedule_vector[NTHREADS];  /* The thread schedule vector  */ 

int flag = 0; 
pthread_cond_t cv;     // condtitional variable 
pthread_mutex_t mtx;    // mutex semaphore 1 
int globalcounter = 0; 
int currentThread; 
#define TASK_LIMIT 6 

這裏是父線程:

int main(int argc,char *argv[]) 
{ 
    int  i;       
    int  worker;      
    int  ids[NTHREADS];    
    int  errcode;     
    int  *status;      
    int  policy;      

    pthread_t threads[NTHREADS];   

    /* Create child threads --------------------------------------------- */ 
    for (worker = 0; worker < NTHREADS; worker++) 
    { 
    ids[worker] = worker;    

    printf("creating child thread using id %d\n", worker); 

    /* Create a child thread ----------------------------------------- */ 
    pthread_create (     
     &threads[worker],  
     NULL,     
     my_thread,   
     &ids[worker]);  
    } 

    /* Initialize the thread schedule vector -------------------------- */ 
    schedule_vector[0] = 0;  /* First thread to be executed (0)  */ 
    schedule_vector[1] = 1;  /* Second thread to be exceuted (1)  */ 
    schedule_vector[2] = 2;  /* Third thread to be executed (2)  */ 
    schedule_vector[3] = 3;  /* Fourth thread to be executed (3)  */ 
    schedule_vector[4] = 4;  /* Fifth thread to be executed (4)  */ 

    signal(SIGALRM, clock_interrupt_handler); 
    alarm(SCHEDULE_INTERVAL); 
    printf("handler set up\n"); 

    /* Reap the threads as they exit ----------------------------------- */ 
    for (worker = 0; worker < NTHREADS; worker++) 
    { 
     /* Wait for thread to terminate --- */ 
     if (errcode=pthread_join(threads[worker],(void *) &status)) 
     { errexit(errcode,"pthread_join"); } 

     /* Check thread's exit status and release its resources -------- */ 
     if (*status != worker) 
     { 
     fprintf(stderr,"thread %d terminated abnormally\n",worker); 
     exit(1); 
     } 
    } 

    /* The main (parent) thread terminates itself ---------------------- */ 
    return(0); 
    } 

這裏是子線程功能:

void *my_thread(void * arg) 
{ 
    long int i;     
    long int counter;  
    int myid=*(int *) arg; 
    counter = 0; 
    printf("\nI am thread #%d\n\n", myid); 

    /* Main loop ------------------------------------------ */ 
    for (i = 0; i < NUM_LOOPS; i++) 
    { 

     currentThread = myid; 
     pthread_mutex_lock(&mtx); 
     while(myid != schedule_vector[flag]){ 
      pthread_cond_wait(&cv, &mtx); 
     } 
     counter++; 
     globalcounter = counter; 


    printf("Thread: %d is running ...\n", myid); 
    usleep(100000); 
    } 
    return arg; 
} 

,這裏是我的中斷服務程序:

void clock_interrupt_handler(void) 
{ 

    printf("scheduler started ++++++++++++++++++++++++++++++++++ \n"); 
    if (currentThread == schedule_vector[flag]) { 
    printf("scheduled thread received: thread %d, and it's counter is at %d\n", currentThread, globalcounter); 
    while(globalcounter < TASK_LIMIT){ 
    if(globalcounter == 5){ 
     flag++; 
     pthread_cond_broadcast(&cv); 
    } 
    pthread_mutex_unlock(&mtx); 
    alarm(SCHEDULE_INTERVAL); 
    } 
    } else { 
    printf("unscheduled thread received, putting thread %d with count %d to sleep...\n", currentThread, globalcounter); 
    alarm(SCHEDULE_INTERVAL); 

    } 


} 

這是輸出:

scheduler started ++++++++++++++++++++++++++++++++++ 
scheduled thread received: thread 0, and it's counter is at 1 
Thread: 0 is running ... 
Thread: 0 is running ... 
Thread: 0 is running ... 
Thread: 0 is running ... 
Segmentation fault (core dumped) 

它基本上重複這種行爲,但每個線程。我想知道究竟是什麼導致了seg錯誤

+0

還請張貼創建代碼,並啓動線程。 – fluter

+0

@fluter添加了父母 –

+0

@fluter好的,就是這樣。對不起......我應該把它放進去...... –

回答

0

似乎pthread_mutex_lock/pthread_mutex_unlock不配對。

正確的代碼看起來像

pthread_mutex_lock() 
pthread_cond_broadcast() 
pthread_mutex_unlock() 

pthread_mutex_lock() 
pthread_cond_wait() 
pthread_mutex_unlock()