mac os x使用Mach调用设置时间警报(回调)

// host_alarm.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; }

// Structure for the IPC message we will receive from the clock
typedef struct msg_format_recv_s {
mach_msg_header_t header;
int data;
mach_msg_trailer_t trailer;
} msg_format_recv_t;

int
main()
{
kern_return_t kr;
clock_serv_t clk_system;
mach_timespec_t alarm_time;
clock_reply_t alarm_port;
struct timeval t1, t2;
msg_format_recv_t message;
mach_port_t mytask;

// The C library optimized this call by returning the task port‘s value
// that it caches in the mach_task_self_ variable
mytask = mach_task_self();

kr = host_get_clock_service(mach_host_self(), SYSTEM_CLOCK,
(clock_serv_t *)&clk_system);
OUT_ON_MACH_ERROR("host_get_clock_service", kr);

// Let us set the alarm to ring after 2.5 seconds
alarm_time.tv_sec = 2;
alarm_time.tv_nsec = 500;

// Allocate a port (specifically, get receive right for the new port)
// We will use this port to receive the alarm message from the clock
kr = mach_port_allocate(
mytask, // the task acquiring the port right
MACH_PORT_RIGHT_RECEIVE, // type of right
&alarm_port); // task‘s name for the port right
OUT_ON_MACH_ERROR("mach_port_allocate", kr);

gettimeofday(&t1, NULL);

// Set the alarm
kr = clock_alarm(clk_system, // the clock to use
TIME_RELATIVE, // how to interpret alarm time
alarm_time, // the alarm time
alarm_port); // this port will receive the alarm message
OUT_ON_MACH_ERROR("clock_alarm", kr);

printf("Current time %ld s + %d us\n"
"Setting alarm to ring after %d s + %d ns\n",
t1.tv_sec, t1.tv_usec, alarm_time.tv_sec, alarm_time.tv_nsec);

// Wait to receive the alarm message (we will block here)
kr = mach_msg(&(message.header), // the message buffer
MACH_RCV_MSG, // message option bits
0, // send size (we are receiving, so 0)
message.header.msgh_size,// receive limit
alarm_port, // receive right
MACH_MSG_TIMEOUT_NONE, // no timeout
MACH_PORT_NULL); // no timeout notification port
// We should have received an alarm message at this point
gettimeofday(&t2, NULL);
OUT_ON_MACH_ERROR("mach_msg", kr);

if (t2.tv_usec < t1.tv_usec) {
t1.tv_sec += 1;
t1.tv_usec -= 1000000;
}

printf("\nCurrent time %ld s + %d us\n", t2.tv_sec, t2.tv_usec);
printf("Alarm rang after %ld s + %d us\n", (t2.tv_sec - t1.tv_sec),
(t2.tv_usec - t1.tv_usec));

out:
mach_port_deallocate(mytask, clk_system);

// Release user reference for the receive right we created
mach_port_deallocate(mytask, alarm_port);

exit(0);
}

gcc -Wall -o host_alarm host_alarm.c

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

时间: 2024-08-29 23:27:34

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 下查看和设置JAVA_HOME

原文链接 : http://blog.csdn.net/done58/article/details/51138057 1, 查看Java版本 打开Mac电脑,查看JAVA版本,打开终端Terminal,通过命令行查看笔者的java版本:: [html] view plain copy bogon:~ donny$ java -version java version "1.7.0_71" Java(TM) SE Runtime Environment (build 1.7.0_71-

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);

mac os下得pycharm怎么设置mercurial?

捣鼓了一会儿,最终搞定了. 先把链接贴上来:https://www.jetbrains.com/pycharm/help/mercurial.html 如果你发现你的pycharm在设置mercurial时是这样: 提示找不到hg,怎么办呢?从官网上的话来看,如果你跟我一样是默认安装的话设置成这样就好啦 或者试试另一个/opt/local/bin/hg If you followed the standard installation procedure, the default locatio

MAC OS下锁屏快捷键设置

首先在应用程序中找到Automator打开,选择文稿类型为服务,点击选取.步骤如下图: 在搜索中输入run,双击Run Shell Script,如下图: 将服务收到选定的从文本选为没有输入,并在Shell脚本框中输入: '/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession' -suspend 展示如下图: 在顶部菜单栏中文件-->存储,命名为Lock Screen,点击存储,如下图: 然

mac os x使用Mach获取调试和虚拟内存统计信息

// host_alarm.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);

Mac OS X 背后的故事

Mac OS X 背后的故事 作者: 王越  来源: <程序员>  发布时间: 2013-01-22 10:55  阅读: 25840 次  推荐: 49   原文链接   [收藏] 作者王越,美国宾夕法尼亚大学计算机系研究生,中国著名 TeX 开发者,非著名 OpenFOAM 开发者. Mac OS X 背后的故事(一)力挽狂澜的Ellen Hancock Mac OS X 背后的故事(二)Linus Torvalds的短视 Mac OS X 背后的故事(三)Mach之父Avie Tevan

Mac OS 10.10 Yosemite正式版怎么升级 升级教程

苹果在2014年10月17号凌晨一点召开了新品发布会,推出了新的 iPad.iMac 产品,以及大家一直所期盼的 Mac OS 10.10 正式版系统.个人是从 Mac OS 10.10 的第一个测试版开始使用,一直到前几天的 GM3.0 版本.对于使用 Mac 电脑的小伙伴而言,新系统采用了全新的扁平风格,并且还增加了好多新功能,比如支持 与 iOS 设备进行 AirDrop 分享,支持互连互通功能,可以在 Mac 上打电话等.这里简单介绍下怎么升级更新 Mac OS 10.10 Yosemi

VMware 12安装虚拟机Mac OS X 10.10使用小技巧(虚拟机Mac OS X 10.10时间设置,虚拟机Mac OS X 10.10通过代理上网,Mac OS X 10.10虚拟机优化,VMware虚拟机相互复制)

1:修改Mac OS 系统时间 2:Mac OS系统 通过代理上网 VMware 12安装Mac OS X 10.10虚拟机优化心得 虚拟显卡硬伤,所以必须要优化下才能用,优化的原则就是能精简的精简,能关特效的就关,不要duang,下面是一些优化设置: 1,从Dock上移除Dashboard 每次不小心点到这个,必然卡机,毛玻璃效果虚拟显卡可吃不消,果断移到垃圾桶,要开应用多的是方法. 2,禁用各种华而不实的透明窗口和动画效果 OS X Yosemite 中大量采用透明菜单.窗口和标题栏,这需要