In: Computer Science
Can someone fill in the blanks?
using namespace std;
double approxPi;
// define the signal handler function
______
______
______
______
______
int main(int argc, char* argv[])
{
pid_t childPid;
double inside = 0;
double total = 0;
double oldPi = 0.0;
switch (childPid = fork()) { // fork the process
case -1:
cerr << "forking error";
exit(0);
case 0: // child process
// register SIGUSR1 handler
______
______
______
while (1) {
// calculate the approximate value of pi using the Monte Carlo
method
for (int i = 0; i < 10000; ++i) {
double x = (double)rand() / RAND_MAX;
double y = (double)rand() / RAND_MAX;
if (x * x + y * y <= 1) {
inside++;
}
total++;
}
approxPi = 4.0 * inside / total;
}
default: // parent process
sleep(5);
______; // send the SIGUSR1 signal to the child
}
return 0;
}
fork is simlar to concept of recusion. it is use to create new process know as child process
-ve indicte no child create
0 indicate new child indicate
+ve indicate return to function calling
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <iostream>
int main(int argc, char* argv[])
{
pid_t childPid;
double inside = 0;
double total = 0;
double oldPi = 0.0;
switch (childPid = fork()) { // fork the process
case -1:
cerr << "forking error";
exit(0);
case 0: // child process
// register SIGUSR1 handler
signal(SIGUSR1,readUsual); while (1) { // calculate the approximate value of pi using the Monte Carlo method for (int i = 0; i < 10000; ++i) { double x = (double)rand() / RAND_MAX; double y = (double)rand() / RAND_MAX; if (x * x + y * y <= 1) { inside++; } total++; } approxPi = 4.0 * inside / total; } default: // parent process sleep(5); break;// send the SIGUSR1 signal to the child } return 0; }