而使用()和睡眠()的区别把程序进入睡眠模式 [英] Difference between use of while() and sleep() to put program into sleep mode
问题描述
我创建了一个共享对象,并从两个不同的程序访问和测量的时间。
I have created a shared object and access it from two different program and measuring the time.
的 数据阵列是两个进程之间的共享对象。的
DATA array is the shared object between two processes.
案例1:使用,而里面的程序1
Case 1: Use of while inside program1
程序1:
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中:
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:首先我运行程序1,然后Program2中
OUTPUT : First I run program1, then Program2
Inside Program1, Time1 = 17620
Inside Program1, Time1 = 17680
Inside Program2, Time1 = 7620
Inside Program2, Time1 = 7600
案例2:里面的程序1睡眠()使用
Case 2: Use of sleep() inside program1
程序1:
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中:
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:首先我运行程序1,然后Program2中
OUTPUT : First I run program1, then Program2
Inside Program1, Time1 = 17620
Inside Program1, Time1 = 17680
Inside Program2, Time1 = 17620
Inside Program2, Time1 = 17600
在-1的情况下的输出,我可以说的共享数据数据阵列是由第一个程序,并从缓存中第二个节目访问它加载到内存/缓存。而这也适用于CASE-2,但结果看起来是从缓存中刷新出来,而 PROGRAM1 进入睡眠。
From the output in case -1, I can say shared data DATA array is loaded into memory/cache by 1st program and second program access it from cache. Whereas this also true for CASE-2, but the result looks like it is flushed out from cache while Program1 goes into sleep.
我使用的linux下的GCC。
I am using GCC under linux.
任何线索?提前致谢 。
Any clue ? Thanks in advance .
推荐答案
您没有确切地描述如何运行不同版本(不同的进程?),但假设他们是连续的 - 这可能是你看到的在影响睡眠的()
You didn't describe exactly how you run the different versions (different processes?), but assuming they're sequential - It is possible that you're seeing the affect of sleep()
这取决于课程的具体实现和硬件,但它很可能给你的CPU送入一些省电/睡眠状态(这是它的设计)。如果是这样的话,那么核心高速缓存将被刷新的过程中,你会唤醒冷缓存。在另一方面,WHIE循环的目的是做一个忙等循环而磨你的CPU,并保持它活着(使用高速缓存一起),除非你碰巧相处方式的上下文切换。
It depends of course on the exact implementation and HW, but it's very likely to send your CPU into some power-saving/sleep state (that's what it's designed for). If that's the case, then the core caches will have to be flushed as part of the process, and you'll wake-up with cold caches. The whie loop on the other hand is intended to do a busy wait loop while grinding your CPU and keeping it alive (along with the caches), unless you happen to get a context switch along the way.
具体细节将再次取决于实施,在x86你可以使用内联汇编调用监控+ MWAIT指令,允许你指定你想要达到的确切C状态的深度。它是,更多的缓存会得到闭合(大多为L3相关)。更深层次的
The exact details would again depend on implementation, on x86 you can use inline assembly to invoke monitor+mwait instructions that allow you to specify the exact C-state depth you want to achieve. The deeper it is, the more caches will get closed (mostly relevant for the L3).
这篇关于而使用()和睡眠()的区别把程序进入睡眠模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!