| CODE: | [Copy to clipboard] | |
| ||
升级OpenSSH(依照最新的OpenSSL)
| CODE: | [Copy to clipboard] | |
| ||
OK,现在ssh -V看一下版本吧
|
|
用户名:clay111 笔名:clay111 地区: 行业:其他 |
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
所有文章大部分从网络上面收集,少部分为原创文章。收集的文章都是一些在工作当中对我帮助很大的文章,在这里首先对原创作者表示感谢,由于很多文章不知道具体的作者是谁,所以我一般在题目加上ZT字样,如果你是文章的原创作者,可以和我留言,我把文章的署名改成您自己。
源码升级-(OpenSSL + OpenSSH) ZT
| CODE: | [Copy to clipboard] | |
| ||
| CODE: | [Copy to clipboard] | |
| ||
apache配置虚拟机
(总是忘,记下来)
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@5zuiai.com
DocumentRoot /usr/local/apache2/htdocs/lhjweb
ServerName www.5zuiai.com
<Directory "/usr/local/apache2/htdocs/lhjweb">
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ErrorLog logs/5zuiai-error_log
TransferLog logs/5zuiai-access_log
</VirtualHost>
DL360服务器的SCSI安装gentoo(Z)
让卡巴挂掉的代码
gentoo 安装NTPf服务
restrict 0.asia.pool.ntp.org
restrict 1.asia.pool.ntp.org
restrict 2.asia.pool.ntp.org
restrict 3.asia.pool.ntp.org
restrict 127.0.0.1 mask 255.0.0.0
restrict 192.168.1.0 mask 255.255.255.0 nomodify
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org
broadcastdelay 0.008
logfile /var/log/ntp.log
3、查看ntp有没有工作
ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+arteq.com.tw 220.130.158.51 3 u 29 64 377 93.116 38.782 7.695
211.22.55.116 .INIT. 16 u - 64 0 0.000 0.000 0.000
*203.116.5.254 130.207.244.240 2 u 33 64 377 82.320 31.845 11.375
+q037240.ppp.asa 200.23.51.205 2 u 221 64 150 405.989 -117.80 40.170
ntptrace xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx: stratum 3, offset -0.029005, synch distance 0.365843
203.116.5.254: timed out, nothing received
***Request timed out
4、查看log文件
tail -f /var/log/ntp.log
12 Jul 15:35:35 ntpd[2089]: synchronized to 59.124.71.8, stratum 3
12 Jul 15:35:35 ntpd[2089]: kernel time sync status change 0001
5、启动服务
/etc/init.d/ntpd start|stop|restart
6、注意事项
服务启动之后,大约5分钟左右才能进行时间服务。看到日志文件出现同步信息,基本服务就好了, 如果过了很长时间没有同步信息或者不能提供时间服务,请检查配置文件。
参考文档:
http://linux.vbird.org/linux_server/0440ntp.php#server_ntp.conf
Local time zone must be set--see zic
cp /usr/share/zoneinfo/Europe/Chongqing /etc/localtime
是因为没有设置localtime,也就是没有自己的时区,拷贝一个就好了。
shell date日期应用(ZT)
无光驱安装gentoo
无光驱安装gentoo
原理:服务器启动,通过网络从DHCP服务器获取TFTP服务器地址,服务器从TFTP服务器下载pxelinux并且执行,pxelinux读取配置文件,从网络下载gentoo内核和引导文件,从而引导gentoo系统,系统引导成功以后,可以正常安装gentoo。
1、环境介绍
一台gentoo服务器(IP:192.168.1.251),一个交换机,一个需要安装系统的无光驱服务器
2、安装DHCP服务器
首先配置gentoo服务器,gentoo服务器需要安装配置DHCP、TFTP服务。
Emerge dhcp
安装完成之后,修改DHCP配置文件
Nano –w /etc/dhcp/dhcpd.conf
文件内容如下所示:
ddns-update-style interim;
ignore client-updates;
option space PXE;
option PXE.mtftp-ip code 1 = ip-address;
option PXE.mtftp-cport code 2 = unsigned integer 16;
option PXE.mtftp-sport code 3 = unsigned integer 16;
option PXE.mtftp-tmout code 4 = unsigned integer 8;
option PXE.mtftp-delay code 5 = unsigned integer 8;
option PXE.discovery-control code 6 = unsigned integer 8;
option PXE.discovery-mcast-addr code 7 = ip-address;
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
option vendor-class-identifier "PXEClient";
vendor-option-space PXE;
option PXE.mtftp-ip 0.0.0.0;
filename "pxelinux.0";
next-server 192.168.1.251;
}
subnet 192.168.1.0 netmask 255.255.255.0 {
option routers 192.168.1.1;
option subnet-mask 255.255.255.0;
range 192.168.1.80 192.168.1.100;
}
启动DHCP服务
/etc/init.d/dhcpd start
3、配置TFTP服务器
首先建立TFTP工作目录
Mkdir /diskless
安装TFTP软件
Emerge tftp-hpa
配置TFTP
Nano –w /etc/conf.d/in.tftpd
配置文件内容如下所示:
# /etc/init.d/in.tftpd
INTFTPD_PATH="/diskless"
INTFTPD_USER="nobody"
#INTFTPD_OPTS="-u ${INTFTPD_USER} -l -vvvvvv -p -c -s ${INTFTPD_PATH}"
INTFTPD_OPTS="-l -v -s ${INTFTPD_PATH}"
启动TFTP
/etc/init.d/in.tftpd start
4、制作gentoo网络启动文件(关键步骤)
首先下载
LiveCD+PXE-0.0.1.tar.bz2 和 install-x86-universal-2004.2.iso
Cp install-x86-universal-2004.2.iso /tmp
解压文件 LiveCD+PXE
Tar zxjpf LiveCD+PXE-0.0.1.tar.bz2
Cd LiveCD+PXE-0.0.1
修改LiveCD+PXE.sh
Vi LiveCD+PXE.sh
查找PXE_HOST和PXE_DIR 修改成自己的服务器地址
PXE_HOST="192.168.1.251"
PXE_DIR="/diskless"
. /LiveCD+PXE.sh smp init
./ LiveCD+PXE.sh smp finish
此步骤为制作gentoo网络启动文件的关键步骤(生成:smp和smp.igz文件),如果制作不成功,则无法通过网络启动gentoo
5、安装pxelinux
Emerge syslinux
cp /usr/lib/syslinux/pxelinux.0 /diskless
mkdir /diskless/pxelinux.cfg
touch /diskless/pxelinux.cfg/default
vi /diskless/pxelinux.cfg/default
内容如下:
prompt 1
timeout 10
default livecd
label livecd
kernel smp
append init=/linuxrc initrd=smp.igz ramdisk_size=80000 ip=dhcp root=/dev/ram0 looptype=squashfs loop=/livecd.squashfs cdroot PXE keymap=us
6、启动需要安装系统的服务器
启动服务器,选择通过PXE启动,尽情享受gentoo网络启动带来的乐趣吧。
注意事项:
1、启动文件为gentoo 2004版 ,linux内核是2.4,如果安装gentoo 2007 LiveCD,安装时会提示linux 内核版本太低,不能安装。只能用install-x86-universal-2004.2.iso进行安装,然后升级系统软件。
2、LiveCD+PXE-0.0.1.tar.bz2 和 install-x86-universal-2004.2.iso 安装包在192.168.1.251上面都有,有需要安装测试的可以从这上面拷贝安装。
另类安装gentoo
通过正常步骤安装gentoo
此处省略2000字安装步骤。注意事项:一定要用genkernel -all来生成系统内核,因为genkernel 生成的内核和liveCD比较类似,系统兼容性比较好。如果不用genkernel生成内核,
挂上需要安装系统的硬盘,用LiveCD启动系统
Fdisk /dev/hda
系统分区
livecd ~ # fdisk -l /dev/hda
Disk /dev/hda: 78 GB,
255 heads, 63 sectors/track, 72943 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 1 12 96358+ 83 Linux
/dev/hda2 13 110 787185 82 Linux swap / Solaris
/dev/hda3 111 72943 585031072+ 83 Linux
/hda1目录为grub 和系统启动文件
/hda3为linux系统文件
我们知道linux一切都是文件,所以我们只要把所有的文件都拷贝过来就好了。
Fdisk –l /dev/hdb (这个是需要安装系统的硬盘)
/dev/hdb1 1 12 96358+ 83 Linux
/dev/hdb2 13 110 787185 82 Linux swap / Solaris
/dev/hdb3 111 72943 585031072+ 83 Linux
Mkdir /mnt/sroot linux系统文件目录
Mkidr /mnt/sboot 启动文件目录
Mkdir /mnt/groot
Mkidr /mnt/gboot
Mount /dev/hda3 /mnt/sroot mount 原目录
Mount /dev/hda1 /mnt/sboot mount 原目录
Mount /dev/hdb3 /mnt/groot mount 目标目录
Mount /dev/hdb1 /mnt/gboot mount 目标目录
Cp –a /mnt/sroot /mnt/groot 拷贝需要安装的文件到目标目录
Cp –a /mnt/sboot /mnt/gboot 拷贝系统启动文件到目标目录
拷贝完成以后,linux系统基本安装完成。只要在目标盘重新安装grub启动就好了。
3、摘下原盘,用liveCD启动。
重新安装grub就好了。
此方法与克隆安装互有优缺点:
克隆安装,只能在两个同等大小的硬盘之间克隆,如果原盘比目标盘大,则不能用克隆方法。
克隆安装比较方便,克隆完成之后,克隆盘可直接启动。
硬盘对拷方法适用于硬盘大小不统一之间的安装linux。
vmstat 说明(ZT )
gentoo 没有加载网卡怎么办?
错误提示:
ERROR: problem starting needed services
“netmount” was not started
查看网卡型号:
lspci
察看编译了哪些模块:
ls /lib/modules/`uname -r`/kernel/drivers/net
动态加载模块:
modprobe e100
加入自动加载模块组:
nano -w /etc/modules.autoload.d/kernel-2.4
在文件里面直接写模块名字,例如:
e100 #(我这里是网卡的型号)
修改/etc/conf.modeles,没有效果。开始在google乱搜,最后仔细阅读gentoo文档,发现:
原始碼 10: 搜尋提供的模組
# ls /lib/modules/`uname -r`/kernel/drivers/net
如果您找到您的網路卡所用的驅動程式,使用 modprobe 載入這個核心模組:
原始碼 11: 使用 modprobe 載入核心模組
(舉例來說,我們將載入 pcnet32 模組)
# modprobe pcnet32
尝试modprobe不同文件,包括ne2k,8139cp。最后modprobe 8139too。安装成功!
但现在还是无法使用文档中说的net-setup,难道是目录不对,我find / 没有发现net-setup的文件名。
注册7年,首次发现一点和别人遇到的情况不同的,首次发帖。但最后解决方法还是在手册中,呵呵!还是那个道理多看手册,问题几乎都能解决。(不知道发在安装版还是gentoo,由斑竹定夺吧!)
gentoo syslog-ng的设置(ZT)
原文摘自:http://blog.sina.com.cn/u/4707c9270100075v
log对于一个系统的重要性是显而易见的,然而不幸的是我发现我的系统并没有很好地记录应该记录的东西,至少昨天半夜它自动重启了,我却找不到为什么。
安装的时候安装的syslog-ng,查了一下它的配置文件,似乎记录的东西很少,对于系统安全来说是很不够的,于是自己man了很多东西,修改了一下设置,过程如下:
1、man其实没什么用,大概我基础不好,里面说的东西我看不懂。
2、syslog-ng的配置文件在/etc/syslog-ng/syslog-ng.conf,里面似乎只记录了messeges,没有其他东西,不确信的话可以自己打开看看。
3、安全起见,先将文件备份。
4、查看syslog-ng的配置说明,参考这个例子:/usr/share/doc/syslog-ng-1.6.9/syslog-ng.conf.sample.gz,然后修改/etc/syslog-ng/syslog-ng.conf,按格式按自己的需要修改。我的修改后的文件如下:
# $Header: /var/cvsroot/gentoo-x86/app-admin/syslog-ng/files/syslog-ng.conf.gentoo,v 1.5 2005/05/12 05:46:10 mr_bones_ Exp $#
# Syslog-ng default configuration file for Gentoo Linux
# contributed by Michael Sterrett
options {
chain_hostnames(off);
sync(0);
# The default action of syslog-ng 1.6.0 is to log a STATS line
# to the file every 10 minutes. That's pretty ugly after a while.
# Change it to every 12 hours so you get a nice daily update of
# how many messages syslog-ng missed (0).
stats(43200);
};
source src { unix-stream("/dev/log"); internal(); pipe("/proc/kmsg"); };
destination authlog { file("/var/log/auth.log"); };
destination syslog { file("/var/log/syslog"); };
destination user { file("/var/log/user.log"); };
destination messages { file("/var/log/messages"); };
# By default messages are logged to tty12...
destination console_all { file("/dev/tty12"); };
# ...if you intend to use /dev/console for programs like xconsole
# you can comment out the destination line above that references /dev/tty12
# and uncomment the line below.
#destination console_all { file("/dev/console"); };
log { source(src); destination(authlog); };
log { source(src); destination(syslog); };
log { source(src); destination(user); };
log { source(src); destination(messages); };
log { source(src); destination(console_all); };
要注意的是sample文件里面有filter的内容,如果你不知道这个是干什么的,man一下syslog-ng。需要的话照sample里面的样子,先添加filter段,再在log段里面添加相应内容。如果你不需要,就象我这样,把需要log的内容直接cp进来,把filter段去掉。
修改:今天查了一下,似乎有几个log里面的内容是差不多的,syslog和auth记录了重复的东西,想必是filter没定义的缘故,于是修改如下(增加了filter段):
# $Header: /var/cvsroot/gentoo-x86/app-admin/syslog-ng/files/syslog-ng.conf.gentoo,v 1.5 2005/05/12 05:46:10 mr_bones_ Exp $#
# Syslog-ng default configuration file for Gentoo Linux
# contributed by Michael Sterrett
options {
chain_hostnames(off);
sync(0);
# The default action of syslog-ng 1.6.0 is to log a STATS line
# to the file every 10 minutes. That's pretty ugly after a while.
# Change it to every 12 hours so you get a nice daily update of
# how many messages syslog-ng missed (0).
stats(43200);
};
source src { unix-stream("/dev/log"); internal(); pipe("/proc/kmsg"); };
destination authlog { file("/var/log/auth.log"); };
destination syslog { file("/var/log/syslog"); };
destination user { file("/var/log/user.log"); };
destination messages { file("/var/log/messages"); };
# By default messages are logged to tty12...
destination console_all { file("/dev/tty12"); };
# ...if you intend to use /dev/console for programs like xconsole
# you can comment out the destination line above that references /dev/tty12
# and uncomment the line below.
#destination console_all { file("/dev/console"); };
filter f_auth { facility(auth); };
filter f_authpriv { facility(auth, authpriv); };
filter f_syslog { not facility(authpriv, mail); };
filter f_user { facility(user); };
log { source(src); filter(f_authpriv); destination(authlog); };
log { source(src); filter(f_syslog); destination(syslog); };
log { source(src); filter(f_user); destination(user); };
log { source(src); destination(messages); };
log { source(src); destination(console_all); };
设置iptables log 如下所示:
vi /etc/syslog-ng/syslog-ng.conf
destination kern { file("/var/log/iptables.log"); };
filter f_kern { facility(kern); };
log { source(src); filter(f_kern); destination(kern); };
保存退出
shell下面运行:
/etc/init.d/syslog-ng reload
iptables -A INPUT -p icmp -j LOG --log-level debug --log-prefix "This is a test!"
查看:
tail -f /var/log/iptables.log
输出类似如下:
Jun 8 19:42:02 localhost szkingrose test---------IN=eth0 OUT= MAC=00:15:58:10:6f:70:00:18:39:84:7a:bc:08:00 SRC=59.151.18.182 DST=192.168.1.251 LEN=84 TOS=0x00 PREC=0x00 TTL=51 ID=20409 PROTO=ICMP TYPE=0 CODE=0 ID=2371 SEQ=5
Jun 8 19:42:02 localhost szkingrose test---------IN=eth0 OUT= MAC=00:15:58:10:6f:70:00:1b:fc:1a:09:c7:08:00 SRC=192.168.1.122 DST=192.168.1.251 LEN=60 TOS=0x00 PREC=0x00 TTL=128 ID=59970 PROTO=ICMP TYPE=8 CODE=0 ID=512 SEQ=19727
Jun 8 19:42:03 localhost szkingrose test---------IN=eth0 OUT= MAC=00:15:58:10:6f:70:00:1b:fc:1a:09:c7:08:00 SRC=192.168.1.122 DST=192.168.1.251 LEN=60 TOS=0x00 PREC=0x00 TTL=128 ID=59973 PROTO=ICMP TYPE=8 CODE=0 ID=512 SEQ=19983
Jun 8 19:42:04 localhost szkingrose test---------IN=eth0 OUT= MAC=00:15:58:10:6f:70:00:1b:fc:1a:09:c7:08:00 SRC=192.168.1.122 DST=192.168.1.251 LEN=60 TOS=0x00 PREC=0x00 TTL=128 ID=59976 PROTO=ICMP TYPE=8 CODE=0 ID=512 SEQ=20239
博客中国实在是太滥了
用了这么长时间,也懒得搬家了,但是实在是不爽,我要骂两句。
你们的文章总数准么? 你们的文章年度文件夹文章数对么?
你们怎么测试的产品,就拿着东西出来唬人了?
博客中国实在是太滥了,什么时候新浪推出博客中国的搬家工具,我搬到新浪去。
c与mysql连接和一个简单查询的例子(ZT)
今天给新来的同事做mysql培训,用到的一个例子:
连接数据库test 用户名root密码abc
在里面有一个username的表,describe username结果如下
mysql> describe username;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| username | varchar(32) | | PRI | | |
+----------+-------------+------+-----+---------+-------+
mysql> select * from username;
+------+----------+
| id | username |
+------+----------+
| 1 | sfs |
+------+----------+
1 row in set (0.00 sec)
[b:f09d522785]本文目的查找执行select id from username这个语句并打印结果[/b:f09d522785]
[code:1:f09d522785]
#include <unistd.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <mysql/mysql.h>
#include <signal.h>
#include <errno.h>
#include <syslog.h>
MYSQL mysql;
main()
{
char host[32]="localhost";
char user[32]="root";
char passwd[32]="abc";
char dbname[32]="test";
if( mysql_init(&mysql) == NULL )
{
syslog(LOG_USER|LOG_INFO,"inital mysql handle error\n");
return 1;
}
if (mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)
{
syslog(LOG_USER|LOG_INFO, "Failed to connect to database: Error: %s\n",mysql_error(&mysql));
return 1;
}
else syslog(LOG_USER|LOG_INFO, "connect to database: \n");
find_ps();
db_close();
return 0;
}
int db_close()
{
mysql_close(&mysql);
return 0;
}
int find_ps ()
{
MYSQL_ROW m_row;
MYSQL_RES *m_res;
char sql[1024],username[32];
int res=1;
int *id;
sprintf(sql,"select id from username");
if(mysql_query(&mysql,sql) != 0)
{
syslog(LOG_USER|LOG_INFO, "select ps_info Error: %s\n",mysql_error(&mysql));
return res;
}
m_res = mysql_store_result(&mysql);
if(m_res==NULL)
{
syslog(LOG_USER|LOG_INFO, "select username Error: %s\n",mysql_error(&mysql));
res = 3;
return res;
}
if(m_row = mysql_fetch_row(m_res))
{
printf("m_row=%d\n",atoi(m_row[0]));
res = 0;
}
mysql_free_result(m_res);
return res;
}
[/code:1:f09d522785]
编译命令
gcc -g connect_db.c -L/usr/lib/mysql -lmysqlclient -lz
注意:
如果/tmp/ccTGmMS21.o: In function `main':
/tmp/ccTGmMS21.o(.text+0x11): undefined reference to `mysql_init'
那么参数增加-L/usr/lib/mysql -lmysqlclient
如果
usr/lib/mysql/libmysqlclient.a(my_compress.o): In function `my_uncompress':
my_compress.o(.text+0xaa): undefined reference to `uncompress'
那么增加-lz参数
我自己是这么编译的 呵呵
gcc -o ttmysql ttmysql.c -L /usr/lib/mysql/lib -lmysqlclient -lz
C语言中关于时间的函数
本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和显示格式等方面进行了阐述。本文还通过大量的实例向你展示了time.h头文件中声明的各种函数和数据结构的详细使用方法。
关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元)
1.概念
在C/C++中,对字符串的操作有很多值得注意的问题,同样,C/C++对时间的操作也有许多值得大家注意的地方。最近,在技术群中有很多网友也多次问到过C++语言中对时间的操作、获取和显示等等的问题。下面,在这篇文章中,笔者将主要介绍在C/C++中时间和日期的使用方法.
通过学习许多C/C++库,你可以有很多操作、使用时间的方法。但在这之前你需要了解一些“时间”和“日期”的概念,主要有以下几个:
Coordinated Universal Time(UTC):协调世界时,又称为世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT)。比如,中国内地的时间与UTC的时差为+8,也就是UTC+8。美国是UTC-5。
Calendar Time:日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是“相对时间”,但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的。
epoch:时间点。时间点在标准C/C++中是一个整数,它用此时的时间和标准时间点相差的秒数(即日历时间)来表示。
clock tick:时钟计时单元(而不把它叫做时钟滴答次数),一个时钟计时单元的时间长短是由CPU控制的。一个clock tick不是CPU的一个时钟周期,而是C/C++的一个基本计时单位。
我们可以使用ANSI标准库中的time.h头文件。这个头文件中定义的时间和日期所使用的方法,无论是在结构定义,还是命名,都具有明显的C语言风格。下面,我将说明在C/C++中怎样使用日期的时间功能。
2. 计时
C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:
clock_t clock( void );
这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
#define CLOCKS_PER_SEC ((clock_t)1000)
可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:
void elapsed_time()
{
printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);
}
当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:
#i nclude “stdio.h”
#i nclude “stdlib.h”
#i nclude “time.h”
int main( void )
{
long i = 10000000L;
clock_t start, finish;
double duration;
/* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- ) ;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", duration );
system("pause");
}
在笔者的机器上,运行结果如下:
Time to do 10000000 empty loops is 0.03000 seconds
上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。
3.与日期和时间相关的数据结构
在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:
#ifndef _TM_DEFINED
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
#define _TM_DEFINED
#endif
ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。
而日历时间(Calendar Time)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。在time.h中,我们也可以看到time_t是一个长整型数:
#ifndef _TIME_T_DEFINED
typedef long time_t; /* 时间值 */
#define _TIME_T_DEFINED /* 避免重复定义 time_t */
#endif
大家可能会产生疑问:既然time_t实际上是长整型,到未来的某一天,从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间)超出了长整形所能表示的数的范围怎么办?对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。为了能够表示更久远的时间,一些编译器厂商引入了64位甚至更长的整形数来保存日历时间。比如微软在Visual C++中采用了__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间(而不是通过使用32位字的time()函数),这样就可以通过该数据类型保存3001年1月1日0时0分0秒(不包括该时间点)之前的时间。
在time.h头文件中,我们还可以看到一些函数,它们都是以time_t为参数类型或返回值类型的函数:
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm * timeptr);
time_t time(time_t * timer);
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);
此外,time.h还提供了两种不同的函数将日历时间(一个用time_t表示的整数)转换为我们平时看到的把年月日时分秒分开显示的时间格式tm:
struct tm * gmtime(const time_t *timer);
struct tm * localtime(const time_t * timer);
通过查阅MSDN,我们可以知道Microsoft C/C++ 7.0中时间点的值(time_t对象的值)是从1899年12月31日0时0分0秒到该时间点所经过的秒数,而其它各种版本的Microsoft C/C++和所有不同版本的Visual C++都是计算的从1970年1月1日0时0分0秒到该时间点所经过的秒数。
4.与日期和时间相关的函数及应用
在本节,我将向大家展示怎样利用time.h中声明的函数对时间进行操作。这些操作包括取当前时间、计算时间间隔、以不同的形式显示时间等内容。
4.1 获得日历时间
我们可以通过time()函数来获得日历时间(Calendar Time),其原型为:
time_t time(time_t * timer);
如果你已经声明了参数timer,你可以从参数timer返回现在的日历时间,同时也可以通过返回值返回现在的日历时间,即从一个时间点(例如:1970年1月1日0时0分0秒)到现在此时的秒数。如果参数为空(NUL),函数将只通过返回值返回现在的日历时间,比如下面这个例子用来显示当前的日历时间:
#i nclude "time.h"
#i nclude "stdio.h"
int main(void)
{
struct tm *ptr;
time_t lt;
lt =time(NUL);
printf("The Calendar Time now is %d\n",lt);
return 0;
}
运行的结果与当时的时间有关,我当时运行的结果是:
The Calendar Time now is 1122707619
其中1122707619就是我运行程序时的日历时间。即从1970年1月1日0时0分0秒到此时的秒数。
4.2 获得日期和时间
这里说的日期和时间就是我们平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?
其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:
struct tm * gmtime(const time_t *timer);
struct tm * localtime(const time_t * timer);
其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么我用localtime()函数在中国地区获得的本地时间会比世界标准时间晚8个小时,即2005年7月30日15点18分20秒。下面是个例子:
#i nclude "time.h"
#i nclude "stdio.h"
int main(void)
{
struct tm *local;
time_t t;
t=time(NUL);
local=localtime(&t);
printf("Local hour is: %d\n",local->tm_hour);
local=gmtime(&t);
printf("UTC hour is: %d\n",local->tm_hour);
return 0;
}
运行结果是:
Local hour is: 15
UTC hour is: 7
4.3 固定的时间格式
我们可以通过asctime()函数和ctime()函数将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。返回的时间格式为:
星期几 月份 日期 时:分:秒 年\n\0
例如:Wed Jan 02 02:03:55 1980\n\0
其中\n是一个换行符,\0是一个空字符,表示字符串结束。下面是两个函数的原型:
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);
其中asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样的话,asctime()函数只是把tm结构对象中的各个域填到时间字符串的相应位置就行了,而ctime()函数需要先参照本地的时间设置,把日历时间转化为本地时间,然后再生成格式化后的字符串。在下面,如果t是一个非空的time_t变量的话,那么:
printf(ctime(&t));
等价于:
struct tm *ptr;
ptr=localtime(&t);
printf(asctime(ptr));
那么,下面这个程序的两条printf语句输出的结果就是不同的了(除非你将本地时区设为世界标准时间所在的时区):
#i nclude "time.h"
#i nclude "stdio.h"
int main(void)
{
struct tm *ptr;
time_t lt;
lt =time(NUL);
ptr=gmtime(<);
printf(asctime(ptr));
printf(ctime(<));
return 0;
}
运行结果:
Sat Jul 30 08:43:03 2005
Sat Jul 30 16:43:03 2005
4.4 自定义时间格式
我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:
size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const struct tm *timeptr
);
我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。
函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串strDest中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。
%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十进制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
如果想显示现在是几点了,并以12小时制显示,就象下面这段程序:
#i nclude “time.h”
#i nclude “stdio.h”
int main(void)
{
struct tm *ptr;
time_t lt;
char str[80];
lt=time(NUL);
ptr=localtime(<);
strftime(str,100,"It is now %I %p",ptr);
printf(str);
return 0;
}
其运行结果为:
It is now 4PM
而下面的程序则显示当前的完整日期:
#i nclude <stdio.h>
#i nclude <time.h>
void main( void )
{
struct tm *newtime;
char tmpbuf[128];
time_t lt1;
time( <1 );
newtime=localtime(<1);
strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
printf(tmpbuf);
}
运行结果:
Today is Saturday, day 30 of July in the year 2005.
4.5 计算持续时间的长度
有时候在实际应用中要计算一个事件持续的时间长度,比如计算打字速度。在第1节计时部分中,我已经用clock函数举了一个例子。Clock()函数可以精确到毫秒级。同时,我们也可以使用difftime()函数,但它只能精确到秒。该函数的定义如下:
double difftime(time_t time1, time_t time0);
虽然该函数返回的以秒计算的时间间隔是double类型的,但这并不说明该时间具有同double一样的精确度,这是由它的参数觉得的(time_t是以秒为单位计算的)。比如下面一段程序:
#i nclude "time.h"
#i nclude "stdio.h"
#i nclude "stdlib.h"
int main(void)
{
time_t start,end;
start = time(NUL);
system("pause");
end = time(NUL);
printf("The pause used %f seconds.\n",difftime(end,start));//<-
system("pause");
return 0;
}
运行结果为:
请按任意键继续. . .
The pause used 2.000000 seconds.
请按任意键继续. . .
可以想像,暂停的时间并不那么巧是整整2秒钟。其实,你将上面程序的带有“//<-”注释的一行用下面的一行代码替换:
printf("The pause used %f seconds.\n",end-start);
其运行结果是一样的。
4.6 分解时间转化为日历时间
这里说的分解时间就是以年、月、日、时、分、秒等分量保存的时间结构,在C/C++中是tm结构。我们可以使用mktime()函数将用tm结构表示的时间转化为日历时间。其函数原型如下:
time_t mktime(struct tm * timeptr);
其返回值就是转化后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行操作了,下面的例子可以计算出1997年7月1日是星期几:
#i nclude "time.h"
#i nclude "stdio.h"
#i nclude "stdlib.h"
int main(void)
{
struct tm t;
time_t t_of_day;
t.tm_year=1997-1900;
t.tm_mon=6;
t.tm_mday=1;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;
t_of_day=mktime(&t);
printf(ctime(&t_of_day));
return 0;
}
运行结果:
Tue Jul 01 00:00:01 1997
现在注意了,有了mktime()函数,是不是我们可以操作现在之前的任何时间呢?你可以通过这种办法算出1945年8月15号是星期几吗?答案是否定的。因为这个时间在1970年1月1日之前,所以在大多数编译器中,这样的程序虽然可以编译通过,但运行时会异常终止。
freebsd安装及安全设置
| ||||
vsftp配置大全