linux 时间&定时器 介绍
1.时间表示
在程序当中,我们经常要输出系统当前的时间,比如我们使用date命令的输出结果.这个时候我们可以使用下面两个函数:
#include
time_t time(time_t *tloc);
char *ctime(const time_t *clock);
time函数返回从1970年1月1日0点以来的秒数.存储在time_t结构之中.不过这个函数的返回值对于我们来说没有什么实际意义.这个时候我们使用第二个函数将秒数转化为字符串. 这个函数的返回类型是固定的:一个可能值为.Thu Dec7 14:58:59 2000 这个字符串的长度是固定的为26.
2.时间的测量
有时候我们要计算程序执行的时间.比如我们要对算法进行时间分析.这个时候可以使用下面这个函数. #include
int gettimeofday(struct timeval *tv,struct timezone *tz);
strut timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};
gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替.
3.计时器的使用
3.1、alarm
-------------------------------------------
如果不要求很精确的话,用alarm()和signal()就够了
unsigned int alarm(unsigned int seconds)
函数说明: alarm()用来设置信号SIGALRM在经过参数seconds指定的秒数后传送给目前的进程。如果参数seconds为0,则之前设置的闹钟会被取消,并将剩下的时间返回。
返回值: 返回之前闹钟的剩余秒数,如果之前未设闹钟则返回0。
alarm()执行后,进程将继续执行,在后期(alarm以后)的执行过程中将会在seconds秒后收到信号SIGALRM并执行其处理函数。
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sigalrm_fn(int sig)
{
printf("alarm!\n");
alarm(2);
return;
}
int main(void)
{
signal(SIGALRM, sigalrm_fn);
alarm(1);
while(1) pause();
}
3.2、setitimer()
-------------------------------------------
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));
setitimer()比alarm功能强大,支持3种类型的定时器:
ITIMER_REAL :&nb
相关文档:
http://blog.csdn.net/noah1987/archive/2008/10/21/3118934.aspx
本程序可以读取.wav文件,然后进行播放。
使用前,请确认您是否安装音频驱动。
确认方法:cat /etc/sndstat,如果显示无此设备,则没有安装驱动。
安装驱动很简单,到oss.com上下载音频驱动,然后按照网上的教程进行就可以了。
源代码如下:
#include ......
本程序可以读取.wav文件,然后进行播放。
确认方法:cat /etc/sndstat,如果显示无此设备,则没有安装驱动。
#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ioctl.h>#include <stdlib.h>#include <stdio.h>#include&nbs ......
前 言
随着超大规模集成电路的发展,计算机处理器技术不断提高,计算机芯片的处理能力越来越强,体积越来越小,计算机技术应用到生活的方方面面。与人们日常生活打交道最多的就是嵌入式系统,从目前广泛使用的手机、MP3播放器到家用电器,嵌入式系统的应用无处不在。嵌入式系统的开发占整个计算机系统开发的比重也越 ......
linux—select详解
select系统调用时用来让我们的程序监视多个文件句柄的状态变化的。程序会停在select这里等待,直到被监视的文件句柄有一个或多个发生了状态改变。
关于文件句柄,其实就是一个整数,通过socket函数的声明就明白了:
int socket(int domain, int type, int protocol);
我们最熟悉的句柄是0、1、2 ......
Linux initial RAM disk (initrd) overview
Learn about its anatomy, creation, and use in the Linux boot process
M. Tim Jones
(mtj@mtjones.com
), Consultant Engineer, Emulex
M. Tim Jones is an embedded software architect and the author of GNU/Linux Application Programming
, AI Application Prog ......