簡體   English   中英

從pthread運行pthread

[英]run pthreads from pthread

我有一個文件,其中包含一個文件名列表,我想在其中搜索一個單詞並將其替換。我對代碼進行了一些修改,僅在此處僅顯示相關部分。問題是,如果該列表中只有一個文件,則它將我無法使用多線程來處理它,因為只有在我有多個文件的情況下線程才可以工作,所以我想保留當前的線程配置,但是我想在處理部分添加一些線程,我有以下代碼:

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,以便每個文件可以處理多個線程?

而不是為每個文件分配一個線程。 為什么不分配線程來處理每個文件的一部分。 如下所示,我為文件中每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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM