C - 多线程WordCount运行时崩溃 - 现在编译失败:重新定义struct timespec?

时间:2016-03-11 09:52:00

标签: c multithreading pthreads mutex word-count

我正在为课程开发一个多线程字数统计程序而且我已经完成了大部分工作但是,尽管成功编译了警告标签,我的程序立即崩溃。因此,基本上程序应该采用给定的txt文档(这只是书中的文本)并将其拆分为16个线程,这些线程分别对单词进行计数,总数在main中求和。 这是我的代码:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

pthread_mutex_t mutex;
//pthread_cond_t cond;


typedef struct thread_data
{
    int input;
    int offset;
    int size;
    int wordCount;
} thread_data;

void *counter(void *arg)
{
    thread_data *data = (thread_data *)arg;
    char *buffer = malloc(data->size);
    int i;
    int count = 0;
    int isChar = 0;

    pthread_mutex_lock(&mutex);

    lseek(data->input, data->offset, SEEK_SET);
    read(data->input, buffer, data->size);

    for (i = 0; i < data->size; i++)
    {
        if (buffer[i] == ' ' || buffer[i] == '\t' || buffer[i] =='\n')
        {
        isChar = 0;
        }
        else
        {
            if (isChar == 0)
            {
                count++;
            }
            isChar = 1;
        }
    }
    data->wordCount = count;
    free(buffer);
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);

    return NULL;
}

int main()
{
    int id;
    int segSize;
    int total;
    //int fileSize;
    pthread_t *threads;
    int input;

    input = open("Assign3.txt", O_RDONLY); //open file

    segSize = lseek(input,0,SEEK_SET)/16;  //Getting segment size for  partition    

    struct thread_data data[16];

    threads = malloc(16*sizeof(pthread_t));

    for(id =0; id < 16; id++)  //Partitioning of file to threads
    {
        data[id].input = input;
        data[id].offset = id*segSize;
    }

    pthread_mutex_init(&mutex, 0);

    for(id = 0; id < 16; id++)  //create threads
    {
        pthread_create(&threads[id], NULL, &counter, &data[id]);
    }
    for(id = 0; id < 16; id++)
    {
        pthread_join(threads[id], NULL);
    }

    for(id = 0; id < 16; id++)  //compute total from threads
    {
        total += data[id].wordCount;
    }

    printf("There are %i words in this document.\n", total);

    //cleanup
    pthread_mutex_destroy(&mutex);  
    close(input);

    return 0;
}

以前,这会编译,运行时会崩溃。我不认为我改变了什么,但是现在当我用MinGW和命令编译它时(第一个是我的教授将如何编译代码)

gcc -Wall -Werror -lpthread Assign3_MichaelMorro.c
//or
gcc -Wall -Werror Assign3_MichaelMorro.c

我收到以下错误:

In file included from Assign3_MichaelMorro.c:3:0:
c:\mingw\include\pthread.h:320:8: error: redefinition of 'struct timespec'
 struct timespec {
        ^
In file included from c:\mingw\include\time.h:53:0,
                 from c:\mingw\include\pthread.h:219,
                 from Assign3_MichaelMorro.c:3:
c:\mingw\include\parts\time.h:105:8: note: originally defined here
 struct timespec
        ^

我该如何处理? 是否有可能在具有原生gcc的linux计算机上执行此操作?如果有人正在使用Linux并且可以尝试为我编译,我会很感激。

1 个答案:

答案 0 :(得分:1)

编译代码时,我会收到警告。对我来说,这些警告似乎非常严重。

$  gcc -Wall a.c -o a -lpthread
a.c: In function 'counter':
a.c:28: warning: cast from pointer to integer of different size
a.c:29: warning: cast from pointer to integer of different size
a.c: In function 'main':
a.c:65: warning: cast from pointer to integer of different size

当我看到这些线条时,我发现您正在向FILE*投射int

这是完全错误的。

您应该将lseek函数调用替换为适用于FILE*的函数,例如fseek

或者,如果您更喜欢使用lseek / read等,则应使用open打开文件,该文件将返回文件描述符(类型int)。

如果由于某种原因,您不想切换到open(),您可以从FILE* fp获取文件描述符,如下所示:

int file = fileno(fp);