C语言中的多线程字数计算

C语言中的多线程字数计算,c,linux,multithreading,C,Linux,Multithreading,我知道我说过我会尝试自己去解决这个问题,我真的做到了,然后我在这里再次发帖之前先找了其他地方,但后来我就陷入了这样的困境: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> void partition_file(int n); v

我知道我说过我会尝试自己去解决这个问题,我真的做到了,然后我在这里再次发帖之前先找了其他地方,但后来我就陷入了这样的困境:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

void partition_file(int n);
void *count_words(void *pos);

int total_count = 0;
int *seg_size = 0;

int main()
{
    int file=0;
    pthread_t tid;
    if((file=open("Device-Driver.txt",O_RDONLY)) < -1)
                return 1;
    partition_file(8);

    for (int i=0; i < 8; i++)
    {
        pthread_create(&tid, NULL, count_words, (void *) seg_size);
    }
    pthread_exit(NULL);
    return 0;

}

void partition_file(int n)
{
    int file=0;
    file=open("Device-Driver.txt",O_RDONLY);
    int size = lseek(file, 0, SEEK_END);
    seg_size = size / n);
    close(file);
}

void *count_words(void *pos)
{
    int file=0;
    int p = *((int *) pos);
    char buffer[seg_size];
    file=open("Device-Driver.txt",O_RDONLY);
    lseek(file,p,SEEK_SET); 
    read(file,buffer,seg_size);     
    for(int i = 0; i < size; i++)
    {
        if(buffer[i] == " ") total_count +=1;
    }
}

#包括
#包括
#包括
#包括
#包括
#包括
无效分区_文件(int n);
void*count_字(void*pos);
整数总计数=0;
int*seg_size=0;
int main()
{
int file=0;
pthread_t tid;
如果((file=open(“Device Driver.txt”,orduonly))<-1)
返回1;
分区文件(8);
对于(int i=0;i<8;i++)
{
pthread_create(&tid,NULL,count_words,(void*)seg_size);
}
pthread_exit(NULL);
返回0;
}
无效分区_文件(int n)
{
int file=0;
文件=打开(“Device Driver.txt”,仅限ordu);
int size=lseek(文件,0,搜索结束);
seg_尺寸=尺寸/n);
关闭(文件);
}
void*count_单词(void*pos)
{
int file=0;
int p=*((int*)位置);
字符缓冲区[seg_size];
文件=打开(“Device Driver.txt”,仅限ordu);
lseek(文件、p、SEEK_集);
读取(文件、缓冲区、seg_大小);
对于(int i=0;i
我该如何解决这个问题?具体而言,这些错误和警告:

warning: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   36 |  seg_size = size / n);
      |           ^
error: expected ‘;’ before ‘)’ token
   36 |  seg_size = size / n);
      |                     ^
      |                     ;
error: expected statement before ‘)’ token
In function ‘count_words’:
error: size of array ‘buffer’ has non-integer type
   44 |  char buffer[seg_size];
      |       ^~~~~~
warning: passing argument 3 of ‘read’ makes integer from pointer without a cast [-Wint-conversion]
   47 |  read(file,buffer,seg_size);
      |                   ^~~~~~~~
      |                   |
      |                   int *
In file included
/usr/include/unistd.h:360:52: note: expected ‘size_t’ {aka ‘long unsigned int’} but argument is of type ‘int *’
  360 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur;
      |                                             ~~~~~~~^~~~~~~~
error: ‘size’ undeclared (first use in this function)
   48 |  for(int i = 0; i < size; i++)
      |                     ^~~~
warning: comparison between pointer and integer
   50 |   if(buffer[i] == " ") total_count +=1;
      |                ^~
警告:从“int”赋值到“int*”将从整数生成指针,无需强制转换[-Wint转换]
36 | seg_尺寸=尺寸/n);
|           ^
错误:应为“;”前“)”标记
36 | seg_尺寸=尺寸/n);
|                     ^
|                     ;
错误:在“')标记之前应该有语句
在函数“count_words”中:
错误:数组“缓冲区”的大小具有非整数类型
44 |字符缓冲区[分段大小];
|       ^~~~~~
警告:传递'read'的参数3会从指针生成整数,而无需强制转换[-Wint转换]
47 |读取(文件、缓冲区、seg|U大小);
|                   ^~~~~~~~
|                   |
|int*
包括在文件中
/usr/include/unistd.h:360:52:注意:应为'size_t'{aka'long unsigned int'},但参数的类型为'int*'
360 |外部数据读取(整数fd、空*字节、大小字节);
|                                             ~~~~~~~^~~~~~~~
错误:“大小”未声明(首次在此函数中使用)
48 |用于(int i=0;i

我认为从缓冲区计算单词的部分是可以的,但是我不确定线程要使用的函数应该如何设置,参数应该如何传递给线程。它需要非常特定的变量类型,但我需要使用其他类型,就像我说的那样,它只是变成了一团乱麻

lseek只移动读取指针。
将读取指针放置在一个位置后,可以使用read读取字符。


我认为在您的练习中,每个线程都必须有自己的读取指针。

如果文件为800字节并分为8:
线程1读取从0到99的字节,
lseek(0,寻道集),读取(文件,缓冲区,100), 计算缓冲区中的字数。。。

线程2: lseek(100,寻道集),读取(文件,缓冲区,100), 计算缓冲区中的字数。。。

线程3: lseek(200,寻道集),读取(文件,缓冲区,100) 计算缓冲区中的字数。。。




您不必创建临时文件

运行此代码时是否遇到任何错误消息或异常?如果是这样的话,在你的问题中发布这些内容也会很有帮助,这将帮助人们更好地回答你的问题。首先,如果你的段是基于字节计数的,那么你可能有一个单词横跨两个段。第二,您可能不想创建新文件,而是让每个线程调用
open()
,获取自己的文件描述符,并处理自己的段。缺少第36行开括号请参见4.5.3中的参数管理(in和out)。从线程返回值和第二条建议:以只读模式打开和关闭每个线程中的文件->每个线程将有一个独立的“句柄文件”。因此,要为每个线程传递的参数将是struct{(in)filename,(in)startPosition,(in)lenToRead,(out as result)words counted},这比创建128个临时文件更有意义,但是如何使用lseek来计算单词数呢?在缓冲区中计算单词数。。。。在读取(文件,缓冲器,100)之后,缓冲区包含字符,您必须编写一些代码来迭代每个字符,检查字符是否是一个空间(或逗号,或者您认为是单词分隔符所需的),并在需要时增加一个计数器字。注意:我为你担心,因为如果在缓冲区中计算字数是非常基本的,那么管理线程就要复杂得多……我应该试着自己至少解决一些问题,尽管如果我真的不能,我可能会回来问这个问题。但是如果我不回来,非常感谢你的帮助!