1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cmath>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7. #include <iomanip>
  8. #include <sys/resource.h>
  9.  
  10. using namespace std;
  11.  
  12. void findPrimes(int primes[], int numPrimesToFind);
  13. void sumPrimes(int primes[], int numPrimesToFind);
  14.  
  15. int main(int argc, char **argv)
  16. {
  17. int numPrimesToFind = atoi(argv[1]);
  18. int primes[numPrimesToFind];
  19. int stat_val;
  20. struct rusage parent_usage;
  21. struct rusage child_usage;
  22. double usage = 0.0;
  23. pid_t childPID;
  24. pid_t child_pid;
  25.  
  26.  
  27. findPrimes(primes, numPrimesToFind);
  28.  
  29. childPID = fork();
  30.  
  31. if(childPID == -1)
  32. {
  33. cout << "No children were forked in the making of this program :(" << endl;
  34. exit(1);
  35. }
  36.  
  37. if(childPID == 0)
  38. {
  39. cout << "=======================Child========================\n";
  40. sumPrimes(primes, numPrimesToFind);
  41. getrusage(RUSAGE_SELF, &child_usage);
  42. usage = child_usage.ru_utime.tv_sec + child_usage.ru_stime.tv_sec; //) / pow(10.0, 9);
  43. cout << "Process ID: " << getpid() << " ..... " << " CPU time " << fixed << showpoint << setprecision(6) << usage << " s\n\n";
  44. cout << "Ending child process\n\n";
  45. cout << "====================================================\n\n";
  46. }
  47. else
  48. {
  49.  
  50. child_pid = wait(0);
  51.  
  52. cout << "==========================Parent=========================\n\n";
  53. getrusage(RUSAGE_CHILDREN, &child_usage);
  54. usage = child_usage.ru_utime.tv_sec + child_usage.ru_stime.tv_sec; //) / pow(10.0, 9);
  55. cout << "Child PID = " << childPID << " Child CPU time = " << fixed << showpoint << setprecision(6) << usage << " s\n";
  56. getrusage(RUSAGE_SELF, &parent_usage);
  57. usage = parent_usage.ru_utime.tv_sec + parent_usage.ru_stime.tv_sec; //) / pow(10.0, 9);
  58. cout << "Parent PID = " << getpid() << " Parent CPU time = " << fixed << showpoint << setprecision(6) << usage << " s\n";
  59.  
  60. cout << endl << endl << endl << "List of first " << numPrimesToFind << " prime numbers:\n";
  61. /*
  62.   for(int index = 0; index < numPrimesToFind; index++)
  63.   {
  64.   cout << primes[index] << endl;
  65.   }
  66.   */
  67. cout << "=============================================================\n\n";
  68.  
  69. }
  70.  
  71. return 0;
  72. }
  73.  
  74. void findPrimes(int primes[], int numPrimesToFind)
  75. {
  76. int divider = 2;
  77. int number = 2;
  78. int index = 0;
  79. bool prime = true;
  80.  
  81. /*loop through to find the first numPrimesToFind prime numbers*/
  82. while(index < numPrimesToFind)
  83. {
  84.  
  85. /*A composite integer will have a prime number as a factor that does
  86.   not exceed the square root of the composite integer. If this prime
  87.   is not found, then the number itself must be prime*/
  88.  
  89. while (divider <= sqrt(number) && prime)
  90. {
  91. /*if number divides evenly into divider, then it is not prime*/
  92. if(number % divider == 0)
  93. prime = false;
  94.  
  95. divider++;
  96. }
  97.  
  98. if(prime)
  99. {
  100. primes[index] = number;
  101. index++;
  102. }
  103.  
  104. prime = true;
  105. divider = 2;
  106. number++;
  107.  
  108. }
  109. }
  110.  
  111. void sumPrimes(int primes[], int numPrimesToFind)
  112. {
  113. int sum = 0;
  114.  
  115. for(int index = 0; index < numPrimesToFind; index++)
  116. sum += primes[index];
  117.  
  118. cout << endl << "The sum of the first " << numPrimesToFind << " prime numbers is: " << sum << endl;
  119. }
  120.