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>
- #include <stdlib.h>
- #include <fcntl.h>
- void A();
- double calculateAverage(double *arr, int len)
- {
- double average=0.00;
- //first sum up all entries
- for(int i=0;i<len;i++)
- {
- average+=arr[i];
- }
- return average;
- }
- static inline long long timespec_to_ns(const struct timespec *tv)
- {
- return ((long long) tv->tv_sec * 1000000000) +
- tv->tv_nsec;
- }
- 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(argc <2)
- {
- printf("You entered less than two arguements\n");
- exit(-2);
- }
- if(argv[1] <0)
- {
- printf("you entered a negative argument\n");
- exit(-1);
- }
- //double *TestArray=malloc(sizeof(double)*atoi(argv[1]));
- //double *TestArray2=malloc(sizeof(double)*atoi(argv[1]));
- //double *TestArray3=malloc(sizeof(double)*atoi(argv[1]));
- //double *TestArray4=malloc(sizeof(double)*atoi(argv[1]));
- int n=(atoi(argv[1]));
- // Do empty 1
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
- for(int i=0;i<n;i++)
- {
- /* 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);
- }*/
- A();
- /* end timing */
- //TestArray[i]=(timespec_to_ms(&end_time)-timespec_to_ms(&start_time));
- }
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
- printf("%f ms\n", (timespec_to_ms(&end_time)
- - timespec_to_ms(&start_time))/n);
- // Do system getpid() call test
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
- for(int i=0;i<n;i++)
- {
- /* begin timing */
- //A();
- getpid();
- //syscall(SYS_getpid);
- /* end timing */
- //TestArray[i]=(timespec_to_ms(&end_time)-timespec_to_ms(&start_time));
- }
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
- // Print average of pid test
- printf("%f ms\n", (timespec_to_ms(&end_time)
- - timespec_to_ms(&start_time))/n);
- int status;
- int p[2];
- int p1[2];
- if(pipe(p)<0) exit(1);// 0 for read 1 for writing
- //pipe(p);
- if(pipe(p1)<0) exit(1);
- //pipe(p1[1]);
- char arr[1];
- //char arr2[1];
- int pid=fork();
- // Do process spawn context switching overhead test
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
- if(pid==0)
- {
- while(1)
- {
- if(read(p[0],&arr,1)<=0) exit(1);
- if(write(p1[1],&arr,1)<=0) exit(1);
- }
- _exit(status);
- }
- for(int i=0;i<n;i++){
- if(write(p[1],&arr,1)<=0) exit(1);
- if(read(p1[0],&arr,1)<=0) exit(1);
- }
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
- printf("%f ms\n", (timespec_to_ms(&end_time)
- - timespec_to_ms(&start_time))/n);
- /*printf("%lld ns\n", (timespec_to_ns(&end_time)
- - timespec_to_ns(&start_time))/n);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement