Cześć. Napisałem program który ma obliczać liczbę pi. Przy niektórych argumentach program działa jak należy (np. 10 oraz 3), jednak przy innych wyskakuje błąd (np. 10 oraz 4) - "naruszenie ochrony pamięci (zrzut pamięci)".
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
double pi = 1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* piSum(void* arg) {
char *args = (char*)arg ;
char* p = strtok(args, " ");
char* array[3];
for (int i = 0; p != NULL; i++)
{
array[i] = p;
p = strtok(NULL, " ");
}
unsigned long thread = atoi(array[0]);
unsigned long w = atoi(array[1]);
unsigned long zakres = atoi(array[2]);
unsigned long zakresN = atoi(array[3]);
pthread_t self = pthread_self();
unsigned long first = 1 + ((thread-1) * zakres);
double iloczyn = 1;
if (thread == 1) {
fprintf(stdout, "thread:%lxd\tsize=%ld\tfirst=%d\n", self, zakres, 1);
for (unsigned long index = 1; index <= zakres; index++) {
double nawias = 2 * index;
iloczyn *= (nawias * nawias) / ((nawias - 1) * (nawias + 1));
}
}
else if (thread < w) {
fprintf(stdout, "thread:%lxd\tsize=%ld\tfirst=%ld\n", self, zakres, first);
for (unsigned long index = first ; index < first + zakres; index++) {
double nawias = 2 * index;
iloczyn *= (nawias * nawias) / ((nawias - 1) * (nawias + 1));
}
}
else if(thread == w){
fprintf(stdout, "thread:%lxd\tsize=%ld\tfirst=%ld\n", self, zakresN, first);
for (unsigned long index = first; index < first + zakresN; index++) {
double nawias = 2 * index;
iloczyn *= (nawias * nawias) / ((nawias - 1) * (nawias + 1));
}
}
fprintf(stdout, "thread:%lxd\tprod=%.20f\n", self, iloczyn);
pthread_mutex_lock(&mutex);
pi = pi * iloczyn;
pthread_mutex_unlock(&mutex);
pthread_exit(0);
}
int main(int argc, char* argv[]){
unsigned long n = atoi(argv[1]);
unsigned long w = atoi(argv[2]);
int boolFlag = 0;
if(n <= 1 || n >= 1000000000){
fprintf(stderr, "n musi spelniac warunek 1<n<1000000000\n");
boolFlag = 1;
}
if(w <= 1 || w >= 100){
fprintf(stderr, "w musi spelniac warunek 1<w<100\n");
boolFlag = 1;
}
if(boolFlag) return 0;
unsigned long reszta = n % w;
unsigned long zakres = 0;
unsigned long zakresN = 0;
if (reszta) {
zakres = (n - reszta) / w;
zakresN = zakres + reszta;
}
else {
zakres = n / w;
zakresN = zakres;
}
pthread_t threads[w];
char data[w][20];
clock_t t1 = clock();
for (unsigned long i = 1; i <= w; i++)
{
sprintf(data[i], "%ld %ld %ld %ld", i, w, zakres, zakresN);
pthread_create(&threads[i], NULL, piSum, data[i]);
}
for (int i = 1; i <= w; i++) {
pthread_join(threads[i], NULL);
}
clock_t t2 = clock();
double time_s = ((double)t2 - (double)t1) / CLOCKS_PER_SEC;
printf("w /threads pi = %.20f\ttime=%f\n", pi*2, time_s);
pi = 1;
t1 = clock();
for (int i = 1; i <= n; i++){
double nawias = 2 * i;
pi *= (nawias * nawias) / ((nawias - 1) * (nawias + 1));
}
t2 = clock();
time_s = time_s = ((double)t2 - (double)t1) / CLOCKS_PER_SEC;
printf("wo/threads pi = %.20f\ttime=%f\n", pi*2, time_s);
return 0;
}
O co może chodzić? Pierwszy raz spotykam się z takim błędem.