2016-04-20 43 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()