C多线程wordcount程序分段故障问题

时间:2015-08-15 19:21:41

标签: c multithreading segmentation-fault pthreads

我几个月来一直在反对这个项目。这是我上一学期上课的一个任务,当我通过时,我永远无法使这一个任务正常工作(Seg Fault)。我欢迎任何帮助或提示,但我非常感谢解释和解释。

该程序应该接收包含文件名列表的文件名(我的例子为240)。这些文件位于与列表和程序相同的目录中的文件夹中。这个程序应该采用这个列表并解析4个线程,为每个线程均匀地分割文件名(对于我的例子,每个线程60个)。然后,每个线程获取60个文件名的列表,并逐个打开每个文件,在每个文件上执行 WordCount 功能。一旦线程完成它们的任务,它们就应该按顺序打印每个文件的结果,每个线程都在自己的块中(即Thread1结果| Thread2结果|线程3结果等......)。

我已经调试了很多,并且知道在创建线程之前一切都按预期工作。我的问题似乎是在线程启动/执行期间。我已经尝试过将mutex添加到混合中,但遗憾的是它没有帮助。我似乎错过了某些东西或者在思考某些东西,因为我的一些同学已经向我展示了他们更紧凑的代码。请协助。谢谢!

这是Main:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MaxLine 200
#define NUMTHREADS 4

char Line[MaxLine];
pthread_mutex_t Lock = PTHREAD_MUTEX_INITIALIZER;

typedef struct thread {
    int id;
    char file;
}ThreadData;

/* ThreadFunction will give each thread its processes to execute */
 void *threadFunc (void *td) {
    ThreadData *data = (ThreadData*)td;
    int thread_num=data->id;
    char filename=data->file;
    printf("thread debug tid: %d and file: %c",thread_num, filename);
    pthread_mutex_trylock(&Lock);
    FILE *fn = fopen(filename, "r");
    if (fn == NULL) {
        error("ERROR: Opening file");
        return 1;
    }
    while (fgets(Line, sizeof(Line), fn) != NULL) {
        CountWord(thread_num, Line);
    }
    fclose(fn);
    free(data);
    pthread_mutex_unlock(&Lock);
    pthread_exit(NULL);
}

int main(int argc, char *argv[]){
    char buf[20];
    int c, i, t, tnum, QUEUETOTAL;
    pthread_t thread[NUMTHREADS];
    ThreadData td[NUMTHREADS];

    if (argc != 2){
        fprintf(stderr,"ERROR: Usage must be Countfile filename\n", argv[0]);
        exit(0);
    }
    char const* const filename = argv[1];
    FILE* file = fopen(filename, "r");
    if ( file == 0 ){
        printf( "Could not open file!\n" );
        exit(0);
    }

    /* Count iterations of while loop to divide files among threads. */
    while (fgets(Line, sizeof(Line), file)){
        QUEUETOTAL++;
    }

    /* Divide work for threads. */
    int thread2taskstart=(QUEUETOTAL/NUMTHREADS); //60
    int thread3taskstart=(QUEUETOTAL/NUMTHREADS)*2; //120
    int thread4taskstart=(QUEUETOTAL/NUMTHREADS)*3; //180
    // QUEUETOTAL = 240

    rewind(file);
    FILE *tempfile1  = fopen("temp1.txt","w");
    for (i=0; i<thread2taskstart; i++) {
        // populate tempfile1 with entries 1-60
        if(fgets(Line,sizeof(Line),file)!=NULL) {
            fputs(Line,tempfile1);
            //printf("Debug temp1: %s",Line);
        }
    }
    fclose(tempfile1);
    FILE *tempfile2  = fopen("temp2.txt","w");
    for (i=thread2taskstart; i<thread3taskstart; i++) {
        // populate tempfile2 with entries 60-120
        if(fgets(Line,sizeof(Line),file)!=NULL) {
            fputs(Line,tempfile2);
            //printf("Debug temp2: %s",Line);
        }
    }
    fclose(tempfile2);
    FILE *tempfile3  = fopen("temp3.txt","w");
    for (i=thread3taskstart; i<thread4taskstart; i++) {
        // populate tempfile3 with entries 120-180
        if(fgets(Line,sizeof(Line),file)!=NULL) {
            fputs(Line,tempfile3);
            //printf("Debug temp3: %s",Line);
        }
    }
    fclose(tempfile3);
    FILE *tempfile4  = fopen("temp4.txt","w");
    for (i=thread4taskstart; i<=QUEUETOTAL; i++) {
        // populate tempfile3 with entries 180-240
        if(fgets(Line,sizeof(Line),file)!=NULL) {
            fputs(Line,tempfile4);
            //printf("Debug temp4: %s",Line);
        }
    }
    fclose(tempfile4);
    fclose(file);

    /* Prepare parameters & launch (4) threads.  Wait for threads
    to finish & print out results as specified in assignment. */
    printf("Counting files …\n");

    for(t=0;t<NUMTHREADS;t++){
        tnum=t+1;
        snprintf(buf, "temp%d.txt", tnum);
        printf("debug tnum and array: %d and %s\n",tnum, buf);
        td[t].id = tnum;
        td[t].file = buf;
        // Creates a new thread for each temp file.
        pthread_create(&thread[t], NULL, threadFunc, td);
    }
    // Joins threads.
    printf("debug: printing threads \n");
    for(t=0;t<NUMTHREADS;t++){
    pthread_join(thread[t], NULL);
    printf("-------------------------  Processes finished for Thread %d  ----------------------- \n",t+1);
    }
    return 0;
}

这是计数功能:

#include <stdio.h>

int CountWord(int tinfo, char cfile){
    int i;
    int ccount = 0;
    int wcount = 0;
    int lcount = 0;
    FILE *fname;
    char fn[strlen(cfile) + 18];
    sprintf(fn, "./CountingFolder/%s", cfile);
    printf("Debug: %s\n", fn);
    fname = fopen(fn, "r");
    if (fname == NULL) {
        error("ERROR: Opening file");
    }
    while ((i = fgetc(fname)) != EOF){
        if (i == '\n') {
            lcount++;
        }
        if (i == '\t' || i == ' '){
            wcount++;
        }
        ccount++;
    }
    printf("Threadid %d processes %s which has %d characters, %d words and %d lines\n", tinfo, cfile, ccount, wcount, lcount);
    fclose(fname);
    return 0;
}

2 个答案:

答案 0 :(得分:3)

1)可能是拼写错误。但是

int CountWord(int tinfo, char cfile){ .. }

应该是

int CountWord(int tinfo, char *cfile){ .. }

2)您将同一buf传递给main()的所有主题。数据竞争和未定义的行为。

3)snprintf()个调用都没有size个参数。未定义的行为。

4)由于所有线程都处理不同的数据,因此您根本不需要锁定。

5)你没有分配td数组。所以你不能在线程函数中调用free(data);。未定义的行为。

代码可能存在更多问题,但可能是因为(3)或(5)而导致的段错误。

答案 1 :(得分:0)

在你的代码中

snprintf(buf, "temp%d.txt", tnum);
printf("debug tnum and array: %d and %s\n",tnum, buf);
td[t].id = tnum;
td[t].file = buf;

最后一行将指针指向此结构的file字段

typedef struct thread {
    int id;
    char file;
}ThreadData;

不应该是char *file;吗?我在MSVC中没有thread.h所以我无法编译它。你肯定已经启用了所有警告吗?