Question

*Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

*Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.*

--The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.*

--This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the entire computation could reach a speedup close to T.

--Numbers N and T are passed from the shell to the pie program as command line parameters. The main(argc, argv[]) function call gets these parameters (T, N) in argv[1] and argv[2] as strings, respectively. (Element argv[0] contains the program name and we don’t need it.) The first parameter, N, is the upper limit for i in the formula above. The second parameter, T, is the number of child processes that compute partial sums. N should always be greater than T, N>T. Otherwise the program should display an error message and call exit(1).

Use the Nilakantha approximation formula for π: π = 3 + 4/(2*3*4) – 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + …. + k*4/((2*i)* (2*i+1)*( 2*i+2))+... where k = 1 if i is odd and k = -1 if i is even and i goes from 1 to N. The program can be run like this from the shell: ./pie N T .... For instance, ./pie 100 4 This command computes the approximation with 101 terms (starting with term 3) with 4 child processes.

--The parent process iterates with an index variable of type int called j from 0 to T (0≤j<T). During iteration with index j, the parent process ... 1. creates a pipe associated with child process with index j, 2. creates a child process with fork(), 3. writes the binary values of N, T, and j to the pipe

--After ending the loop (above) that created the T child processes, the parent process starts a new loop, with index j from 0 to T (0≤j<T). In iteration with index j, the parent process reads from the pipe associated with child j the value of the partial sum computed by the child and adds it to an accumulator double variable.

--After the second loop ends, the parent process: 1. displays the sum approximating the value of π stored in the accumulator, with a message that reads like: The approximation of pi with N=%d and T=%d processes is %f. (this is a C printf format string, make sure the code displays the actual numbers!) 2. waits for all child processes to end, 3. exits with code 0.

--The child process with index j runs its code in a function called computePartialSum. In this function the child process does this: 1. closes any unnecessary file descriptors (which ones?), 2. reads the values for N,T, and j from the pipe that were written by the parent process, 3. computes the partial sum of the series (as described below), 4. writes the partial sum to the pipe, 5. calls exit(0).

--The child process should NOT rely on global variables to access N, T, and j. These values must be read from the parent process using a pipe. Doing otherwise is considered a mistake.

--For example, if the user runs command ./pie 100 4 then: child process #0 computes the partial sum for i going from 1 to 25, child process #1 computes the partial sum for i going from 26 to 50, child process #2 computes the partial sum for i going from 51 to 75, child process #3 computes the partial sum for i going from 76 to 100

1: A child process with index j (with 0≤j<T) will compute a partial sum for i going from N*j/T+1 to (N/T)*(j+1). Note that * and / are used on ints.

2: Check for errors when making library such as, fork(), pipe(), etc.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

include Cïostheam > Cout Input nitea Cout ce Elementy er

Add a comment
Know the answer?
Add Answer to:
*Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...

    Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum...

  • Write a program using the fork() system call to do thefollowing. The parent process (main program)...

    Write a program using the fork() system call to do thefollowing. The parent process (main program) forks a process(CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. Have the parent invoke the wait () call to wait for both the child processesto complete before exiting the program. IN A C program

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • Modify the template program below and include the following additional functionalities: - Instead of 3 children...

    Modify the template program below and include the following additional functionalities: - Instead of 3 children processes, create 6 children processes. - 6 children processes are connected to the parent process via 6 pipes, one for each pair, child1-parent, child2-parent, child3-parent, .... - Child keeps writing to the pipe with 1 second apart between consecutive writes (as in child() in p. 167). - Keyboard inputs need to be monitored as in the original code. - Parent keeps reading from the...

  • Using either a UNIX or a Linux system, write a C program that forks a child...

    Using either a UNIX or a Linux system, write a C program that forks a child process that ultimately becomes a zombie process. Process states can be obtained from the command: ps -l The process states are shown below the S column; processes with a state of Z are zombies. The process identifier (pid) of the child process is listed in the PID column, and that of the parent is listed in the PPID column. Because you do not want...

  • What to do Write a C program that computes Pi with the approximation algorithm that I...

    What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT