Question

In: Computer Science

Write a C or C++ program that uses pthreads to compute the number of values that...

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);

}

Solutions

Expert Solution

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 :


Related Solutions

Program in C++ **********Write a program to compute the number of collisions required in a long...
Program in C++ **********Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing and double hashing. For simplicity, only integers will be hashed and the hash function h(x) = x % D where D is the size of the table (fixed size of 1001). The simulation should continue until the quadratic hashing fails.*********
Using C++ Write a program to compute the number of collisions required in a long random...
Using C++ Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing, and double hashing.
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c that accepts a list of numbers from the user. There are two threads: Main thread waits for the numbers to be entered by the user and for the second thread to finish sorting. It then asks user whether there are more lists or not. It will do this until the user indicates there are no more lists to be entered, at which point it...
Use any C program without using PTHREADS calculate time taken to execute program. Write same program...
Use any C program without using PTHREADS calculate time taken to execute program. Write same program of question 1 using PTHREADS. Calculate time taken to execute program.(1 mark) Identify data races in above program and explain why this situation occurred with an example (1 mark) Rewrite the code to avoid data races should use any of the THREE techniques.(1.5 marks) please I need the c code.. critical section mutex solution semaphore functions Barriers Read-Write Locks Run program using 1, 2,...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and...
Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and condition variables. To implement the program, please followthe hints below. (a) The detailed 'Dining-Philosophers' problem statement: Refer to Page 71 ~ Page 72 in 'Lecture-08.pptx'    (b) The introduction to Pthreads mutex locks and condition variables: Refer to Page 17 ~ Page 39 in'Lecture-08.pptx'    (c) Creating five philosophers, each of which will run as a separate thread. Philosophers alternatebetween thinking and eating: The activities...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
in C++ Write a program to compute the current and the power dissipation in an AC...
in C++ Write a program to compute the current and the power dissipation in an AC circuit that has four resistors R1, R2, R3, and R4 in parallel. The voltage source is V. Test your solution with various voltage levels and resistor values. Execute and submit the program and the results in screen captures. Please note that the equivalent resistor is given by 1/Rtotal = 1/R1 + 1/R2 + 1/R3 + 1/R4 and the current I is given by I...
Write a C program that asks the user to enter double values (the values can be...
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT