The difference between using while () and sleep () to put the program to sleep

I created a shared object and access to it from two different programs and time measurements.

A DATA array is a shared object between two processes.

Case 1: Inside use1

program1:

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program1, Time1=%d\n",Time_needed);    


start=timer;
access shared DATA array   
end=timer;

Time_needed= end-start
printf("Inside Program1, Time2=%d\n",Time_needed);    

while(1){}; // I replace this by sleep(1000) in CASE-2

Program2:

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    
start=timer;
access shared DATA array      
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    

OUTPUT: first I run program1, then Program2

Inside Program1, Time1 = 17620
Inside Program1, Time1 = 17680

Inside Program2, Time1 = 7620
Inside Program2, Time1 = 7600

Case 2: Using Sleep () Inside a Program1

program1:

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program1, Time1=%d\n",Time_needed);    


start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program1, Time2=%d\n",Time_needed);    

sleep(1000);

Program2:

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    
start=timer;
access shared DATA array   
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    

OUTPUT: first I run program1, then Program2

Inside Program1, Time1 = 17620
Inside Program1, Time1 = 17680

Inside Program2, Time1 = 17620
Inside Program2, Time1 = 17600

From the output in the case of -1, I can say that the shared data DATA array is loaded into memory / cache according to the 1st program, and the second is the cache from the cache. While this is also true for CASE-2, but the result looks as if it is unloaded from the cache, and Program1 goes into sleep mode.

I am using GCC under linux.

? .

0
2

, ( ?), , . , ()

, , HW, , / ( ). , , . , whie , ( ), .

, x86 monitor + mwait, C-, . , ( L3).

0

, .

H/W, (, ( ) ).

, program_2 program_1, . , program_2 , _1, .

, - - , program_1.

0

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


All Articles