[RK_2014_0918] in linux, test the impact of usleep function on CPU usage. rk_2014_0918usleep
1. Local Environment
CPU information Intel (R) Core (TM) i3 cpu m 350 @ 2.27 GHz
Intel(R) Core(TM) i3 CPU M 350 @ 2.27GHz
Intel(R) Core(TM) i3 CPU M 350 @ 2.27GHz
Intel(R) Core(TM) i3 CPU M 350 @ 2.27GHz
Memory Information
MemTotal: 1990228 kB
2. Build Test code
1. Test code
#include <iostream>#include <unistd.h>using namespace std;#define ElapsedTime 1intmain(void){ cout << "current pid : " << getpid() << endl;
while (1) { //usleep(ElapsedTime); } return 0;}
2. Compile
# g++ -o test_usleep.o test_usleep.cpp
3. Run
# ./test_usleep.o
Iii. CPU usage
Note: Do not run other programs when testing test_usleep.o.
1. No code for "usleep (ElapsedTime );"
CPU usage: 99.9% pid user pr ni virt res shr s % CPU % mem time + COMMAND 9540 root 20 0 3368 893 760 R 99.9 0.0. 52 test_usleep.o
2. # define ElapsedTime 1 // 1 microsecond
CPU usage: 99.9% pid user pr ni virt res shr s % CPU % mem time + COMMAND 9556 root 20 0 3368 872 760 R 99.9 0.0. 10 test_usleep.o
3. # define ElapsedTime 10 // 10 microseconds
CPU usage: 14.0% pid user pr ni virt res shr s % CPU % mem time + COMMAND 9596 root 20 0 3368 868 760 R 14.0 0.0. 99 test_usleep.o
4. # define ElapsedTime 100 // 100 microseconds
CPU usage: 11.7% pid user pr ni virt res shr s % CPU % mem time + COMMAND 9610 root 20 0 3368 868 760 S 11.7 0.0. 16 test_usleep.o
5. # define ElapsedTime 1000 // 1000 microseconds = 1 millisecond
CPU usage: 1.7% pid user pr ni virt res shr s % CPU % mem time + COMMAND 9624 root 20 0 3368 868 760 S 1.7 0: 00. 79 test_usleep.o
6. # define ElapsedTime 10000 // 10000 microseconds = 10 milliseconds
CPU usage: 0.3% pid user pr ni virt res shr s % CPU % mem time + COMMAND 9634 root 20 0 3368 868 760 S 0.3 0.0. 04 test_usleep.o
7. # define ElapsedTime 100000 // 100000 microseconds = 100 milliseconds
CPU usage: 0.0% pid user pr ni virt res shr s % CPU % mem time + COMMAND 9644 root 20 0 3368 872 760 S 0.0 0.0. 00 test_usleep.o
Iv. References
1. Obtain CPU information in linux
cat /proc/cpuinfo
2. Obtain memory information in linux
cat /proc/meminfo
3. in linux, use top to view the status of a single process
top -p pid
5. Website
Http://www.cnblogs.com/tom-and-jerry/p/3978797.html
[End]
How to use Openmp in Linux
Is the result normal in linux?
It may be that the CPU usage is calculated differently. Computing Windows CPU usage is sometimes weird. Especially if you still have io, it is not surprising that you do not count the CPU usage of the kernel as 0%.
In linux, how does one implement sleep (0 )?
In linux, sleep (0) is implemented as follows:
Unsigned int sleep (unsigned int seconds)
{
......
/* This is not necessary but some buggy programs depend on this .*/
If (seconds = 0)
Return 0;
......
}
If your program requires less real-time performance, you can use usleep (1). Otherwise, you can only use signals or other event mechanisms.