gpt4 book ai didi

c - 多线程共享文件的读访问: pthreads

转载 作者:行者123 更新时间:2023-11-30 15:30:45 25 4
gpt4 key购买 nike

我必须实现一个应用程序,其中用户通过命令行传递多个单词,并且应用程序查找文件每行中的单词计数。每个单词都会在自己的线程中搜索文件。

到目前为止,我已将其实现为单线程应用程序。

代码如下:

 //Below function reads file line and returns it
char* readLine(FILE* file, char* line)
{
if (file == NULL) {
printf("Error: file pointer is null.");
exit(1);
}

int maximumLineLength = 128;
char *lineBuffer = (char *) malloc(sizeof(char) * maximumLineLength);
if (lineBuffer == NULL) {
printf("Error allocating memory for line buffer.");
exit(1);
}


char ch = getc(file);//Get each character
int count = 0;
//loop for line or EOF
while ((ch != '\n') && (ch != EOF))
{
if (count == maximumLineLength)
{
maximumLineLength += 128;
lineBuffer = realloc(lineBuffer, maximumLineLength);
if (lineBuffer == NULL)
{
printf("Error reallocating space for line buffer.");
exit(1);
}
}

lineBuffer[count] = ch;
count++;
ch = getc(file);
}

lineBuffer[count] = '\0';//Add null character

line = (char *) malloc(sizeof(char) * (count + 1));
strncpy(line, lineBuffer, (count + 1));
free(lineBuffer);
return line;
}



//Below function finds the occurance of
//word in the line
//Need to refine to take into consideration
//scenarios such that {"Am"," am "," am","?Am",".Am"}etc
int findWord(char* line,char* word)
{
int count=0;

int lineLen = strlen(line);
int wordLen = strlen(word);

char* temp= (char *) malloc(sizeof(char) * (lineLen+1));
strcpy(temp,line);

while(true)
{
if( strstr(temp,word) == NULL)
break;
strcpy(temp, strstr(temp,word));
// printf("@@%s\n",temp);
strcpy(temp,temp+wordLen+1);
// printf("##%s\n",temp);
count++;
}
//printf("%d\n",count);
free(temp);
return count;
}

//Below function fills the linked list for data structure lineCount
//with word occurance statistics
//line by line and the total
//The number of elements in the list would be number of lines in the
//file
LineCount* findCount(FILE* file, char* word,LineCount** lineCountHead)//Make it multithreaded fn()
{
LineCount* lineHead= NULL;

char* line = NULL;
int lineNumber=1;
int count=0;


if (file == NULL) {
printf("Error: file pointer is null.");
exit(1);
}

while (!feof(file)) {
LineCount* temp=NULL;
line = readLine(file, line);
//printf("%s\n", line);
count=findWord(line,word);

//Critical Section Start
temp=LineCountNode(lineNumber,count);
addToLineCountList(temp,lineCountHead);
//Criticla Section End


lineNumber++;
}


free(line);
return lineHead;
}

所以基本上我希望我的调用线程函数是 LineCount* findCount(FILE* file, char* word,LineCount** lineCountHead)

我的理解是,文件将被访问 - 仅用于线程的读取目的,因此无需考虑同步。

目前我打开文件的方式为:pFile = fopen(argv[1],"r");。我的问题是如何以读取共享模式打开?我知道在 C++ 中存在读取共享模式。如何在c中实现这一点?

另外,如何以线程调用函数所需的形式编写我的函数 LineCount* findCount(FILE* file, char* word,LineCount** lineCountHead) ,即形式 void* fn(void*)

最佳答案

虽然在只读模式下文件本身没有问题,但标准 C 库中的 IO 函数并未设计为可从多个线程并行使用。它们是线程安全的(或者至少我是这样认为的),但是从多个线程正确使用它们并不是微不足道的。

在最低层,每个FILE结构都包含一个文件位置指针——或者IO函数维护一个操作系统提供的指针。让多个线程扰乱文件光标位置听起来是一个让你的生活变得更加困难的好方法。

最好的方法是多次打开文件 - 每个线程一次。每个线程都会有自己的 FILE 指针、流缓冲区等。请注意,这并不是 C 和 POSIX 线程所独有的 - 这是使用多个线程的固有问题。

无论如何,我不确定您想通过使用多线程来实现什么目的。一般来说,像这样的搜索操作是 I/O 绑定(bind)的 - 对同一文件的多线程访问很可能会让事情变得更糟。

唯一可能有意义的情况是,如果您有大量字符串需要搜索,并且您有一个单个 I/O 线程为所有其他线程提供数据通过公共(public)缓冲区。这将分配 CPU 密集型部分,而不会导致不当的 I/O...

关于c - 多线程共享文件的读访问: pthreads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410968/

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