I have created a shared library where I declare 4K static array data structure (initialized) and share it. Then I access the 4K static array from Program1 for multiple times and each time measure the access time to access the entire 4K shared static array. I got almost same access time ( nearly 17000 ticks).
Now, If I access the same shared static array from another program2, I got almost 1/2 access time than program1. Again after program2 access the shared static array, if I access the same shared static array in Program1 as I did before , I got almost 1/2 access time as original OR almost same access time as Program2.
Can anyone explain me, why it is happening ?
In case 1, before Program2 access shared data structure, program1 access the same shared data structure multiple times. So *why not lower access time for 2nd access * ?
In case 2, why access time in program2 become lower? after program2 access shared data structure , why access time in program1 become lower?
Here is my Shared library :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
static int DATA[1024]={1,2,3,4,.....1024};
inline void foo(void)
{
int j;
int k=0;
for(j=0;j<1024;j++)
{
k=DATA[j];
}
k+=0;
}
Program1 :
foo(); // do not measure time this time to avoid page fault
start=rdtsc();
foo();
end=rdtsc();
temp=end-start;
total_time=temp;
printf("Inside Program1, Before IPC, Time taken 1 = %llu\n",total_time);
start=rdtsc();
foo();
end=rdtsc();
temp=end-start;
total_time=temp;
printf("Inside Program1, Before IPC, Time taken 2 = %llu\n",total_time);
// USE IPC here , to receive signal
start=rdtsc();
foo();
end=rdtsc();
temp=end-start;
total_time=temp;
printf("Inside Program1, after IPC, Time taken 1 = %llu\n",total_time);
// USE IPC here , to send signal
// USE IPC here , to receive signal
start=rdtsc();
foo();
end=rdtsc();
temp=end-start;
total_time=temp;
printf("Inside Program1, after IPC, Time taken 2 = %llu\n",total_time);
Program2 :
foo(); // do not measure time this time to avoid page fault
// USE IPC here , to send signal
start=rdtsc();
foo();
end=rdtsc();
temp=end-start;
total_time=temp;
printf("Inside Program2, after IPC, Time taken 1 = %llu\n",total_time);
start=rdtsc();
foo();
end=rdtsc();
temp=end-start;
total_time=temp;
printf("Inside Program2, after IPC, Time taken 2 = %llu\n",total_time);
// USE IPC here , to receive signal
start=rdtsc();
foo();
end=rdtsc();
temp=end-start;
total_time=temp;
printf("Inside Program2, after IPC, Time taken 3 = %llu\n",total_time);
Output Program1
Inside Program1, Before IPC, Time taken 1 =17200
Inside Program2, Before IPC, Time taken 2 =17200 // Why not lower than previous
**Inside Program1, after IPC, Time taken 1 = 8504** // why lower ?
**Inside Program1, after IPC, Time taken 2 = 7489** // why lower ?
Output Program2
Inside Program2, after IPC, Time taken 1 = 7500
Inside Program2, after IPC, Time taken 2 = 7600
**Inside Program2, after IPC, Time taken 3 = 8500** // Here access time increased as compared to previous