gpt4 book ai didi

C - 多线程计数字母频率导致内存错误

转载 作者:行者123 更新时间:2023-11-30 16:27:07 28 4
gpt4 key购买 nike

我正在尝试使用 C 多线程来找出文本文件中每个字母的频率。作业是:1)编写一个函数,读取文本中以“.”结尾的每个句子。 2) 编写一个函数,在二维数组中加载一个句子 3) 编写一个函数,为每个句子的每个字母生成一个 pthread(pthread 函数将该字母的计数器加 1)。编辑:我通过 Valgrind 发现问题出在 sentence 函数中,因为我不明白为什么。

代码如下:

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

char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
int count[26];

char* sentence(char * s){
char* p;
char* q;
char* arr;
int i;
p = s;
q = malloc(100);
arr = q;
for (i=0; *p != '.'; i++){
*q = *p;
q++;
p++;
}
*q = '\0';
return arr;
}

char** load_sentence(char* p, char** q, int i){
q[i] = malloc(strlen(p)+1);
strcpy(q[i], p);
return q;
}

void* count_letter(void * s){
char* p = (char*) s;
int i;
for (i=0; i<26; i++){
if (*p == alphabet[i]){
count[i]++;
}
}
}

void frequency(char* str){
char* s = str;
int i, j, l;
l = strlen(str);
pthread_t tid[l];
for (i=0; i<l; i++){
pthread_create(&tid[i], NULL, count_letter, (void*) s);
s++;
}
for (j=0; j<l; j++){
pthread_join(tid[j], NULL);
}
}


int main(int argc, char* argv[]){

int fd;
char buff[100];
fd = open(argv[1], O_RDONLY);
char ** text = malloc(10*sizeof(char*));
read(fd, buff, sizeof(buff));
char* start = buff;
int i = 0; //number of phrases!
char* p = NULL;

while (*(p = sentence(start)) != '\0'){
text = load_sentence(p, text, i);
start += strlen(p)+1;
i++;
}

int j, k;

for (k=0; k<i; k++){
frequency(text[k]);
}

for (j=0; j<26; j++){
printf("%c : %d times\n", alphabet[j], count[j]);
}
}

对于这样的情况看起来是这样的: 希望这是一本好书。再见。输出正确:

a : 2 times
b : 1 times
c : 0 times
d : 2 times
e : 3 times
f : 0 times
g : 3 times
h : 1 times
i : 2 times
j : 0 times
k : 0 times
l : 0 times
m : 0 times
n : 1 times
o : 3 times
p : 1 times
q : 0 times
r : 1 times
s : 1 times
t : 1 times
u : 0 times
v : 0 times
w : 0 times
x : 0 times
y : 1 times
z : 0 times

对于其他人来说,这是一个“内存错误”,以free():无效的下一个大小(正常)开头。该错误有很多行内存映射并以中止结束。

我对 C 很陌生,很抱歉我的经验不足。

在这种情况下是否有必要引入互斥体

最佳答案

根据 reference,您之前使用 mutex 的版本具有未定义的行为,因为您多次初始化互斥体。 :

Attempting to initialize an already initialized mutex results in undefined behavior.

您正在同时访问count,因此您必须使用互斥体来制作线程安全代码。您在 count_letter 中调用了 pthread_mutex_init ,这是不正确的,这个函数是您线程的主体(互斥体的多次初始化而不破坏它会导致 UB),您应该调用 pthread_mutex_init 仅一次,例如作为 main 函数中的第一行:

int main() {
pthread_mutex_init(&mtx,NULL);

返回前添加

 pthread_mutex_destroy(&mtx);

count_letter 函数中的关键部分是行

count[i]++;

您应该按如下方式修改它

pthread_mutex_lock(&mtx);
count[i]++;
pthread_mutex_unlock(&mtx);
<小时/>

现在,回到sentence实现,在与.比较之前,需要检查*p是否没有指向空终止符:

for (i=0; *p && *p != '.'; i++){ 
^^ added

未经测试,\0 != . 返回 true 并且循环继续...

关于C - 多线程计数字母频率导致内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52855450/

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