Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective: The aim of this assignment is to help students understand: (1) how to write multithreaded programs and consolidate the concepts learned in class [Chapter-4);

image text in transcribedimage text in transcribedimage text in transcribed

Objective: The aim of this assignment is to help students understand: (1) how to write multithreaded programs and consolidate the concepts learned in class [Chapter-4); and (ii) the main concepts of multi- threaded programming with synchronization Chapter-5]. Students will obtain hands-on experience in working with POSIX PThreads. Tasks: 1. Program-1. An interesting way of calculating T is to use a technique known as Monte Carlo, which involves randomization. This technique works as follows. Suppose you have a circle inscribed within a square, as shown in the figure (assume that the radius of this circle is 1; thus we have a square of size 2x2). First, generate a series of points as simple (x, y) coordinates. These points must fall within the Cartesian coordinates that bound the square. Of the total number of random points that are generated, some will occur within the circle. Next, estimate r by performing the following calculation: 1=4x (number of points in circle) / (total number of points) Write a multi-threaded version of this algorithm that creates a separate thread (the slave- thread) to generate a number of random points. The slave-thread will count the number of points that occur within the circle (the hit_count) and store that result in the global variable circle_count. When the slave-thread has exited, the parent thread (the master-thread) will calculate and output the estimated value of n. It is worth experimenting with the number of random points generated. As a general rule, the greater the number of random points, the closer the approximation of t. Note: Program-1 contains only 2 threads; a master-thread and its single slave-thread. Below, is the code for generating random numbers, as well as the code for determining if the random (x, y) point occurs within the circle /* Generates a double precision random number */ double random_double() 1 return random() / ((double) RAND_MAX + 1); 1 /* seed the random number generator */ srandom((unsigned) time(NULL)); /*generate random numbers between -1.0 and +1.0 (exclusive) */ /* to obtain a random (x, y) point*/ x = random_double() * 2.0 - 1.0; y = random_double() * 2.0 - 1.0; /* is (x, y) point within the circle ? */ if (sqrt (x*x + y*y)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions

Question

What is meant by a Sharpe-optimal portfolio?

Answered: 1 week ago