Advertisement
HimikoWerckmeister

Untitled

Mar 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <time.h>
  3. #include <unistd.h>
  4. #include <sys/syscall.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10.  
  11.  
  12. void A();
  13.  
  14. double calculateAverage(double *arr, int len)
  15. {
  16. double average=0.00;
  17. //first sum up all entries
  18. for(int i=0;i<len;i++)
  19. {
  20. average+=arr[i];
  21. }
  22. return average;
  23. }
  24.  
  25. static inline long long timespec_to_ns(const struct timespec *tv)
  26. {
  27. return ((long long) tv->tv_sec * 1000000000) +
  28. tv->tv_nsec;
  29. }
  30.  
  31. double timespec_to_ms(struct timespec *ts)
  32. {
  33. return ts->tv_sec*1000.0 + ts->tv_nsec/1000000.0;
  34. }
  35.  
  36. int main(int argc, char **argv)
  37. {
  38. struct timespec start_time, end_time;
  39.  
  40. if(argc <2)
  41. {
  42. printf("You entered less than two arguements\n");
  43. exit(-2);
  44. }
  45. if(argv[1] <0)
  46. {
  47. printf("you entered a negative argument\n");
  48. exit(-1);
  49. }
  50. //double *TestArray=malloc(sizeof(double)*atoi(argv[1]));
  51. //double *TestArray2=malloc(sizeof(double)*atoi(argv[1]));
  52. //double *TestArray3=malloc(sizeof(double)*atoi(argv[1]));
  53. //double *TestArray4=malloc(sizeof(double)*atoi(argv[1]));
  54.  
  55. int n=(atoi(argv[1]));
  56. // Do empty 1
  57. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
  58. for(int i=0;i<n;i++)
  59. {
  60.  
  61. /* begin timing */
  62. /*for(int i=0;i<1000000;i++)
  63. {
  64. //getpid();
  65. syscall(SYS_getpid);
  66. }*/
  67. /*int errorCount=0;
  68. for(int i=0;i<1000;i++)
  69. {
  70. int pid = fork();
  71. if (pid == 0) {
  72. return 0;
  73. }
  74. if (pid < 0) {
  75. printf("ERROR: Fork failed.\n");
  76. errorCount++;
  77. }
  78. wait(0);
  79.  
  80. }*/
  81. A();
  82.  
  83. /* end timing */
  84.  
  85. //TestArray[i]=(timespec_to_ms(&end_time)-timespec_to_ms(&start_time));
  86. }
  87. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
  88. printf("%f ms\n", (timespec_to_ms(&end_time)
  89. - timespec_to_ms(&start_time))/n);
  90.  
  91.  
  92. // Do system getpid() call test
  93. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
  94. for(int i=0;i<n;i++)
  95. {
  96.  
  97. /* begin timing */
  98.  
  99. //A();
  100. getpid();
  101. //syscall(SYS_getpid);
  102.  
  103. /* end timing */
  104.  
  105. //TestArray[i]=(timespec_to_ms(&end_time)-timespec_to_ms(&start_time));
  106. }
  107. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
  108. // Print average of pid test
  109. printf("%f ms\n", (timespec_to_ms(&end_time)
  110. - timespec_to_ms(&start_time))/n);
  111. int status;
  112. int p[2];
  113. int p1[2];
  114. if(pipe(p)<0) exit(1);// 0 for read 1 for writing
  115. //pipe(p);
  116. if(pipe(p1)<0) exit(1);
  117. //pipe(p1[1]);
  118. char arr[1];
  119. //char arr2[1];
  120. int pid=fork();
  121. // Do process spawn context switching overhead test
  122. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
  123. if(pid==0)
  124. {
  125. while(1)
  126. {
  127. if(read(p[0],&arr,1)<=0) exit(1);
  128. if(write(p1[1],&arr,1)<=0) exit(1);
  129.  
  130. }
  131. _exit(status);
  132. }
  133. for(int i=0;i<n;i++){
  134. if(write(p[1],&arr,1)<=0) exit(1);
  135. if(read(p1[0],&arr,1)<=0) exit(1);
  136. }
  137.  
  138.  
  139. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
  140. printf("%f ms\n", (timespec_to_ms(&end_time)
  141. - timespec_to_ms(&start_time))/n);
  142.  
  143. /*printf("%lld ns\n", (timespec_to_ns(&end_time)
  144. - timespec_to_ns(&start_time))/n);
  145. */
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement