【发布时间】:2021-09-06 09:55:38
【问题描述】:
我有一个用 C 语言为我正在上课的课程编写的程序。目标是获取一个数组并使用 10 个不同的线程计算某个字符串的所有实例。在这种情况下,我们试图计算文件中有多少个“是”。应该是 55。
我当前的逻辑是将数组拆分为每个单词,然后单独处理每个单词,并包含一个测试以查看是否应该在当前线程或另一个线程上处理它。
任何帮助将不胜感激。我的导师提供了所有代码,除了: num_substring、allowedOnThread 和计数器。这些都是我做的。
我对 C 很陌生。
我目前有:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 10240
#define NUM_THREADS 10
int n1,n2;
char *s1,*s2;
FILE *fp;
int countArray[NUM_THREADS]={0};
int total = 0;
//read input file and generate string s1/s2 and length n1/n2
int readf(FILE *fp)
{
if((fp=fopen("strings.txt", "r"))==NULL){
printf("ERROR: can't open string.txt!\n");
return 0;
}
s1=(char *)malloc(sizeof(char)*MAX);
if(s1==NULL){
printf("ERROR: Out of memory!\n");
return -1;
}
s2=(char *)malloc(sizeof(char)*MAX);
if(s1==NULL){
printf("ERROR: Out of memory\n");
return -1;
}
/*read s1 s2 from the file*/
s1=fgets(s1, MAX, fp);
s2=fgets(s2, MAX, fp);
n1=strlen(s1); /*length of s1*/
n2=strlen(s2)-1; /*length of s2*/
if(s1==NULL || s2==NULL || n1<n2) /*when error exit*/
return -1;
return 0;
}
int num_substring(int t) {
//add your logic here
//1, how to distribute different parts of string s1 into different threads
//2, how to sum up the total number of substring from all threads
char *str = s1; // This is what we will need to process for the string
char *token = strtok(str, " ");
int count = 0;
int index = 0;
while(token != NULL) {
// Determine if it should be on the thread
if(allowedOnThread(t, index) == 1){
count = count + counter(token);
}else{
return count;
}
index++;
token = strtok(NULL, " ");
}
return count;
}
int allowedOnThread(int thread, int index) {
int threadMultiplier = n1 / NUM_THREADS;
// Check range
int min = thread * threadMultiplier;
int max = (thread * threadMultiplier) + threadMultiplier;
if(thread >= 1){
min = (thread * threadMultiplier) + 1;
}
if(thread + 1 == NUM_THREADS){
max = n1 + 1;
}
if(min <= index && index <= max) {
return 1;
}
return 0;
}
int counter(char *str){
int i,j,k;
int count;
int complete = 0;
for (i = 0; i <= (n1-n2); i++){
count=0;
for(j = i,k = 0; k < n2; j++,k++){ /*search for the next string of size of n2*/
if (*(str+j)!=*(s2+k)){
break;
}else{
count++;
}
if(count==n2){
complete++;
}
}
}
total = total + complete;
return complete;
}
void *calSubStringThread(void *threadid){
long tid = (long)threadid;
printf("This is thread %ld, ", tid);
int num = num_substring(tid);
printf("find num of is: %d\n", num);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int t, rc;
readf(fp);
for(t=0; t<NUM_THREADS; t++){
rc = pthread_create(&threads[t], NULL, calSubStringThread, (void *) (size_t)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t=0; t<NUM_THREADS; t++){
pthread_join(threads[t], NULL);
}
printf("The number of substrings is: %d\n", total);
return 1;
}
输入文件:
Thss is an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. This is an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss ss. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That is a kiwi fruit. This is an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ssss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. That is cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. That ss a haw. Thss ss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ssss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. That is a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. This is a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree This is a banana. This is a berry. This is cherry. This is a haw. Thss is a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. This is a banana. This is a berry. This is cherry. This is a haw. This is a lemon. There ss a hickory on the tree. Thss ss an apple. That ss a pear. That ss an orange. That ss a kiwi fruit. Thss ss an avocado. There ss a peach on the tree. Thss ss a banana. That ss a berry. That ss cherry. That ss a haw. Thss ss a lemon. There ss a hickory on the
is
在此先感谢您。我已经为此工作了好几个小时。
【问题讨论】:
-
我下载并运行了你的程序。我不得不将
counter和allowedOnThread移到num_substring上方,以防止函数的隐式定义。我一共得到了 55 个,但都是在一个线程(线程 2)中完成的。所有其他人报告为 0。您是否有定义/使用allowedOnThread和counter或者它们只是您创建的子功能以促进num_substring?我猜你只需要填写num_substring并且可以做任何你想做的事情。因为你在线程之间分配工作的方式有点奇怪。 -
如果允许你重构代码,有更好的方法来拆分工作。你的教授的代码(
readf)有点像新手。您想保留.以便(例如)lemon.与lemon不同,还是可以将.视为分隔符[如空格]?您是否需要保留大小写以便(例如)This与this不同? -
写完最后一条评论后,我才意识到
s2是要在s1中搜索的字符串,并且您想计算s2在s1中出现的次数。对吗? -
您是否需要将
calSubStringThread与just 一起使用tid参数?我要做的是让主线程为每个线程计算一个位置/长度,并将其在单独的结构实例中传递给每个线程。 -
请注意,您不能在竞争线程中使用
strtok——它不是线程安全的。你应该改用strtok_r。
标签: arrays c linux pthreads distributed-computing