msec是毫秒; usec是微秒

本文详细解释了计算机网络中常见的几个时间单位:毫秒(ms)、微秒(us)与纳秒(ns),并介绍了它们之间的换算关系。

使用ping命令时,经常出来msec和usec。
msec是毫秒; usec是微秒

 

ms意思是“毫秒”,1秒=1000毫秒
us应为μs,是指“微秒”,1毫秒=1000微秒,即1秒=1000,000微秒。
ns是“纳秒”,1微秒=1000纳秒, 即1秒=1000,000,000纳秒

解析: int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) { assert(a); assert(b); if (a->tv_sec < b->tv_sec) return -1; if (a->tv_sec > b->tv_sec) return 1; if (a->tv_usec < b->tv_usec) return -1; if (a->tv_usec > b->tv_usec) return 1; return 0; } AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) { assert(a); assert(b); if (avahi_timeval_compare(a, b) < 0) return - avahi_timeval_diff(b, a); return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec; } struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) { AvahiUsec u; assert(a); u = usec + a->tv_usec; if (u < 0) { a->tv_usec = (long) (1000000 + (u % 1000000)); a->tv_sec += (long) (-1 + (u / 1000000)); } else { a->tv_usec = (long) (u % 1000000); a->tv_sec += (long) (u / 1000000); } return a; } AvahiUsec avahi_age(const struct timeval *a) { struct timeval now; assert(a); gettimeofday(&now, NULL); return avahi_timeval_diff(&now, a); } struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) { assert(tv); gettimeofday(tv, NULL); if (msec) avahi_timeval_add(tv, (AvahiUsec) msec*1000); if (jitter) { static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int last_rand; static time_t timestamp = 0; time_t now; int r; now = time(NULL); pthread_mutex_lock(&mutex); if (now >= timestamp + 10) { timestamp = now; last_rand = rand(); } r = last_rand; pthread_mutex_unlock(&mutex); /* We use the same jitter for 10 seconds. That way our * time events elapse in bursts which has the advantage that * packet data can be aggregated better */ avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*r/(RAND_MAX+1.0))); } return tv; }
10-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值