【问题标题】:-lpthread: Program runs on zsh terminal but won't run on linux bash-lpthread:程序在 zsh 终端上运行,但不会在 linux bash 上运行
【发布时间】:2021-12-09 09:40:37
【问题描述】:

我在 mac 上使用 VScode,执行时使用:gcc Test2.c -o Test2 -lpthread。这很好用。但是当我在 linux bash 上做同样的事情时,它返回我有一个分段错误。

由于没有错误消息,我不确定我的内存问题出在哪里。

这是我的代码(使用线程创建排序算法):

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

int arraysize;
int midarraysize;
int num1[10000];

typedef struct {
    int starting_index;
    int ending_index;
} pointer;

void *sorter(void *params); // MERGE SORT used
void *merger(void *params);

void merge(int arr[], int low1, int mid, int high1) {
    
    int i, j, k;
    int firststart = mid - low1 + 1;
    int secondstart = high1 - mid;
    int first[firststart], second[secondstart]; 
    for (i = 0; i < firststart; i++) {
        first[i] = arr[low1 + i];
    }
    for (j = 0; j < secondstart; j++) {
        second[j] = arr[mid + 1 + j];
    }

    i = 0;
    j = 0;
    k = low1;

    while (i < firststart && j < secondstart) {
        if (first[i] <= second[j]) {
            arr[k] = first[i];
            ++i;
        } else {
            arr[k] = second[j];
            j++;
        }
        k++;
    }
    while (i < firststart) {
        arr[k] = first[i];
        i++;
        k++;
    }
    while (j < secondstart) {
        arr[k] = second[j];
        j++;
        k++;
    }

}

void sort(int arg[], int low, int high) {
    int mid;
    if (low < high) {
        mid = (low + high)/ 2;
        sort(arg, low, mid);
        sort(arg, mid+1, high);    
        merge(arg, low, mid, high);
    } else{
        return;
    }
}

int main(){ 

    FILE *IntList= fopen("IntegerList.txt", "r");
    
    char cIntlist[2000]; 
    char fullnum[100][100];
    int num[2000];

    int i, j, count, totalnum;

    fgets(cIntlist, 2000, IntList); //at some point, it stops reading the file

    for(i =0; i <= (strlen(cIntlist)); i++){
        if(cIntlist[i] == ','){// || NULL || '\0'){
            fullnum[count][j] = '\0' || NULL;
            count++;
            j = 0;
            totalnum ++;
            //printf("total number: %d\n", totalnum);
        } else {
            fullnum[count][j] = cIntlist[i];
            j++;
        }
    }

    //printf("total number: %d\n", totalnum);

    printf("\n-----UNSORTED NUMBERS-----\n");

    for(i = 0;i < 2000;i++){
        //printf(" full sum%s\n", fullnum[i]);
        num[i] = atoi(fullnum[i]);
        if(num[i] == 0){
            //printf("indication\n\n");
            //printf("check size array: %lu\n\n", strlen(num));
            break;
        }
        printf("%d,",num[i]);
    }

    arraysize = totalnum +1;
    midarraysize = arraysize/2;
    
    //printf("\nnew array list:\n");
    //memcpy(num1[1000],num[2000], sizeof(num1));
    for(i = 0; i < 2000; i++){
        num1[i] = num[i];
    }
    for(i = 0;i < 2000;i++){
        num1[i] = atoi(fullnum[i]);
        //printf("%d,",num[i]);
        if(num1[i] == 0){
            //printf("indication\n\n");
            //printf("check size array: %lu\n\n", strlen(num));
            break;
        }
    }
    
    printf("\n\nMidpoint: %d", midarraysize);
    printf(", Array Size: %d\n", arraysize);
    
//////////////////////////////////////////////////////////////////////

    pthread_t thread1;
    pthread_t thread2;
    
    // char *tparam1 = "sort1";
    // char *tparam2 = "sort2";
    // char *tparam3 = "merge";

    pointer *tparam1 = (pointer *)malloc(sizeof(pointer));
    pointer *tparam2 = (pointer *)malloc(sizeof(pointer));
    
    pthread_create(&thread1, NULL, sorter, tparam1);
    pthread_create(&thread2, NULL, sorter, tparam2);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    
    pthread_t thread3;
    pointer *tparam3 = (pointer *)malloc(sizeof(pointer));
    pthread_create(&thread3, NULL, merger, tparam3);
    pthread_join(thread3, NULL);
    
    FILE *outputfile = fopen("SortedIntegerList.txt", "w");

    fprintf(outputfile, "\n -----SORTED INTEGER LIST-----\n");
    printf("\n-----SORTED LIST-----\n\n");

    for(i = 0; i <= arraysize; i++){
        if(num1[i] == 0){
            break;
        }
        printf("%d,",num1[i]);
        fprintf(outputfile, "%d,", num1[i]);
    }
    printf("\n\n");

    free(tparam3);
    free(tparam2);
    free(tparam1);
    fclose(outputfile);
    fclose(IntList);
    pthread_exit(0);
    return 0;
}


void *sorter(void *params){

    sort(num1, 0, midarraysize);
    sort(num1, midarraysize + 1, arraysize - 1);

    pthread_exit(0);
}

void *merger(void *params){

    merge(num1, 0, (arraysize/2), arraysize-1);

    pthread_exit(0);
}

不知道内存问题出在哪里,估计是线程使用不正确吧?

【问题讨论】:

  • 当您使用调试器时,它告诉您是什么导致了段错误?
  • When you used a debugger, what did it tell you was causing the segfault? it just says: Segmentation Fault 因此,当程序在调试器中并在段错误后停止时,使用调试器获取回溯,并查看段错误发生在哪一行。并从那里开始消除错误。记得在启用调试信息的情况下编译 (-g)。
  • 请同时编译带有警告-Wall -Wextra 并尝试修复所有警告。为什么要并行运行两个 sorter 函数?他们不会处理同一组数据并覆盖自己的结果吗?它们之间有同步吗?为什么void *params 没有在线程函数中使用?
  • 你也可以尝试使用valgrind来运行你的程序,它会检测到很多指针问题和越界错误。
  • 请注意,shell(zsh 和 bash)与此无关。相关的区别是 MacOS 与 Linux。但即便如此,也可能只是您的错误代码碰巧在两个操作系统之间以不同的方式中断,这很常见。

标签: c linux macos segmentation-fault pthreads


【解决方案1】:

pthread_exit(0); 主线程而不是做join()。结果不会好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多