In: Computer Science
Write a C or C++ program that uses pthreads to compute the
number of values that are
evenly divisible by 97 between a specified range of values
(INCLUSIVE).
The program should accept 3 command-line arguments:
low value
high value
number of threads to create to perform the computation
-Specifics for program
order of input: 0 9000000000 2 this means 0 to 9 Billion (INCLUSIVE); 2 threads
alarm(90); this should be the first executable line of the program to make sure it does not run for too long
use long ints instead of ints
hard-code the use of 97 in computations, instead of into a variable (const may be ok)
time1 please copy function below to get the time
sample output from the above run:
THREAD 0: 46391753
THREAD 1: 46391753
TOTAL 92783506
TIME 7.425558
time1 function
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
double time1()
{
struct timeval tv;
gettimeofday( &tv, ( struct timezone * ) 0 );
return ( (double) (tv.tv_sec + (tv.tv_usec / 1000000.0)) );
}
double time2()
{
return ( (double) time(NULL) );
}
void main(int argc,char *argv[])
{
double t1, t2, t3, t4;
t1 = time1();
t2 = time2();
printf("%lf %lf\n",t1,t2);
sleep(2);
t3 = time1();
t4 = time2();
printf("%lf %lf\n",t3,t4);
printf("%lf %lf\n",t3-t1,t4-t2);
}
Here the complete code, due to time limit I am unable to add an explanation, but the code is running fine :
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
void* division();
const int divider = 97 ;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t* cond = NULL;
int thread_count;
long int start;
long int end;
int count;
int* arr;
int *arr2;
volatile int cnt = 0;
double time1()
{
struct timeval tv;
gettimeofday( &tv, ( struct timezone * ) 0 );
return ( (double) (tv.tv_sec + (tv.tv_usec / 1000000.0)) );
}
double time2()
{
return ( (double) time(NULL) );
}
int main(int argc, char *argv[])
{
double t1, t2, t3, t4;
t1 = time1();
t2 = time2();
pthread_t* tid;
if(argc != 4)
{
printf("Invalid Command Line Argument! \n Existing");
return 0;
}
start = atoi(argv[1]);
end = atoi(argv[2]);
thread_count = atoi(argv[3]);
cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t) *
thread_count);
tid = (pthread_t*)malloc(sizeof(pthread_t) * thread_count);
arr = (int*)malloc(sizeof(int) * thread_count);
arr2 = (int*)malloc(sizeof(int) * thread_count);
for(int i=0; i<thread_count; i++)
{
arr[i] = i;
pthread_create(&tid[i], NULL, division,
(void*)&arr[i]);
}
for(int i=0; i<thread_count; i++)
{
pthread_join(tid[i], NULL);
}
long int sum = 0;
for(int i=0; i<thread_count; i++)
{
printf("Thread %d : %d\n", i, arr2[i]);
sum += arr2[i];
}
printf("TOTAL : %ld\n", sum);
t3 = time1();
t4 = time2();
printf("TIME : %lf %lf\n",t3-t1,t4-t2);
return 0;
}
void* division(void *p)
{
int turn = *(int*)p;
while(end > 97)
{
pthread_mutex_lock(&mutex);
if (turn != cnt) {
pthread_cond_wait(&cond[turn], &mutex);
}
end = end - divider ;
arr2[turn] += 1;
if (cnt < thread_count - 1) {
cnt++;
}
else {
cnt = 0;
}
pthread_cond_signal(&cond[cnt]);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output :