-
-
Save zkylab/b1604c7c763518cda8c056fa4c72ccd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
#define NUM_OF_THREAD 10 | |
static int res = 0; | |
int checkIsItPrime(int n) { | |
int i, flag = 0; | |
for (i = 2; i <= n / 2; ++i) { | |
if (n % i == 0) { | |
flag = 1; | |
break; | |
} | |
} | |
if (flag == 0) | |
return 1; | |
else | |
return 0; | |
} | |
void *generateNumbersAndCountPrimes(void *arg) { | |
int k,i; | |
int primeCounter = 0; | |
for (i = 0; i < 1000; i++) { | |
int num = (rand() % (10000 - 1000 + 1)) + 1000; | |
if (checkIsItPrime(num) != 1) | |
continue; | |
else | |
primeCounter++; | |
} | |
res = primeCounter; | |
} | |
int main() { | |
int threadsNumbersStructure[1][1000]; | |
int k, i; | |
int arr = 1000; | |
srand(time(NULL)); | |
for (i = 0; i < NUM_OF_THREAD; i++) { | |
pthread_t thread_id; | |
pthread_create(&thread_id, NULL, generateNumbersAndCountPrimes, (void *) arr ); | |
pthread_join(thread_id, NULL); | |
printf("Thread %ld - ", i); | |
printf("%d primes.\n", res); | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment