pthread编程中的分段错误

时间:2015-06-17 18:09:07

标签: c multithreading pthreads

我正在编写一个程序,用以下行读取文件:number operator number并希望使用pthreads处理这些带有4个独立线程的行,并显示每行的结果,格式为:

行号结果

我的文件有2个运算符:加号和减号。当我仅使用subReadThread(和subExec)或仅使用plusReadThread(以及plusExec)运行代码时,它可以正常运行并给我正确的结果但是当我想要运行时线程在一起我得到分段错误错误,执行过程不会继续。

以下是代码(也在http://pastebin.com/2fDSDucm):

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#define BUFFER_SIZE 5
#define TRUE 1
int numberOfLines ;
int sumCounter, subCounter, mulCounter, divCounter ;
int sumFileEnd, subFileEnd, mulFileEnd, divFileEnd ;
pthread_t *tid ;

clock_t begin, end;
double time_spent;

sem_t sumfull, sumEmpty ;
sem_t subfull, subEmpty ;
//sem_t mulfull, mulEmpty ;
//sem_t divfull, divEmpty ;
//sem_t outfull, outEmpty ;

char sumBuff [5][25] ;
char subBuff [5][25] ;
//char mulBuff [] ;
//char divBuff [] ;
//char outBuff [] ;

pthread_mutex_t sumMutex ;
pthread_mutex_t subMutex ;

void initData(){
        pthread_mutex_init(&sumMutex, NULL)      ;
        sem_init(&sumfull, 0, 0) ;
        sem_init(&sumEmpty, 0, BUFFER_SIZE) ;
        sem_init(&subfull, 0, 0) ;
        sem_init(&subEmpty, 0, BUFFER_SIZE) ;

        sumCounter = subCounter = mulCounter = divCounter = 0 ;
        sumFileEnd = subFileEnd = mulFileEnd = divFileEnd = 0 ;
}

void *plusReadThread(){
        printf("Enter to the plusReadThread\n");
        FILE *sumfp = fopen("sample.txt","r");
        char * line = NULL;
        size_t len = 0;
        ssize_t read;
        char *isSum ;
        int lineNumber = 1 ;
        if (lineNumber == 1){
                read = getline(&line, &len, sumfp) ;
                lineNumber ++ ;
        }
        while (!feof(sumfp)){
                printf("Enter to the while of plusReadThread\n");
                read = getline(&line, &len, sumfp) ;
                isSum = strchr(line, '+') ;
                if (isSum != NULL){
                        printf("Down on sumEmpty\n");
                        sem_wait(&sumEmpty);
                        pthread_mutex_lock(&sumMutex) ;
                        printf("Lock the mutex in plusReadThread\n");
                        if(sumCounter < BUFFER_SIZE){
                                printf("Counter was < 5 in plusReadThread\n");
                                char temp[20] ;
                                sprintf(temp, "%d", lineNumber);
                                strcat(temp, ".");
                                strcat (temp, line);
                                printf("lineNumber is %d line content is %s", lineNumber, line );
                                strcpy(sumBuff[sumCounter], temp);
                                printf("Copied the line in to the sumBuff\n");
                                printf("sumCounter = %d sumBuff[%d] = %s",sumCounter, sumCounter, sumBuff[sumCounter] );
                                sumCounter ++;
                                printf("sumCounter changed to %d\n", sumCounter);
                        }
                        pthread_mutex_unlock(&sumMutex);
                        printf("Unlock the mutex in plusReadThread\n");
                        sem_post(&sumfull);
                        printf("Signal on sumfull\n");
                }
                lineNumber ++;
        }
        if (feof(sumfp))
                sumFileEnd = 1;
        fclose(sumfp);
}
void *plusExec(){
        printf("Enter to the plusExec\n");
        char *search = "+" ;
        char *temp1;
        char *temp2;
        int operand1 ;
        int operand2 ;
        int result ;
        while (!sumFileEnd || sumCounter!=0){
                printf("Enter to the plusExec while\n");
                printf("Here sumCounter is %d \n", sumCounter);
                sem_wait(&sumfull);
                pthread_mutex_lock(&sumMutex);
                printf("Lock the mutex in plusExec\n");
                temp1 = strtok(sumBuff[sumCounter-1], search) ;
                operand2 = atoi(strtok(NULL, search));
                temp2 = strtok(temp1, ".") ;
                operand1 = atoi(strtok(NULL, ".")) ;
                result = operand1 + operand2 ;
                printf("%d %d\n", atoi(temp2) ,result);
                sumCounter --;
                sem_post(&sumEmpty);
                pthread_mutex_unlock(&sumMutex);
                printf("Unlock the mutex in plusReadThread\n");
        }
}

void *subReadThread(){
        printf("Enter to the subReadThread\n");
        FILE *subfp = fopen("sample.txt","r");
        char * line = NULL;
        size_t len = 0;
        ssize_t read;
        char *isSub ;
        int lineNumber = 1 ;
        if (lineNumber == 1){
                read = getline(&line, &len, subfp) ;
                lineNumber ++ ;
        }
        while (!feof(subfp)){
                printf("Enter to the while of subReadThread\n");
                read = getline(&line, &len, subfp) ;
                isSub = strchr(line, '-') ;
                if (isSub != NULL){
                        printf("Down on subEmpty\n");
                        sem_wait(&subEmpty);
                        pthread_mutex_lock(&subMutex) ;
                        printf("Lock the mutex in subReadThread\n");
                        if(subCounter < BUFFER_SIZE){
                                printf("Counter was < 5 in subReadThread\n");
                                char temp[20] ;
                                sprintf(temp, "%d", lineNumber);
                                strcat(temp, ".");
                                strcat (temp, line);
                                printf("lineNumber is %d line content is %s", lineNumber, line );
                                strcpy(subBuff[subCounter], temp);
                                printf("Copied the line in to the subBuff\n");
                                printf("subCounter = %d subBuff[%d] = %s",subCounter, subCounter, subBuff[subCounter] );
                                subCounter ++;
                                printf("subCounter changed to %d\n", subCounter);
                        }
                        pthread_mutex_unlock(&subMutex);
                        printf("Unlock the mutex in subReadThread\n");
                        sem_post(&subfull);
                        printf("Signal on subfull\n");
                }
                lineNumber ++;
        }
        if (feof(subfp))
                subFileEnd = 1;
        fclose(subfp);
}
void *subExec(){
        printf("Enter to the subExec\n");
        char *search = "-" ;
        char *temp1;
        char *temp2;
        int operand1 ;
        int operand2 ;
        int result ;
        while (!subFileEnd || subCounter!=0){
                printf("Enter to the subExec while\n");
                printf("Here subCounter is %d \n", subCounter);
                sem_wait(&subfull);
                pthread_mutex_lock(&subMutex);
                printf("Lock the mutex in subExec\n");
                temp1 = strtok(subBuff[subCounter-1], search) ;
                operand2 = atoi(strtok(NULL, search));
                temp2 = strtok(temp1, ".") ;
                operand1 = atoi(strtok(NULL, ".")) ;
                result = operand1 - operand2 ;
                printf("%d %d\n", atoi(temp2) ,result);
                subCounter --;
                sem_post(&subEmpty);
                pthread_mutex_unlock(&subMutex);
                printf("Unlock the mutex in subReadThread\n");
        }
}

int main (int argc, char* argv []){
        begin = clock();
        FILE *fp = fopen("sample.txt", "r") ;
        fscanf(fp, "%d", &numberOfLines) ;
        fclose(fp) ;

        initData() ;
        tid = (pthread_t*)malloc(4*sizeof(pthread_t)) ;
        pthread_create(&tid[0],NULL,plusReadThread,NULL);
        pthread_create(&tid[1],NULL,plusExec,NULL);
        pthread_create(&tid[2],NULL,subReadThread,NULL);
        pthread_create(&tid[3],NULL,subExec,NULL);

        pthread_join(tid[0] , NULL);
        pthread_join(tid[1] , NULL);
        pthread_join(tid[2] , NULL);
        pthread_join(tid[3] , NULL);

        end = clock();
        time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
        printf("%F\n", time_spent);
        exit(0);
}

当我仅使用subReadThread(和subExec)或仅使用sumReadThread(和sumExec)运行代码时,它可以正常工作并为我提供正确的结果。但是当我想一起运行线程时(如上面的链接),我得到一个分段错误错误,执行过程不会继续。 以下是我的一次运行(http://pastebin.com/nBqfiDcW)的输出:

6 1222
5 -2369
3 -2355
2 -1593
12 3306
11 4688
9 4549
7 4834
4 1827
10 -2571
23 7537
21 3789
19 1751
15 2803
13 2673
8 1304
34 588
33 3367
29 6922
26 4204
25 5212
20 364
18 -1890
17 37
37 1658
16 -1867
36 4541
14 -2184
48 7613
47 3093
44 3349
40 2516
30 -1163
28 -530
38 1016
27 1403
24 -1102
22 989
56 5370
54 4465
53 4918
35 -565
51 4918
32 1169
31 1781
43 941
42 525
41 1359
Segmentation fault

1 个答案:

答案 0 :(得分:0)

查看堆栈跟踪:

email

它位于cm.c的subExec()第138行,您调用了atoi()并且它已经分段。

仔细检查传递给atoi()的内容。

特别是,您传递了strtok()的结果,因此请仔细检查它是否正确。

此外,如果您执行程序,检查变量等,您可以更好地了解导致此问题的原因。

查看有关使用gdb的一些入门教程,例如this