在pthread中使用结构时出现分段错误

时间:2020-04-22 01:50:15

标签: c struct pthreads

我一直试图用线程来拆分写入文件,为此,我试图使用结构来保存文件的开始和结束位置。代码可以编译,但是,当代码尝试创建多个线程并且不执行线程代码时,我遇到了段错误。我是否正确使用结构?

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX_THREADS 100
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

struct position {
    int start;
    int end;
};

 void *ThreadJob(void *id) //what the thread should do
 {
    pthread_mutex_lock(&mutex);
    struct position *b;
    printf("\nstart: %d \nend: %d\n", (*b).start, (*b).end);
    double* arrayPtr = malloc( 100000* sizeof(double));
    FILE *file;
    FILE* nFile; // New file
    double n; 
    nFile = fopen("newTriData1.txt","a");
    char line[128]; //the lines of the txt file
    file = fopen("TriData1.txt", "r");
    long tid;
    tid = (long)id;
    int count = 0;
    while (fgets(line, 128, file))  //gets the lines from the txt file - line by line
    { 
        sscanf(line ," %lf", &arrayPtr[count]); //converts the value on the line into a double to manipulate     
        count++; //increment the count
    }
    free(arrayPtr);
    while((*b).start<(*b).end){
        double x = (sqrt(8*arrayPtr[(*b).start]+1) - 1) / 2; //equation to detect triangular numbers 
        if (x == floor(x)) //checks if the value has a remainder. The value should be a whole number
            {
                fprintf(nFile, "\nNumber %s: Triangular\n", line); //if true writes the value and triangular 
            }
        else 
            {
                fprintf(nFile, "\nNumber %s: Not Triangular\n", line);

            }
        (*b).start++;
    }
    (*b).start=(*b).end;
    (*b).end = ((*b).end + (*b).end);
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

 int main (void) //main
 {
    struct position a;
    (a).start=0;
    int line_count;
    FILE *file;
    double count_lines = 1.0;
    char check;
    double i = 4;
    file = fopen("TriData1.txt", "r");
    double divider;
    check = getc(file);
    while (check != EOF)
    {
        if (check == '\n')
        {
            count_lines = count_lines + 1;
        }

        check = getc(file); //take the next character from the file
    }
    printf("cl: %f", count_lines);
    double vo = fmod(count_lines,4); //using fmod to find which number divides the line count into equal parts for the number of threads
    if (fmod(count_lines,4) == 0) {
    double value1 = count_lines/4;
    double value2 = count_lines/4;
    double value3 = count_lines/4;
    double value4 = count_lines/4;

    printf("v1: %f \n v2: %f \n v3: %f \n v4: %f", value1,value2,value3,value4);
    divider =4;
    line_count = count_lines/4;
    (a).end=line_count;
    }
    else
    {
        while (fmod(count_lines, i) != 0) //if the value is not divisible by 4 then i will increment until a suitable divider is found
        {
            i++;
            divider = i;
            line_count = count_lines/i;
            printf("divider: %f", divider);
        }
        (a).end=line_count;
    }

    fclose(file); //close file.
    printf("There are %f lines in this file\n", count_lines);
    printf("\nstart: %d \nend: %d\n", (a).start, (a).end);
    pthread_t threads[MAX_THREADS];
    int thread;
    long threadNum ;
    for(threadNum=0; threadNum<divider; threadNum++){
       printf("Creating thread %ld\n", threadNum);
       thread = pthread_create(&threads[threadNum], NULL, ThreadJob, (void *)threadNum);
       if (thread){
          printf("ERROR; %d\n", thread);
          exit(-1);
       }
    }
    pthread_exit(NULL);
    return 0;
 }

1 个答案:

答案 0 :(得分:3)

几乎每行代码都是错误的,但是大多数错误只会使程序执行错误的操作,而不是完全崩溃。这只是导致程序崩溃的错误:

struct position *b;
printf("\nstart: %d \nend: %d\n", (*b).start, (*b).end);

这可能会导致段错误,因为您不能取消引用未初始化的指针。

free(arrayPtr);
while((*b).start<(*b).end){
    double x = (sqrt(8*arrayPtr[(*b).start]+1) - 1) / 2; //equation to detect triangular numbers 

那可能是段错误,因为释放内存后就无法使用。另外,由于您仍未初始化b,因此它的最后一行可能会出现段错误。

    (*b).start++;
}
(*b).start=(*b).end;
(*b).end = ((*b).end + (*b).end);

所有这些行都可能会发生段错误,因为b仍未初始化。

坦率地说,您现在应该放弃诸如线程之类的高级主题,而努力尝试理解C的基础。