mac os x在Mach中获取时钟基本属性和时间值

// host_clock.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <mach/mach.h>
#include <mach/clock.h>

#define OUT_ON_MACH_ERROR(msg, retval) if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; }

int
main()
{
kern_return_t kr;
host_name_port_t myhost;
clock_serv_t clk_system, clk_calendar, clk_realtime;
natural_t attribute[4];
mach_msg_type_number_t count;
mach_timespec_t timespec;
struct timeval t;

myhost = mach_host_self();

// Get a send right to the system clock‘s name port
kr = host_get_clock_service(myhost, SYSTEM_CLOCK,
(clock_serv_t *)&clk_system);
OUT_ON_MACH_ERROR("host_get_clock_service", kr);

// Get a send right to the calendar clock‘s name port
kr = host_get_clock_service(myhost, CALENDAR_CLOCK,
(clock_serv_t *)&clk_calendar);
OUT_ON_MACH_ERROR("host_get_clock_service", kr);

// Get a send right to the real-time clock‘s name port
kr = host_get_clock_service(myhost, REALTIME_CLOCK,
(clock_serv_t *)&clk_realtime);
OUT_ON_MACH_ERROR("host_get_clock_service", kr);

//// System clock
count = sizeof(attribute)/sizeof(natural_t);
// Get the clock‘s resolution in nanoseconds
kr = clock_get_attributes(clk_system, CLOCK_GET_TIME_RES,
(clock_attr_t)attribute, &count);
OUT_ON_MACH_ERROR("clock_get_attributes", kr);
// Get the current time
kr = clock_get_time(clk_system, &timespec);
OUT_ON_MACH_ERROR("clock_get_time", kr);
printf("System clock : %u s + %u ns (res %u ns)\n",
timespec.tv_sec, timespec.tv_nsec, attribute[0]);

//// Real-time clock
count = sizeof(attribute)/sizeof(natural_t);
kr = clock_get_attributes(clk_realtime, CLOCK_GET_TIME_RES,
(clock_attr_t)&attribute, &count);
OUT_ON_MACH_ERROR("clock_get_attributes", kr);
kr = clock_get_time(clk_realtime, &timespec);
OUT_ON_MACH_ERROR("clock_get_time", kr);
printf("Realtime clock: %u s + %u ns (res %u ns)\n",
timespec.tv_sec, timespec.tv_nsec, attribute[0]);

//// Calendar clock
count = sizeof(attribute)/sizeof(natural_t);
kr = clock_get_attributes(clk_calendar, CLOCK_GET_TIME_RES,
(clock_attr_t)&attribute, &count);
OUT_ON_MACH_ERROR("clock_get_attributes", kr);
kr = clock_get_time(clk_calendar, &timespec);
gettimeofday(&t, NULL);
OUT_ON_MACH_ERROR("clock_get_time", kr);
printf("Calendar clock: %u s + %u ns (res %u ns)\n",
timespec.tv_sec, timespec.tv_nsec, attribute[0]);

printf("gettimeofday : %ld s + %d us\n", t.tv_sec, t.tv_usec);

out:
// Should deallocate ports here for cleanliness
mach_port_deallocate(mach_task_self(), myhost);
mach_port_deallocate(mach_task_self(), clk_calendar);
mach_port_deallocate(mach_task_self(), clk_system);
mach_port_deallocate(mach_task_self(), clk_realtime);

exit(0);
}

haidragondeMacBook-Air:6-2 haidragon$ ls
host_clock.c
haidragondeMacBook-Air:6-2 haidragon$ gcc -Wall -o host_clock host_clock.c
host_clock.c:28:5: warning: variable ‘clk_calendar‘ is used uninitialized whenever ‘if‘ condition is
      true [-Wsometimes-uninitialized]
    OUT_ON_MACH_ERROR("host_get_clock_service", kr);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
host_clock.c:10:9: note: expanded from macro ‘OUT_ON_MACH_ERROR‘
    if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; }
        ^~~~~~~~~~~~~~~~~~
host_clock.c:78:44: note: uninitialized use occurs here
    mach_port_deallocate(mach_task_self(), clk_calendar);
                                           ^~~~~~~~~~~~
host_clock.c:28:5: note: remove the ‘if‘ if its condition is always false
    OUT_ON_MACH_ERROR("host_get_clock_service", kr);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
host_clock.c:10:5: note: expanded from macro ‘OUT_ON_MACH_ERROR‘
    if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; }
    ^
host_clock.c:17:52: note: initialize the variable ‘clk_calendar‘ to silence this warning
    clock_serv_t           clk_system, clk_calendar, clk_realtime;
                                                   ^
                                                    = 0
host_clock.c:33:5: warning: variable ‘clk_realtime‘ is used uninitialized whenever ‘if‘ condition is
      true [-Wsometimes-uninitialized]
    OUT_ON_MACH_ERROR("host_get_clock_service", kr);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
host_clock.c:10:9: note: expanded from macro ‘OUT_ON_MACH_ERROR‘
    if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; }
        ^~~~~~~~~~~~~~~~~~
host_clock.c:80:44: note: uninitialized use occurs here
    mach_port_deallocate(mach_task_self(), clk_realtime);
                                           ^~~~~~~~~~~~
host_clock.c:33:5: note: remove the ‘if‘ if its condition is always false
    OUT_ON_MACH_ERROR("host_get_clock_service", kr);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
