Advertisement
HimikoWerckmeister

Untitled

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