gpt4 book ai didi

c - 从 pthread 运行 pthreads

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:10 28 4
gpt4 key购买 nike

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

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);
}

关于c - 从 pthread 运行 pthreads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18386737/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com