现在的位置: 首页 > 综合 > 正文

netbsd源码分析(2)

2013年03月14日 ⁄ 综合 ⁄ 共 2366字 ⁄ 字号 评论关闭

/*
 * Structure returned by gettimeofday(2) system call,

 * and used in other calls.

 */

系统时间timeval的结构由2部分组成,一部分是秒tv_sec,另一个部分是微秒tv_usec;

struct timeval {
    time_t        tv_sec;        /* seconds */
    suseconds_t    tv_usec;    /* and microseconds */

};

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/

POSIX.1b定义了另一个类似于timeval的系统时间结构纳秒

POSIX 标准的实时部分(POSIX.1b),POSIX.1b-1993标准详细定义了UNIX系统中关于实时性的特征。在标准中主要定义了调度的优先权,内存锁,实时信号,改良的IPC和定时器和一些其他的功能。

POSIX.1b-1993 defines in addition to POSIX.1-1990 the following new
concepts and functions:


Improved Signals
----------------

POSIX.1b adds a new class of signals. These have the following new
features:

  - there are much more user specified signals now, not only SIGUSR1
    and SIGUSR2.

  - The additional POSIX.1b signals can now carry a little bit data (a
    pointer or an integer value) that can be used to transfer to the
    signal handler information about why the signal has been caused.

  - The new signals are queued, which means that if several signals of
    the same type arrive before the signal handler is called, all of
    them will be delivered.

  - POSIX.1b signals have a well-defined delivery order, i.e. you can
    work now with signal priorities.

  - A new function sigwaitinfo() allows to wait on signals and to
    continue quickly after the signal arrived with program execution
    without the overhead of calling a signal handler first.

The new queued signals are a necessary prerequisite for the
implementation of the POSIX.1b asynchronous I/O interface (see below).
They might also provide a good interface for delivering hardware
interrupts to user processes.

New functions for signals are:

  sigwaitinfo(), sigtimedwait(), sigqueue().
0.000 001 微秒 = 1皮秒
0.001 微秒 = 1纳秒
1,000 微秒 = 1毫秒
1,000,000 微秒 = 1

由2部分组成,一部分是秒 tv_sec,另一个部分是纳秒十亿分之一秒)tv_nsec

/*
 * Structure defined by POSIX.1b to be like a timeval.
 */
struct timespec {
    time_t    tv_sec;        /* seconds */
    long    tv_nsec;    /* and nanoseconds */
};

需要有一个操作完成这2种格式的转换

#if defined(_NETBSD_SOURCE)

系统时间转实时扩展标准
#define    TIMEVAL_TO_TIMESPEC(tv, ts) do {                \
    (ts)->tv_sec = (tv)->tv_sec;                    \
    (ts)->tv_nsec = (tv)->tv_usec * 1000;                \
} while (/*CONSTCOND*/0)

上面主要是操作微秒部分,因为要将其转为纳秒,下面是类似的操作,将纳秒转为微秒。

1纳秒 =0.001
微秒

#define    TIMESPEC_TO_TIMEVAL(tv, ts) do {                \
    (tv)->tv_sec = (ts)->tv_sec;                    \
    (tv)->tv_usec = (suseconds_t)(ts)->tv_nsec / 1000;        \
} while (/*CONSTCOND*/0)

只所以这么写

do

{

..

}while(/*...*/0)

第一,源代码只循环一次,

第二,可以加上灵活的条件,在什么情况下再重新尝试一次

或者可以加上灵活的条件,在什么情况下再重新尝试一次

接下来是时区

struct timezone {
        int     tz_minuteswest; /* minutes west of Greenwich */
        int     tz_dsttime;     /* type of dst correction */
};

时区已经不再用了,为提供向后兼容性,保留下来

抱歉!评论已关闭.