Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <time.h>
- #include <unistd.h>
- #include <sys/syscall.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- /* Your Answers Here
- 2 a) Compiled the code on Ubuntu 14.04 LTS, with a partition of the harddisk for the OS [i.e dual boot]
- 4X Intel(R) Core(TM) i3 CPU M 330 2.13Ghz RAM 4GB
- 2 b) 8.9 ms
- 2 c) 8.9/1000,000 --> 0.0000089 ms
- 3 a) 9.8 ms
- 3 b) 9.8/1000,000 -> 0.0000098 ms
- syscall responses
- a) 166.5 ms
- b) 166.5/1000,000 --> 0.0001665 ms
- fork and wait responses
- a) 124.6 ms
- b) 124.6/1000 .. I deploy 1000 iterations --> 0.1246 ms
- c) In each pass of the for loop the the fork() function call calls a system call called clone(). In the error case,
- the user function printf(), calls write() or on some distros fwrite() to write the values onto the screen. Then
- the wait() system call is invoked in the parent to avoid zombie processes.
- */
- void A(){
- }
- double timespec_to_ms(struct timespec *ts)
- {
- return ts->tv_sec*1000.0 + ts->tv_nsec/1000000.0;
- }
- int main(int argc, char **argv)
- {
- struct timespec start_time, end_time;
- if(argv[1] <0)
- {
- printf("you entered a negative argument\n");
- exit(-1);
- }
- double *TestArray=malloc(sizeof(double)*atoi(argv[1]));
- for(int i=0;i<atoi(argv[1]));i++)
- {
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
- /* begin timing */
- /*for(int i=0;i<1000000;i++)
- {
- //getpid();
- syscall(SYS_getpid);
- }*/
- /*int errorCount=0;
- for(int i=0;i<1000;i++)
- {
- int pid = fork();
- if (pid == 0) {
- return 0;
- }
- if (pid < 0) {
- printf("ERROR: Fork failed.\n");
- errorCount++;
- }
- wait(0);
- }*/
- /* end timing */
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
- }
- //printf("%f ms\n", timespec_to_ms(&end_time)
- //- timespec_to_ms(&start_time));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement