Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an a

ID: 3845122 • Letter: W

Question

Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number P_1 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), thus, 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 dont 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 P_1 (http://en.wikipedia.org/wiki/Pi): P_1 = 3 + 4f(2*3*4) -4f(4*5*6) + 4f(6*7*8) - 4f(8*9*10) +. .. + k*4f((2*i+ 1)*(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 NT For instance, ./pie 100 4 This command computes the approximation with 101 terms (starting with term 3) with 4 child processes. Here is a description of the algorithm to be implemented. The parent process iterates with an index variable of type int called j from 0 to T(0 lessthanorequalto j

Explanation / Answer

The command line in Linux is so incredibly powerful with so many functions available to you via the OS and optional applications you can install. It is often the easiest solution in C++ programs to just use the command line to achieve something you need doing rather than finding a harder way to do it in code and the following examples let you do just that.


#include <stdlib.h>
   //Console command:
   system("cp file.x newfile.x");

   //Execute file:
   execlp("/usr/bin/omxplayer", " ", "/home/pi/projects/game/audio/alpha.wav", NULL);       //Execute file: file, arguments (1 or more strings followed by NULL - omxplayer has [OPTIONS] [FILE] hence the blank string)

System Function (Calling shell)

#include <stdlib.h>


   system("cp file.x newfile.x");
Call On A Background Thread
Can you get away with just adding a '&' to the end of the line to cause it to be done in the background? If not:


#include <unistd.h>
   int pid;
   pid=fork();
   if(pid==0)
   {
       //printf("I am the child ");
       execlp("/usr/bin/omxplayer", " ", "/home/pi/projects/game/audio/alpha.wav", NULL);       //Execute file: file, arguments (1 or more strings followed by NULL - omxplayer has [OPTIONS] [FILE] hence the blank string)
       _exit(0);
   }
   else
   {
       //printf("I am the parent ");
       wait();
   }
Running as a different root user
If your application has been run as root user with sudo because you are using the IO pins you may want to make command lines calls as the standard pi user. You can change to a different user using su – USERNAME -c before the command and surrounding it with quotes.


   system("su - pi -c "fetchmail > /dev/null"");
Getting The Result Of An Executed Console Command

#include <string>
#include <iostream>
#include <stdio.h>
using namespace std; //Or use std::string;

//************************************************************
//************************************************************
//********** EXECUTE CONSOLE COMMAND AND GET RESULT **********
//************************************************************
//************************************************************
//Note, this will return the output stdout. Some commands may generate a stderr response instead of stdout which you'd see on the console but this won't
//return, unless you simply add " 2>&1" to the end of your command string. Then you'll get the output of stdout and stderr
string do_console_command_get_result (char* command)
{
   FILE* pipe = popen(command, "r");       //Send the command, popen exits immediately
   if (!pipe)
       return "ERROR";
  
   char buffer[128];
   string result = "";
   while(!feof(pipe))                       //Wait for the output resulting from the command
   {
       if(fgets(buffer, 128, pipe) != NULL)
           result += buffer;
   }
   pclose(pipe);
   return(result);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote