Call a thread and call new program primeThreads.c

Viewed 77

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);
}
1 Answers

the following proposed code:

  1. cleanly compiles
  2. uses size_t for variables that will never be < 0
  3. limits the scope of variable i to the enclosing for() statement
  4. allows the compiler to determine the number of entries in the number[] table
  5. eliminated the 'magic' number 10
  6. eliminated the delay from usleep( 1000 )
  7. eliminated the unused header file: unistd.h
  8. changed the pthread_t tl to an array so easy to use pthread_join() so multiple threads can exist at same time
  9. inserted appropriate horizontal spacing for readability
  10. Note: an indent width of 2 can be 'lost' when using variable width fonts, so changed all indent width to 4 spaces
  11. Note: adding threads to a 'memory bound' program will NOT make the program faster.

and now, the proposed code:

#include <stdio.h>
#include <math.h>
#include <pthread.h>

void *prime( void* );

int main( void ) 
{
    long int number[] = {5011*5009, 1293961, 1293967, 5021*4241, 4723*5009, 
                         5011*5021, 1299173, 1160807, 4241*4723, 1161829};

    pthread_t t1[ sizeof(number)/sizeof(number[0]) ];

    for ( size_t i = 0; i < (sizeof(number)/sizeof(number[0])); i++) 
    {
        pthread_create( &t1[i], NULL, prime, (void*)(number+i) );  
    } 

    for( size_t i = 0; i < (sizeof(number)/sizeof(number[0])); i++ )
    {
        pthread_join( t1[i], NULL ); 
    }
    return(0);
}


void *prime( void *n ) 
{
    size_t num = *((size_t *)n);
    size_t max = (size_t) sqrt((double)num);
  
    for ( size_t i = 2; i <= max; i++ ) 
    {
        if ( num % i == 0 ) 
        {
            printf( "%ld is not prime\n", num );
            pthread_exit( NULL );
        }
    }

    printf( "%ld is prime\n", num );    
    pthread_exit( NULL );
}

a run of the proposed code results in:

25100099 is not prime
1293961 is prime
1293967 is prime
21294061 is not prime
1299173 is prime
25160231 is not prime
20030243 is not prime
1160807 is prime
23657507 is not prime
1161829 is prime
Related