wozzinator
March 8th, 2008, 08:47 AM
I'm writing a prime number checking program just to play around with simulatenous thread processing and i'll get a Seg Fault (core dump) error randomly. I apologize for the indenting. C+P for some reason won't read the tab indentation.
My code is:
/* Prime Number Checker
* 3/5/2008
* A Just-For-Fun program that will implement threads to
* have core0 check if "i" is prime and core1 will check
* if "i+2" is prime. */
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <stdbool.h>
void *Prime(int x);
bool isPrime(int x);
int main(void)
{
long int Start = clock();
int x=1;
pthread_t pth0=0,pth1=0;
printf("The prime number check is starting at %d and ending at 2147483647\n", x);
for (x=1; x > 0; x+=4)
{
pthread_create(&pth0, NULL, Prime(x), NULL);
pthread_create(&pth1, NULL, Prime(x+2), NULL);
}
long int Stop = clock();
long int TotalTime = Stop - Start;
printf("The time it took to check every number between 1 and 2147483647 was %.2f seconds.\n", (double) TotalTime/1000000);
}
void *Prime(int x)
{
long int begin = clock();
if (isPrime(x) == true)
{
long int end = clock();
long int TimePerCheck = end - begin;
printf("%10d is a prime number and the check took %.2f seconds\n", x, (double)TimePerCheck/1000000);
}
return NULL;
}
bool isPrime(int x)
{
for (int i = 2; i < (x/2); i++)
for (int j = i; j < (x/2); j++)
if ((i * j) == x)
return false;
return true;
}
I am compiling with gcc using the flags:
gcc -std=c99 -lpthread -W -O2 threaded-prime.c
Sample output:
The prime number check is starting at 1 and ending at 2147483647
1 is a prime number and the check took 0.00 seconds
3 is a prime number and the check took 0.00 seconds
5 is a prime number and the check took 0.00 seconds
7 is a prime number and the check took 0.00 seconds
...
241 is a prime number and the check took 0.00 seconds
251 is a prime number and the check took 0.00 seconds
257 is a prime number and the check took 0.00 seconds
Segmentation fault (core dumped)
Sometimes it will get to around 900 other times anywhere between 3 and 500.
I've browsed around for pthreads on the forums and i've gotten from it not compiling to having it get this far, so any help would be appreciated.
My code is:
/* Prime Number Checker
* 3/5/2008
* A Just-For-Fun program that will implement threads to
* have core0 check if "i" is prime and core1 will check
* if "i+2" is prime. */
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <stdbool.h>
void *Prime(int x);
bool isPrime(int x);
int main(void)
{
long int Start = clock();
int x=1;
pthread_t pth0=0,pth1=0;
printf("The prime number check is starting at %d and ending at 2147483647\n", x);
for (x=1; x > 0; x+=4)
{
pthread_create(&pth0, NULL, Prime(x), NULL);
pthread_create(&pth1, NULL, Prime(x+2), NULL);
}
long int Stop = clock();
long int TotalTime = Stop - Start;
printf("The time it took to check every number between 1 and 2147483647 was %.2f seconds.\n", (double) TotalTime/1000000);
}
void *Prime(int x)
{
long int begin = clock();
if (isPrime(x) == true)
{
long int end = clock();
long int TimePerCheck = end - begin;
printf("%10d is a prime number and the check took %.2f seconds\n", x, (double)TimePerCheck/1000000);
}
return NULL;
}
bool isPrime(int x)
{
for (int i = 2; i < (x/2); i++)
for (int j = i; j < (x/2); j++)
if ((i * j) == x)
return false;
return true;
}
I am compiling with gcc using the flags:
gcc -std=c99 -lpthread -W -O2 threaded-prime.c
Sample output:
The prime number check is starting at 1 and ending at 2147483647
1 is a prime number and the check took 0.00 seconds
3 is a prime number and the check took 0.00 seconds
5 is a prime number and the check took 0.00 seconds
7 is a prime number and the check took 0.00 seconds
...
241 is a prime number and the check took 0.00 seconds
251 is a prime number and the check took 0.00 seconds
257 is a prime number and the check took 0.00 seconds
Segmentation fault (core dumped)
Sometimes it will get to around 900 other times anywhere between 3 and 500.
I've browsed around for pthreads on the forums and i've gotten from it not compiling to having it get this far, so any help would be appreciated.