从pthread运行pthreads

时间:2013-08-22 17:06:26

标签: c pthreads

我有一个文件,其中包含一个文件名列表,我想在其中搜索一个单词并替换它 我稍微修改了一下代码,只是为了显示相关部分 问题是,如果我在该列表中只有一个文件,它将不会使用多线程处理它,因为只有当我有多个文件时线程才能正常工作 所以我想保留当前的线程配置,但我想在处理部分添加一些线程 我有这段代码:

struct words_list {
    char word[20];
    struct words_list * next;
};
FILE * INFILE;
int num_thread = 10;
// Mutex variables
pthread_mutex_t input_queue;
pthread_mutex_t word_list;


int main(int argc,char **argv)
{
    //some code is missing

    if((INFILE = fopen(myfile,"r")) == NULL) {
        fprintf(stderr,"Can't open input file\n");
        exit(0);
    }

    for(i = 0 ; i < number_thread; i++)
    {
        if(pthread_create(&thread_id[i],NULL,&search,NULL) != 0)
        {
            i--;
            fprintf(stderr,RED "\nError in creating thread\n" NONE);
        }
    }
    for(i = 0 ; i < number_thread; i++)
        if(pthread_join(thread_id[i],NULL) != 0)
        {
            fprintf(stderr,RED "\nError in joining thread\n" NONE);
        }

    fflush(INFILE);
    fclose(INFILE);
}

void * search(void * data)
{
    char file[20];
    while (!feof(INFILE))
    {
        if (fgets(file,sizeof(file),INFILE) != NULL)
        {
            if (strlen(file) < 8)
                break;
            if (file[strlen (file) - 1] == '\n')
                file[strlen (file) - 1] = '\0';
        }
        process(file);
    }
    return NULL;
}


void process(char *filename)
{
    char buff[512];
    char word[20];
    struct words_list * curr_word = first_word;

    if(verbose != 0)
        fprintf(stderr,"Processing: %s\n",filename);
    while(curr_word != NULL)
    {
        //some code missing
        pthread_mutex_lock(&word_list);
        strncpy(word,curr_word->word,sizeof(word) - 1);
        pthread_mutex_unlock(&word_list);


            **//replace_word must run with multiple threads**

        ret =  replace_word(word,buff,sizeof(buff));

            //end of threads part



        //code missing
    }
}

如何在粗体​​部分添加其他pthread,以便每个文件可以处理多个线程?

1 个答案:

答案 0 :(得分:1)

而不是为每个文件分配一个线程。为什么不分配线程来处理每个文件的部分。如下所示,我为文件中的每50个字节的数据分配了一个线程。在这种技术中,即使列表中只有一个文件,您仍然可以使用多个线程来解析它。希望能帮助到你。

#include "stdio.h"
#include "pthread.h"
#include "sys/stat.h"
#include "string.h"
#include "fcntl.h"

#define TOTAL_NUMBER_THREADS 100

struct words_list {
    char word[20];
    struct words_list * next;
};

struct file_segment {
    char filename[50];
    size_t foffset;
    size_t size;
}fs[TOTAL_NUMBER_THREADS];

FILE * INFILE;
int num_thread=0;

// Mutex variables
pthread_mutex_t input_queue;
pthread_mutex_t word_list;
pthread_t thread_id[TOTAL_NUMBER_THREADS];

void *process( void *arg);

void segment_file(char *filename)
{
    int fd;
    int offset=0;
    struct stat statbuf;
    size_t size;

    fd = open(filename, O_RDONLY);
    if(fd < 0)
    {
        perror("fopen");
        return;
    }

    fstat(fd, &statbuf);
    size=statbuf.st_size;

    while((offset < size) && (num_thread <= 100))
    {
      strncpy(fs[num_thread].filename, filename, sizeof(fs[num_thread].filename));
      fs[num_thread].foffset=offset;
      fs[num_thread].size=(size>50)?50:size;
      offset+=fs[num_thread].size;

      if(pthread_create(&thread_id[num_thread],NULL,&process,&fs[num_thread]) != 0)
      {
        fprintf(stderr,"\nError in creating thread\n");
      }
      num_thread++;
    }

    return;
}

void *process( void *arg)
{
   char buf[50];
   struct file_segment *fs;
   char word[20];
   //struct words_list * curr_word = first_word;
   fs = (struct file_segment *) arg;
   FILE *fp;

   fp=fopen(fs->filename, "r");
   fseek(fp, fs->foffset, SEEK_SET);
   fread(buf,1,fs->size,fp);

    while(curr_word != NULL)
    {
        //some code missing
        pthread_mutex_lock(&word_list);
        strncpy(word,curr_word->word,sizeof(word) - 1);
        pthread_mutex_unlock(&word_list);


            **//replace_word must run with multiple threads**

        ret =  replace_word(word,buff,sizeof(buff));

            //end of threads part
       //code missing
    }

    //printf("Filename: %s\n Info: %s\n", fs->filename, buf);
    printf("%s", buf);

   return;
}

int main(int argc,char **argv)
{
    //some code is missing
    char file[50];
    int i;

    if((INFILE = fopen("list.txt","r")) == NULL) {
        fprintf(stderr,"Can't open input file\n");
        return 0;    }

    while (!feof(INFILE))
    {
        if (fgets(file,sizeof(file),INFILE) != NULL)
        {
            if (strlen(file) < 8)
                break;
            if (file[strlen (file) - 1] == '\n')
                file[strlen (file) - 1] = '\0';
        }
        segment_file(file);
    }

    for(i = 0 ; i < num_thread; i++)
        if(pthread_join(thread_id[i],NULL) != 0)
        {
            fprintf(stderr,"\nError in joining thread\n");
        }

    fflush(INFILE);
    fclose(INFILE);
}