Unexpected lower access times in multiple process scenarios compared to a single process scenario

I access the shared library (shared data array structure) from program 1 and find the access time to read all the elements of this array. I got about 17,000 ticks, and only Program1 was running alone.

Now, when I run program2 (having an empty while loop to keep it from completing) on ​​another tab, first start program 1 and measure the access time to read all the elements of this array. To my surprise, now I get 8000ticks compared to the previous script, where only Program1 is executed.

It seems that while ONLY program1 is running, it takes longer to read the array than when there are 2 programs, program 1 performs the same task as the previous one, and program2 keeps the processor busy with the while loop. A higher access time with the presence of program 1 is expected, while the real result is the opposite.

Why is this happening?

Here is a shared library

#include <stdio.h> 
static const int DATA[1024]={1 ,2 ,3,.....1024];
inline void foo(void)
{
    int j, k=0,count=0;

    for(j=0;j<1024;j++)
      {
        k=DATA[j];
      }

    k+=0;    
}

Program1

   int main(void)
    {    
    foo();

    start=timer();
    foo();
    end=timer();
    printf("Time1=%llu\n",end-start);    

    start=timer();
    foo();
    end=timer();
    printf("Time2=%llu\n",end-start);    


    start=timer();
    foo();
    end=timer();
    printf("Time3=%llu\n",end-start); 
    sleep(1);

    start=timer();
    foo();
    end=timer();
    printf("after sleep(1)\n");
    printf("Time4=%llu\n",end-start);    

    start=timer();
    foo();
    end=timer();
    printf("Time5=%llu\n",end-start);    

    sleep(2); 
    start=timer();
    foo();
    end=timer();
    printf("after sleep(2)\n");
    printf("Time6=%llu\n",end-start);    

    return 0;
    }

program2

   int main(void)
    {
    while(1)
        {}        
    return 0;
    }

CASE1 (ONLY program1 works)

Exit

Time1=17918
Time2=17672  
Time3=17816  

after sleep(1)
**Time4= 20716 ** // Is it due to wake up from sleep mode ?
Time5=17722

after sleep(2)
**Time6=20910** // Is it due to wake up from sleep mode ?

CASE1 (Program2 starts first, then Program1 starts)

Exit

Time1 =7483  
Time2=7205
Time3=7399

after sleep(1)
**Time4= 8734 ** // Is it due to wake up from sleep mode ?
Time5=7326

after sleep(2)
**Time6=9070** // Is it due to wake up from sleep mode ?

According to my understanding, while the processor is used only by program 1, the time required to read the array should be less than the CPU used by both Program1 and program2.

Where do I make mistakes? I have an i7 machine, only one core, hyperthreading is disabled, ASLR is disabled.

1:

Mystial, , ONLY program1 , CPU , , , . DATA .

. Program1 Program2 .

#include <stdio.h> 
static const int DATA[1024]={1 ,2 ,3,.....1024];
inline void foo(void)
{
    int j, k=0,count=0;
  while(count++<10000)
  {  
    for(j=0;j<1024;j++)
      {
        k=DATA[j];
      }
   }  
    k+=0;    
}

CASE1 ( 1 )

Time1=75186246
Time2=77570299 
Time3=80548529 

after sleep(1)
**Time4= 92608363 ** // Is it due to wake up from sleep mode ?
Time5=75616487

after sleep(2)
**Time6=97021338** // Is it due to wake up from sleep mode ?

CASE1 ( Program2, Program1)

Time1 =139337099 
Time2=155801957
Time3=146586856

after sleep(1)
**Time4= 130558062 ** // Why lower access time after sleep mode ?
Time5=145250551 // Time5 is expected lower than Time4 as other run . Why lower here ?

after sleep(2)
**Time6=130940183** // Again Why lower access time after sleep mode ?

  • 2, , (t4/t6) (t3/t5, ). , - , Mystical?

  • , 2 , (t4/t6) (t3/t5, ). q (1) q (2) . (t4 < t3)? DATA ( ).

  • t2<t1 and t3<t2 true, , . ?

gcc linux. , , . .

+4
1

, , , :

. , , "" .

program1 CPU . , , .

( 2), 2 CPU . , .


, , , , , . ( 1 )

+5

Source: https://habr.com/ru/post/1526476/


All Articles