host_clock.c:10:5: note: expanded from macro ‘OUT_ON_MACH_ERROR‘
    if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; }
    ^
host_clock.c:28:5: warning: variable ‘clk_realtime‘ is used uninitialized whenever ‘if‘ condition is
      true [-Wsometimes-uninitialized]
    OUT_ON_MACH_ERROR("host_get_clock_service", kr);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
host_clock.c:10:9: note: expanded from macro ‘OUT_ON_MACH_ERROR‘
    if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; }
        ^~~~~~~~~~~~~~~~~~
host_clock.c:80:44: note: uninitialized use occurs here
    mach_port_deallocate(mach_task_self(), clk_realtime);
                                           ^~~~~~~~~~~~
host_clock.c:28:5: note: remove the ‘if‘ if its condition is always false
    OUT_ON_MACH_ERROR("host_get_clock_service", kr);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
host_clock.c:10:5: note: expanded from macro ‘OUT_ON_MACH_ERROR‘
    if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); goto out; }
    ^
host_clock.c:17:66: note: initialize the variable ‘clk_realtime‘ to silence this warning
    clock_serv_t           clk_system, clk_calendar, clk_realtime;
                                                                 ^
                                                                  = 0
3 warnings generated.
haidragondeMacBook-Air:6-2 haidragon$ ls
host_clock  host_clock.c
haidragondeMacBook-Air:6-2 haidragon$ ./host_clock
System clock  : 20035 s + 406693924 ns (res 10000000 ns)
Realtime clock: 20035 s + 406780101 ns (res 10000000 ns)
Calendar clock: 1562077197 s + 706757784 ns (res 10000000 ns)
gettimeofday  : 1562077197 s + 706766 us
haidragondeMacBook-Air:6-2 haidragon$ 

原文地址:https://blog.51cto.com/haidragon/2416418

时间: 2024-10-13 14:45:13

mac os x在Mach中获取时钟基本属性和时间值的相关文章

mac os x使用Mach调用获取基本的主机信息

// host_basic_info.c #include <stdio.h> #include <stdlib.h> #include <mach/mach.h> #define EXIT_ON_MACH_ERROR(msg, retval) if (kr != KERN_SUCCESS) { mach_error(msg ":" , kr); exit((retval)); } int main() { kern_return_t kr; //

在Mac OS X 10.8中配置Apache + PHP + MySQL

在Mac OS X 10.8中配置Apache+PHP+MySQL的内容包括: 配置Apache 配置PHP 安装MySQL 配置PHPAdmin 设置数据库默认字符集 一. 配置Apache 1. 启动Apache 打开终端,输入: sudo apachectl start 打开浏览器,输入: http://localhost 应该可以看到”It works!“的页面,该页面位于/Library/WebServer/Documents/目录下,这是Apache的默认根目录. 2. 配置用户访问

方便mac os 10.9系统中phpstorm配置php运行环境

自己安装php,不用mac安装,这样就有php开发环境了. 安装很简单,直接运行一个命令, 需要几分钟,请慢慢等待. curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5 (注5.5是php版本可以是5.6等等) 新安装的php目录是/usr/local/php5/bin,接下来在PhpStorm中设置新安装的php解释器: PhpStorm->Preferences->PHP 会来到解释器设置页 设置我们安装的php解释器:/usr/

sql 中获取最后生成的标识值 IDENT_CURRENT ,@@IDENTITY ,SCOPE_IDENTITY 的用法和区别

原文:sql 中获取最后生成的标识值 IDENT_CURRENT ,@@IDENTITY ,SCOPE_IDENTITY 的用法和区别 IDENT_CURRENT 返回为任何会话和任何作用域中的指定表最后生成的标识值. 语法 IDENT_CURRENT('table_name') 参数 table_name 是将要返回其标识值的表的名称.table_name 的数据类型为 varchar,没有默认值. 返回类型 sql_variant 注释 IDENT_CURRENT 类似于 Microsoft

如何在asp.net中获取GridView隐藏列的值?

在阅读本文之前,我获取gridview某行某列的值一般做法是这样的:row.Cells[3].Text.ToString().有点傻瓜呵呵 在Asp.net 2.0中增加了一个新的数据绑定控件:GridView,其目的用来取代Asp.net1.x中的DataGrid控件.获取GridView中的某列值的方法为   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)    {        stri

超时时间已到。在从池中获取连接之前超时时间已过,连接池达到最大

NET网站出错信息如下: 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.InvalidOperationException: 超时时间已到.超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小. 源错误: 执行当前 Web 请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息. “/”应用程序中的服务

关于Artdialog插件中获取内部表单元素值的使用心得

在开发中既需要artdialog插件的美观大方,又需要自定义功能,所以就出现了以下情况(废话不多说,转入正题) 前台代码: 1 <input type="button" class="ontest" value="免费询盘"/> 2 <div style="display:none;clear:both;"> 3 <table> 4 <tr> 5 <td>姓名:<

java中获取系统的当前时间

转自:http://www.cnblogs.com/Matrix54/archive/2012/05/01/2478158.html 一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowString {public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFor

webBrowser控件中获取元素 的class 属性值

html 代码如下: <TR id="t030006" class="sr plus selected"> <TD><INPUT id=cvrgNo030006 value=030006 readOnly CHECKED type=checkbox jQuery1456994811776="96"></TD> <TD>车辆损失险 </TD> <TD style=&quo