Question

In: Computer Science

Can someone fill in the blanks? using namespace std; double approxPi; // define the signal handler...

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;
}

Solutions

Expert Solution

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;
}

Related Solutions

Can someone make this code work without using template? C++ using namespace std; template < typename...
Can someone make this code work without using template? C++ using namespace std; template < typename T>    void print_array(T arr[], int size)    {        ofstream outfile;        outfile.open("/Users/android/Desktop/outfile.txt");        cout << "Printing Array: " << endl;        for (int i = 0; i < size; i++)        {            cout << arr[i] << endl;            outfile << arr[i] << endl;        }    } template < typename T>...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;    double parellel;    cout << "how many resistors: ";    cin >> number_resistors;    while (number_resistors != 0)    {        cout << "Enter the values of" << number_resistors << " resistors ";        for (int i = 0; i < number_resistors; i++) {            cin >> Resistors;            series += Resistors;                parellel...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };   //Challenge #2: print the list from given range   printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6   cout << endl;   //Challenge #3: print the list, but backwards   printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8   cout << endl;                  //Challenge #4: reverse order of items in list   reverse(nums,...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" << endl; cout << "Today we have a deal on e-books. Customers will receive a discount off their entire order.." << endl; cout << "The discount is 15% off your total order and cost per book is 8.99." << endl; cout << "Customers who buy 20 or more e-books will receive 20% off instead." << endl; cout << endl; return 0; } int no_of_books() {...
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct...
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct Node {     int data;     struct Node* next; }; void printMiddle(struct Node *head) {     struct Node *slow_ptr = head;     struct Node *fast_ptr = head;     if (head!=NULL)     {         while (fast_ptr != NULL && fast_ptr->next != NULL)         {             fast_ptr = fast_ptr->next->next;             slow_ptr = slow_ptr->next;         }         printf("The middle element is [%d]\n\n", slow_ptr->data);     } } void...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate...
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate memory for at least 3 double elements //ensure all elements have been initialized //call function, replace 0 with the proper number of elements pointed to by dd ddAns = lookUpFirstNeg(dd, 0); if(ddAns == NULL) cout << "No negatives found.\n"; else cout << "First negative value is:" << ddAns << endl; //properly deallocate memory allocated above return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT