Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 148 additions & 37 deletions assignment2/ex02/task2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,171 @@
#include <pthread.h>
#include <math.h>

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int NPRIMES=100;
/* Define a data type for the arguments to pass to the functions of threads. */
struct thread_arg_t
{
/* thread id */
unsigned int id;

/* starting index */
unsigned int startindex;

/* upper index */
unsigned int upperlimit;

/* calclauted primes */
int* primes;


};

/* Returns the index of the next prime (non-zero, since we "knock out" non-primes by setting their value to zero) */
int getNextPrime(int i, int x[]) {
while (x[i] == 0) {
i++;
}
return i;
int getNextPrime(int i, int *x)
{
printf("inside getNextPrime");
pthread_mutex_lock(&lock);
while (*(x + i) == 0)
{
i=i+1;
}
pthread_mutex_unlock(&lock);

return i;
}

void displayPrimeNumbers(int primes[],int NPRIMES)
void displayPrimeNumbers(int *primes,int NPRIMES)
{
for(int i=2; i < NPRIMES; i++) {
if ( primes[i] != 0 ) {
printf("Prime number: %d\n", primes[i]);
printf("display all primes[] \n");

for(int i=2; i < NPRIMES; i++)
{
if ( *(primes +i) != 0 )
{
printf("Prime number: %d\n", *(primes +i));
}
}
}
}

int *init(int primes[],int NPRIMES)
int *init(int *primes,int NPRIMES)
{
/* we start at 2 because it is the smallest prime */
/*list all numbers from 2 to max */
for(int i=2; i < NPRIMES; i++) {
primes[i] = i;
printf("%d\n", i);
}
return primes;
/* we start at 2 because it is the smallest prime */
/*list all numbers from 2 to max */
for(int i=2; i < NPRIMES; i++)
{
*(primes +i) = i;

}
printf("init of primes[] \n");
return primes;
}

int *filterPrimes(int startingindex,int primes[],int maxlimit){
/*make iterations from 2 to NPRIMES and update counter with next prime number*/
for(int i=startingindex; i < maxlimit; i = getNextPrime(i+1, primes)) {
/*find multiple i.e j=i*2 and j=j+i and marked that number by setting array[index]=0 */
for(int j = (i*2); j < maxlimit; j += i) {
printf("i: %d, j: %d\n", i, j);
primes[j] = 0;
//void *filterPrimes(int startingindex,int *primes,int maxlimit)
//{
// printf("inside filterPrimes()");
// /*make iterations from 2 to NPRIMES and update counter with next prime number*/
// for(int i=startingindex; i <= maxlimit; i = getNextPrime(i+1, primes))
// {
// /*find multiple i.e j=i*2 and j=j+i and marked that number by setting array[index]=0 */
// for(int j = (i*i); j < maxlimit; j += i)
// {
//// printf("i: %d, j: %d\n", i, j);
// if(j%i==0)
// {
// *(primes +j) = 0;
// *(primes +i) = 0;
//
// }
//
// }
//
// }
// printf("exited filterPrimes()");
// return NULL;
//}

void *filterPrimes(int startingindex,int *primes,int maxlimit)
{
/*make iterations from 2 to NPRIMES and update counter with next prime number*/
startingindex=getNextPrime(startingindex, primes);
for(int i=startingindex; i <= maxlimit; i = getNextPrime(i+1, primes))
{
/*find multiple i.e j=i*2 and j=j+i and marked that number by setting array[index]=0 */
for(int j = (i*2); j <= NPRIMES; j += i)
{

pthread_mutex_lock(&lock);
*(primes +j) = 0;
pthread_mutex_unlock(&lock);

}

}
return NULL;
}

void *calculate_prime_pthread_runnable(void *arg)
{
printf("inside runnable()");
/* Cast the argument from void* to its proper type. */
struct thread_arg_t *targ = arg;
/* Add index to seed to make sure that different threads get different
* seeds even if they are created so close in time that time() returns
* the same value. */
unsigned int startingindex = targ->startindex;
unsigned int upperlimit = targ->upperlimit;

filterPrimes(startingindex,targ->primes,upperlimit);

}
return primes;
printf("exited runnable()");
return NULL;
}

int main(int argc, char* argv[]) {
int* primes;
int NPRIMES;
int main(int argc, char* argv[])
{
int* primes,prime1;
int NUM_THREADS=2;
NPRIMES = 100;
pthread_t threads[NUM_THREADS]; /* An array of the threads. */
struct thread_arg_t args[NUM_THREADS]; /* One argument struct per thread. */
primes = (int*)malloc(NPRIMES * sizeof(int));
unsigned int sqrt_num = (int) ceil(sqrt((double) NPRIMES));

primes=init(primes,NPRIMES);
filterPrimes(2,primes,sqrt_num);

filterPrimes(sqrt_num+1,primes,NPRIMES);
/* Create (and run) all the threads. */
for (int i = sqrt_num+1,j=0; j < NUM_THREADS; j++)
{
if(i>=NPRIMES)
{
break;
}
args[j].startindex = i;
args[j].upperlimit=NPRIMES;
args[j].primes=primes;
if (pthread_create(&threads[j], NULL,calculate_prime_pthread_runnable, &args[j]))
{
fprintf(stderr, "Error creating thread #%d!\n", j);
exit(1);
}
i=i+1;

}

/* Block until all threads are finished. */
for (int i = 0; i < NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}

NPRIMES = 24;
primes = (int*)malloc(NPRIMES * sizeof(int));
unsigned int sqrt_num = (int) ceil(sqrt((double) NPRIMES));
sleep();
primes=args[0].primes;

primes=init(primes,NPRIMES);
primes=filterPrimes(2,primes,sqrt_num);
displayPrimeNumbers(primes,NPRIMES);
displayPrimeNumbers(primes,NPRIMES);

return 0;

return 0;
}