使用线程的单词总和

时间:2016-05-08 17:48:58

标签: c multithreading

问题:实现一个程序,该程序将文件名作为参数后跟单词。对于每个单词,创建一个单独的线程,在给定的文件中计算其外观。打印出所有单词的外观总和。

下面我做了一个代码,但是当我运行它时,我收到了:Segmentaion Fault(核心转储)。

PS:我不知道代码是否正确并且成功地完成了要求

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

pthread_mutex_t mtx;

int sum = 0;
char filename[10];
char word[10];

void * voidCount(void* p){
    char cmd[100], appearences[100];
    FILE *f;
    sprintf(cmd, "echo %s | grep -o %s | wc -l", filename, word);
    f = popen(cmd, "r");
    fgets(appearences, sizeof(int), f);

    pthread_mutex_lock(&mtx);
    sum += *((int*)appearences);
    pthread_mutex_unlock(&mtx);
    return NULL;
}
int main(int argc, char *argv[]){

    pthread_mutex_init(&mtx, NULL);
    pthread_t threads[argc-1];
    int i = 0;
    for ( i = 1; i < argc-1; i++){
        strcpy(filename, argv[1]);
        strcpy(word, argv[i]);

        pthread_create(&threads[i], NULL, voidCount, NULL);
    }
    for (i = 0; i < argc-1; ++i){
        pthread_join(threads[i], NULL);
    }
    printf("Total appearences: %d", sum);
    pthread_mutex_destroy(&mtx);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

程序必须完成一些任务才能解决问题

  • 根据要搜索的字数设置线程数
  • 正确分配和初始化线程参数
  • 将每个单词作为单独的线程参数传递
  • 在计算期间保护共享变量
  • 获得结果

这是对代码的最小功能演示返工,几乎没有必要的注释。单词搜索区分大小写。

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

pthread_mutex_t mtx;
int sum = 0;
char filename[10]; //declared global for simplicity
                   //we search all the words in the same file
void * voidCount(void* p){
    char  *word, cmd[100], appearences[100];
    int    num;
    FILE  *f;

    word =  ((char*)p);
    printf ("word : %s\n",word);    
    sprintf(cmd, "more %s | grep -o %s | wc -l", filename, word);//echo didn't work
    printf ("%s\n",cmd);    //debug print
    pthread_mutex_lock(&mtx);    
    f = popen(cmd, "r");

    if(fgets(appearences, sizeof(appearences), f) == NULL)
        printf("Pipe error\n");     //better error handling is welcome...
    printf("Appearances of \"%s\": %s\n", word, appearences);

    num = atoi(appearances);    //convert string to int
    sum += num;    
    pclose(f);                  //close pipe
    pthread_mutex_unlock(&mtx);
    return NULL;
}


int main(int argc, char *argv[])
{
    pthread_mutex_init(&mtx, NULL);
    pthread_t threads[argc-1];    
    char **data = calloc(argc,sizeof(char*)); // initialize thread data
    int i = 0;
    strcpy(filename, argv[1]);    
    for ( i = 2; i < argc; i++){
        data[i-2] = calloc(33, sizeof(char));//let's allow for somewhat longer words
        strcpy(data[i-2], argv[i]);
        strcat(data[i-2], "\0");
        pthread_create(&threads[i-2], NULL, voidCount, data[i-2]);//each word a thread arg
    }
    for (i = 0; i < argc-1; ++i){;
        pthread_join(threads[i], NULL);
    }
    printf("Total appearences: %d\n", sum);
    pthread_mutex_destroy(&mtx);

    for (i = 0; i < argc; i++)//free thread data
        free (data[i]);    
    free (data);
    return 0;
}

希望这可以提供帮助。