DescriptionFor this first assignment we’re going to play with POSIX threads in order to do some mathematical calculating. First, for my benefit as well as yours, let’s define the natural log of a...

1 answer below »
DescriptionFor this first assignment we’re going to play with POSIX threads in order to do some mathematical calculating. First, for my benefit as well as yours, let’s define the natural log of a number1:ln(x) = (x - 1) - (x - 1)2 / 2 + (x - 1)3 / 3 - (x - 1)4 / 4 + (x - 1)5 / 5 - (x - 1)6 / 6 + ....If the value of “x” is in the interval (0,2) (in other words 0 We want to calculate these with threads. Write a C/C++ program that will take three command line arguments. The first of these is the floating-point number “x” above, which will be in the interval (0,2). The second is the number of threads to start. The third parameter is the number of iterations per thread. For example:./prog1 1.5 4 3Will calculate ln(1.5) using four threads. Each thread will calculate in three terms. Here’s how the work would be broken down in this case:Thread number:Calculates And then… And then…1 +(x-1) +(x-1)5/5 +(x-1)9/9 2 -(x-1)2/2 -(x-1)6/6 -(x-1)10/10 3 +(x-1)3/3 +(x-1)7/7 +(x-1)11/11 4 -(x-1)4/4 -(x-1)8/8 -(x-1)12/12Thread “N” is responsible for (x-1)N/N along with the appropriate sign, and will loop through the correct number of terms, adding the total number of threads (in this example, 4) to N for each term.Do all of the floating point with double precision; don’t use plain old “float”.1 See also https://betterexplained.com/articles/demystifying-the-natural-logarithm-ln/ which is an easy read.As each thread calculates the next term, it is necessary to update one global variable with the next calculation. For example, thread 3 calculates (x-1)7/7 and then adds this number into the global variable. As we will describe in class, it is necessary to use thread locking to “protect” this global from being accessed simultaneously.Print the answer with 14 digits of precision on the right of the decimal. Also print, with 14 digits on the right, the result according to the “log” function in the math library. (The actual library function is named “log” and not “ln”.) That’s ALL that I want for output, just those two numbers, one per line. For example:./prog1 1.5 4 3 0.40545869196992 0.40546510810816Note that the last few digits may not match – that’s fine, as long as it’s close.Requirements Summary: Use command line arguments:  I will test your program with varying command line parameters The program must compile and run on Linux (Loki, Ubuntu, etc…) with no warnings or errors. Be written in C or C++. Print out calculated answer and answer using log() with 14 digits of precision. Use pthreads. Start all the threads first – then use pthread_join() to wait for each thread’s completion. Inside each thread update a global variable and use locking mechanism to protect global variableI run it hereMy calculated answerMath function answerWhat to Hand InI want two files, your C or C++ source code file and a working “Makefile” named (as you might guess) “Makefile”. If you are not familiar with what this is, we can talk about it briefly in class.Please set up your “Makefile” so that the following options are used. Any warnings will cause me to take off points. If you are using C++ it will be “g++” and not “gcc”.gcc -Wall -std=c1x prog1.c -o prog1 -lpthreadRemember that when using POSIX threads, you need to do two things:1. You need to #include 2. You need to add –lpthread (lower case L followed by pthread) on the command line to “gcc” or “g++”. This brings in the correct library.And to use the “log” function you will need #include When to Hand InThis program will be due at 11:30 PM on February 17, 2020.How to Hand InUpload the source code and the Makefile to Canvas.
Answered Same DayFeb 07, 2021

Answer To: DescriptionFor this first assignment we’re going to play with POSIX threads in order to do some...

Ankit answered on Feb 11 2021
146 Votes
// **** For Makefile: make argument to remove executable file
#include
#includ
e
#include // log()
#include // atoi(), atof()
#include // setprecision()
using namespace std;
static long double Result=0;
static int sign=0;
//--------------calculation of log
pthread_mutex_t lock_unlock = PTHREAD_MUTEX_INITIALIZER;
void* cal_natlog (void* term_pt) {
    void *val=0;
long double number=*(long double *) term_pt;
long double temp_res;
    pthread_mutex_lock(&lock_unlock);
sign++;
    temp_res = (pow(number-1,sign)/sign);
    if (sign == 1) {
Result...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here