Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include <sys/resource.h>
  6.  
  7. #define BILLION 1E9
  8.  
  9.  
  10. /* for simplicity the # of procs is defined here,
  11. But a better idea is to read it from the command line */
  12. #define PROCESSES 4
  13. main()
  14. {
  15. int i, pid;
  16.  
  17. /* Take initial time here */
  18. /* Use clock_gettime(). Do NOT use clock() */
  19.  
  20. struct timespec requestStart, requestEnd;
  21.  
  22. clock_gettime(CLOCK_REALTIME, &requestStart);
  23.  
  24. for(i=0; i<PROCESSES; i++)
  25. {
  26. pid = fork();
  27. if(pid < 0) /* some error occurred – fork failed */
  28. {
  29. printf("Error");
  30. exit(-1);
  31. }
  32. if(pid == 0) /* child process code */
  33. {
  34. do_work(i, i+1, 20000, PROCESSES);
  35. exit(0);
  36. }
  37. /* do not place any wait() call here */
  38. }
  39.  
  40. /* wait for all processes to finish their execution */
  41. for(i=0; i<PROCESSES; i++)
  42. {
  43. wait(NULL);
  44. }
  45.  
  46. clock_gettime(CLOCK_REALTIME, &requestEnd);
  47.  
  48. double accum = ( requestEnd.tv_sec - requestStart.tv_sec ) + ( requestEnd.tv_nsec - requestStart.tv_nsec ) / BILLION;
  49. printf( "Time = %lf\n", accum );
  50.  
  51. /* Take final time here */
  52. /* Use clock_gettime(). Do NOT use clock() */
  53. /* Compute the execution time*/
  54. }
  55.  
  56.  
  57. /* this function is executed by each process */
  58. do_work(int nrProc, int inceput, int b, int pas)
  59. {
  60. int i, j, suma_div;
  61.  
  62.  
  63. for (i = inceput; i <= b; i += pas) {
  64. suma_div = 0;
  65. for (j = 1; j <= i / 2; j++) {
  66. if (i % j == 0) {
  67. suma_div += j;
  68. }
  69. }
  70. if (suma_div == i) {
  71. printf("Proces %d: %d \n", nrProc, i);
  72. }
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement