#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define cpu_set_t thread_affinity_policy_data_t #define CPU_SET(cpu_id, new_mask) \ (*(new_mask)).affinity_tag = (cpu_id + 1) #define CPU_ZERO(new_mask) \ (*(new_mask)).affinity_tag = THREAD_AFFINITY_TAG_NULL #define GET_AFFINITY(pid, size, mask) \ (*(mask)).affinity_tag = THREAD_AFFINITY_TAG_NULL #define SET_AFFINITY(pid, size, mask) \ thread_policy_set(mach_thread_self(), THREAD_AFFINITY_POLICY, \ (int *)mask, THREAD_AFFINITY_POLICY_COUNT) // this function return the current time of system in terms of milliseconds double calculatetime() { /* double timeinseconds = 0.0; struct timeval ctime; gettimeofday(&ctime, (struct timezone*)0); timeinseconds = (double)(ctime.tv_sec + ctime.tv_usec*1.0e-3); return(timeinseconds); */ { /* Calculate nanoseconds in a timeval structure */ clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); return((double)mts.tv_sec)*1000000000 + (double) mts.tv_nsec; } } // this is a sample procedure call which do some primary operation int Func_call(){ int j; int sum =0; return(sum); } int main(int argc, const char * argv[]) { //variables to record times double start ; double end; // variables to set to set thread affinity to one processor cpu_set_t mask; CPU_ZERO(&mask); unsigned int lenght = sizeof(mask); CPU_SET(0, &mask); // pipe variables int pipeone[2], pipetwo[2]; int byte_lenght; char connectionString[] = "This is a message sent via the pipe!\n"; pid_t child_processId; char buff_read[500], buff_read2[500]; //variables to record times in system call and procedure call double systemCallStart, systemCallSstop, avgSystemCallDuration, dur, avgFunctionCallDuration; long k; int i,p; long num_itr = 1000000; int n; printf("Start Running \n"); // This part calculates the cost of a procedure call //record the beginning time of procedure call operations systemCallStart = calculatetime(); for (i = 0; i< num_itr; i++){ Func_call(); } systemCallSstop = calculatetime(); dur =systemCallSstop - systemCallStart; avgFunctionCallDuration = (systemCallSstop - systemCallStart) / num_itr; printf("The average time takes for a function call is: %f ns\n", avgFunctionCallDuration); // This part of the code calcluate the cost of system call in unix so we call a system // function getpid() in a loop for 1000000 //record the beginning time of system call operations systemCallStart = calculatetime(); for (k = 0; k