Advertisement
HimikoWerckmeister

Untitled

Mar 6th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 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.  
  9. /* Your Answers Here
  10. 2 a) Compiled the code on Ubuntu 14.04 LTS, with a partition of the harddisk for the OS [i.e dual boot]
  11. 4X Intel(R) Core(TM) i3 CPU M 330 2.13Ghz RAM 4GB
  12. 2 b) 8.9 ms
  13. 2 c) 8.9/1000,000 --> 0.0000089 ms
  14.  
  15. 3 a) 9.8 ms
  16. 3 b) 9.8/1000,000 -> 0.0000098 ms
  17.  
  18. syscall responses
  19. a) 166.5 ms
  20. b) 166.5/1000,000 --> 0.0001665 ms
  21.  
  22. fork and wait responses
  23. a) 124.6 ms
  24. b) 124.6/1000 .. I deploy 1000 iterations --> 0.1246 ms
  25. c) In each pass of the for loop the the fork() function call calls a system call called clone(). In the error case,
  26. the user function printf(), calls write() or on some distros fwrite() to write the values onto the screen. Then
  27. the wait() system call is invoked in the parent to avoid zombie processes.
  28.  
  29. */
  30. void A(){
  31. }
  32.  
  33. double timespec_to_ms(struct timespec *ts)
  34. {
  35. return ts->tv_sec*1000.0 + ts->tv_nsec/1000000.0;
  36. }
  37.  
  38. int main(int argc, char **argv)
  39. {
  40. struct timespec start_time, end_time;
  41.  
  42.  
  43. if(argv[1] <0)
  44. {
  45. printf("you entered a negative argument\n");
  46. exit(-1);
  47. }
  48. double *TestArray=malloc(sizeof(double)*atoi(argv[1]));
  49. for(int i=0;i<atoi(argv[1]));i++)
  50. {
  51. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
  52. /* begin timing */
  53. /*for(int i=0;i<1000000;i++)
  54. {
  55. //getpid();
  56. syscall(SYS_getpid);
  57. }*/
  58. /*int errorCount=0;
  59. for(int i=0;i<1000;i++)
  60. {
  61. int pid = fork();
  62. if (pid == 0) {
  63. return 0;
  64. }
  65. if (pid < 0) {
  66. printf("ERROR: Fork failed.\n");
  67. errorCount++;
  68. }
  69. wait(0);
  70.  
  71. }*/
  72.  
  73. /* end timing */
  74. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
  75. }
  76.  
  77. //printf("%f ms\n", timespec_to_ms(&end_time)
  78. //- timespec_to_ms(&start_time));
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement