gpt4 book ai didi

c - pthread 编程中的段错误

转载 作者:行者123 更新时间:2023-11-30 17:13:21 26 4
gpt4 key购买 nike

我正在编写一个程序来读取包含以下行的文件:数字运算符号,并希望使用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

最佳答案

查看堆栈跟踪:

#0  0x00007ffff7834517 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff7830f60 in atoi () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x0000000000401258 in subExec () at cm.c:138
#3 0x00007ffff7bc4182 in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0
#4 0x00007ffff78f147d in clone () from /lib/x86_64-linux-gnu/libc.so.6

它位于 cm.c 的 subExec() 第 138 行中,您称之为 atoi()它出现了段错误。

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

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

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

寻找一些关于使用 gdb 的入门教程,例如 this .

关于c - pthread 编程中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30899142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com