So I have a program prime.c below and I'm supposed to speed it up by making each prime call a thread and call the new program primeThreads.c as such:
> gcc –o prime prime.c –lm
> time ./prime
>gcc –o primeThreads primeThreads.c –lm –lpthread
>time ./primeThread
I'm just a little confused as to how I would do this? Am I supposed to create the threads in primeThreads.c and then call it? Also how would I call this file from prime.c? I tried creating the threads in prime.c below and
/tmp/cc8Lo90g.o: In function `main':
prime.c:(.text+0xa7): undefined reference to `pthread_create'
prime.c:(.text+0xb8): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
prime.c
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <pthread.h>
void *prime(void*);
int main()
{
int i;
pthread_t t1;
long int number[10] = {5011*5009, 1293961, 1293967, 5021*4241, 4723*5009,
5011*5021, 1299173, 1160807, 4241*4723, 1161829};
for (i = 0; i < 10; i++) {
prime(number+i);
pthread_create(&t1, NULL, prime, " ");
pthread_join(t1, NULL);
}
return(0);
}
void *prime(void *n) {
int i;
long int num = *((long int *)n);
int max = (int) sqrt(num);
for (i = 2; i <= max; i++) {
usleep(1000);
if (num % i == 0) {
printf("%ld is not prime\n", num);
return NULL;
}
}
printf("%ld is prime\n", num);
}