Linux下时间度量的深入分析
一)ANSI clock函数
1)概述:
clock 函数的返回值类型是clock_t,它除以CLOCKS_PER_SEC来得出时间,一般用两次clock函数来计算进程自身运行的时间.
ANSI clock有三个问题:
1)如果超过一个小时,将要导致溢出.
2)函数clock没有考虑CPU被子进程使用的情况.
3)也不能区分用户空间和内核空间.
所以clock函数在linux系统上变得没有意义.
2)测试
编写test1.c程序,测试采用clock函数的输出与time程序的区别.
vi test1.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void )
{
long i=1000L;
clock_t start, finish;
double duration;
printf( "Time to do %ld empty loops is ", i );
start = clock();
while (--i){
system("cd");
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", duration );
return 0;
}
gcc test1.c -o test1
time ./test1
Time to do 1000 empty loops is 0.180000 seconds
real 0m3.492s
user 0m0.512s
sys 0m2.972s
3)总结:
(1)程序调用 system("cd");,这里主要是系统模式子进程的消耗,test1程序不能体现这一点.
(2)0.180000 seconds秒的消耗是两次clock()函数调用除以CLOCKS_PER_SEC.
(3)clock()函数返回值是一个相对时间,而不是绝对时间.
(4)CLOCKS_PER_SEC是系统定义的宏,由GNU标准库定义为1000000.
二)times()时间函数
1)概述:
原型如下:
clock_t times(struct tms *buf);
tms结构体如下:
strace tms{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
注释:
tms_utime记录的是进程执行用户代码的时间.
tms_stime记录的是进程执行内核代码的时间.
tms_cutime记录的是子进程执行用户代码的时间.
tms_cstime记录的是子进程执行内核代码的时间.
2)测试:
vi test2.c
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static void do_cmd(char *);
st
相关文档:
Linux 同步方法剖析
内核原子,自旋锁和互斥锁
文档选项
将此页作为电子邮件发送
级别: 中级
M. Tim Jones, 顾问工程师, Emulex
2007 年 11 月 19 日
在学习 Linux® 的过程中,您也许接触过并发(concurrency)、临界段(critical section)和锁定,但是如何在内核中使用这些概念呢?本文讨 ......
今天碰到个问题,中文文件和带中文的目录通过SVN取到WINDOWS系统,然后通过SCP传到LINUX服务器之后,出现了文件名及目录名编码集改变的问题。(都变成了GBK)
最后通过下载一个linux专门用于转换 文件名和目录名 的工具,convmv
转换指令如下:
convmv -f [原始编码] -t [目标编码] -r(目录循环) --notest(实际改变,否 ......
以Red Hat为例:
1.进入安装目录
#cd /usr/local
给所有用户添加可执行的权限
#chmod +x jdk-1_5_0-linux-i586.rpm.bin
#./jdk-1_5_0-linux-i586.rpm.bin
此时会生成文件jdk-1_5_0-linux-i586.rpm,同样给所有用户添加可执行的权限
#chmod +x jdk-1_5_0-linux-i586.rpm
安装程序
#rpm&nb ......
CPU查看:
1、 # dmesg | grep CPU
Initializing CPU#0
CPU: Trace
cache: 12K uops
CPU: L1 I-cache: 0K, L1 D-cache: 16K
CPU: L2
cache: 256K
Intel machine check reporting enabled on CPU#0.
CPU: After generic, caps: bfebfbff 00000000 00000000 00000000
CPU ......
redhat9下安装VMware Tools
下面是安装步骤:
1.启动Rad Hat 9.0(图形界面方式登陆),并且以管理员的身份登陆。
2.在VMware虚拟机的菜单中点击:虚拟机->安装VMware 工具->install。
3.Red Hat 9.0自动挂载VMware Tools的虚拟光驱,并显示在桌面。
4.进去VMware Tools的虚拟光驱里,把VMwareTools-5.5.1-19175.tar ......