gpt4 book ai didi

c - 段错误而未更改我的程序

转载 作者:行者123 更新时间:2023-12-03 12:52:03 24 4
gpt4 key购买 nike

我试图编写一个使用线程计算素数的程序,该程序正在运行,但未给出期望的结果(它告诉所有数字都是素数)。今天,我尝试再次运行该程序,即使我没有更改程序,也遇到了段错误。我尝试使用gdb查找发生的时间,并且我认为它在pthread_join函数中发生。

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int n_threads; // number of threads
int* numbers; // array of numbers
int elements_per_thread;

int isPrime(int n){
int i = 0;
for(i=0; i < n; i++)
if(n%i == 0)
return 0;
return 1;
}

void * threadPrime(void * arg){
int* idxp = (int*)arg;
int idx = *idxp;
int start = idx* elements_per_thread;
int finish = (idx+1)*elements_per_thread-1;
int i;
for(i=start; i<finish; i++)
if(!isPrime(i))
numbers[i]=0;
pthread_exit(NULL);
}

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

if (argc != 3) {
printf("usage: %s largest_number number_threads\n", argv[0]);
return 1;
}

int largest_number = atoi(argv[1]); // value of the largest number to test for primality
int n_numbers = largest_number-1; // number of numbers to test
n_threads = atoi(argv[2]);

// create and fill vector of numbers to test
numbers = (int *)malloc(n_numbers*sizeof(int)) ; // allocate a vector for n_numbers integers

int i;
for (i = 2; i <= largest_number; i++)
numbers[i-2] = i;

int* id = (int *)malloc(n_threads*sizeof(int));
// compute primes

pthread_t* thid = (pthread_t *)malloc(n_threads*sizeof(int));
for(i=0;i<n_threads;i++){
id[i] = i;
if(pthread_create(&thid[i],NULL,threadPrime,(void*)(id+i)) != 0){
printf("Erro\n");
exit(0);
}
}

for(i=0;i<n_threads;i++){
if(pthread_join(thid[i],NULL) != 0){
printf("Erro\n");
exit(0);
}
}

// print result
printf("Primes:");
int n_primes = 0;
for (i = 0; i < n_numbers; i++)
if (numbers[i]) {
n_primes++;
printf (" %d", numbers[i]);
}
printf("\nTotal: %d primes\n", n_primes);

return 0;
}

问题解决了。更正下面的代码。
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int n_threads; // number of threads
int* numbers; // array of numbers
int elements_per_thread;

int isPrime(int n){
int i = 0;
for(i=2; i < n; i++)
if(n%i == 0)
return 0;
return 1;
}

void * threadPrime(void * arg){
int* idxp = (int*) arg;
int idx = *idxp;
int start = idx*elements_per_thread;
int finish = (idx+1)*elements_per_thread;
int i;
for(i=start; i<=finish; i++)
if(!isPrime(numbers[i]))
numbers[i]=0;
pthread_exit(NULL);
}

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

if (argc != 3) {
printf("usage: %s largest_number number_threads\n", argv[0]);
return 1;
}

int largest_number = atoi(argv[1]); // value of the largest number to test for primality
int n_numbers = largest_number-1; // number of numbers to test
n_threads = atoi(argv[2]);

// create and fill vector of numbers to test
numbers = (int *)malloc(n_numbers*sizeof(int)) ; // allocate a vector for n_numbers integers

int i;
for (i = 2; i <= largest_number; i++)
numbers[i-2] = i;

int* id;
id = (int *)malloc(n_threads*sizeof(int));

// compute primeselements_per_thread = n_numbers/n_threads;

elements_per_thread = (n_numbers/n_threads)+1;

pthread_t* thid = malloc(n_threads*sizeof(*thid));

for(i=0;i<n_threads;i++){
id[i] = i;
if(pthread_create(&thid[i],NULL,threadPrime,(void*)(id+i)) != 0){
printf("Erro\n");
exit(0);
}
}

for(i=0;i<n_threads;i++){
if(pthread_join(thid[i],NULL) != 0){
printf("Erro\n");
exit(0);
}
}

// print result
printf("Primes:");
int n_primes = 0;
for (i = 0; i < n_numbers; i++)
if (numbers[i]) {
n_primes++;
printf (" %d", numbers[i]);
}
printf("\nTotal: %d primes\n", n_primes);

return 0;
}

最佳答案

存在许多问题:

1)循环的起始索引应为2。否则,您将在这里被零除错误:

for(i=0; i < n; i++) // should be i=2
if(n%i == 0)
return 0;

2)根本没有设置 elements_per_thread。因此它将是0(因为它是一个全局变量),并且永远不会调用线程函数中的循环。在main()中设置它:
elements_per_thread = n_numbers/n_threads;

3)当您调用 isPrime()时,您正在传递 i。但是您确实想传递 numbers[i]。您还希望在素数测试中包括 finish。所以应该是
    for(i=start; i<=finish; i++)
if(!isPrime(numbers[i]))
numbers[i]=0;

4)线程数组分配错误。它应该是
pthread_t* thid = malloc(n_threads * sizeof *thid);

有更多有效的测试素数的方法(例如,您只需要检查最多n/2即可查看素数)。但是,解决上述问题后,您将拥有一个有效的代码,并考虑在以后进行改进。

关于c - 段错误而未更改我的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33083232/

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