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; // the standard return type for Mach calls
host_name_port_t myhost;
kernel_version_t kversion;
host_basic_info_data_t hinfo;
mach_msg_type_number_t count;
char *cpu_type_name, *cpu_subtype_name;
vm_size_t page_size;

// get send rights to the name port for the current host
myhost = mach_host_self();

kr = host_kernel_version(myhost, kversion);
EXIT_ON_MACH_ERROR("host_kernel_version", kr);

count = HOST_BASIC_INFO_COUNT; // size of the buffer
kr = host_info(myhost, // the host name port
HOST_BASIC_INFO, // flavor
(host_info_t)&hinfo, // out structure
&count); // in/out size
EXIT_ON_MACH_ERROR("host_info", kr);

kr = host_page_size(myhost, &page_size);
EXIT_ON_MACH_ERROR("host_page_size", kr);

printf("%s\n", kversion);

// the slot_name() library function converts the specified
// cpu_type/cpu_subtype pair to a human-readable form
slot_name(hinfo.cpu_type, hinfo.cpu_subtype, &cpu_type_name,
&cpu_subtype_name);

printf("cpu %s (%s, type=0x%x subtype=0x%x "
"threadtype=0x%x)\n", cpu_type_name, cpu_subtype_name,
hinfo.cpu_type, hinfo.cpu_subtype, hinfo.cpu_threadtype);
printf("max_cpus %d\n", hinfo.max_cpus);
printf("avail_cpus %d\n", hinfo.avail_cpus);
printf("physical_cpu %d\n", hinfo.physical_cpu);
printf("physical_cpu_max %d\n", hinfo.physical_cpu_max);
printf("logical_cpu %d\n", hinfo.logical_cpu);
printf("logical_cpu_max %d\n", hinfo.logical_cpu_max);
printf("memory_size %u MB\n", (hinfo.memory_size >> 20));
printf("max_mem %llu MB\n", (hinfo.max_mem >> 20));
printf("page_size %u bytes\n", page_size);

exit(0);
}

haidragondeMacBook-Air:6-1 haidragon$ gcc -Wall -o host_basic_info host_basic_info.c
host_basic_info.c:55:43: warning: format specifies type ‘unsigned int‘ but the argument has type
      ‘vm_size_t‘ (aka ‘unsigned long‘) [-Wformat]
    printf("page_size        %u bytes\n", page_size);
                             ~~           ^~~~~~~~~
                             %lu
1 warning generated.
haidragondeMacBook-Air:6-1 haidragon$ ls
host_basic_info     host_basic_info.c
haidragondeMacBook-Air:6-1 haidragon$ ./host_basic_info
Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64
cpu              x86_64h (Intel x86-64h Haswell, type=0x7 subtype=0x8 threadtype=0x1)
max_cpus         4
avail_cpus       4
physical_cpu     2
physical_cpu_max 2
logical_cpu      4
logical_cpu_max  4
memory_size      2048 MB
max_mem          8192 MB
page_size        4096 bytes
haidragondeMacBook-Air:6-1 haidragon$ 

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

时间: 2024-07-31 11:07:40

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

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

利用堡垒主机获取防火墙里面主机信息

#!/usr/bin/env python # coding=utf-8 import paramiko import os,sys,time # define baolei blip = "188.188.22.225" bluser = "root" blpassword = "1233434" # define server info hostname = "112.74.96.65" username = "

Python 之 获取Host Esxi 主机信息

代码 #Author Kang #获取ESXI主机的CPU和内存使用情况,并统计百分比 import ssl import pysphere from pysphere import VIServer,MORTypes,VIProperty from pysphere.resources import VimService_services as VI ssl._create_default_https_context = ssl._create_unverified_context host_

Mac OS X 配置 Apache+Mysql+PHP 详细教程

网上的教程已经有很多,这里简洁的记录一下.以 Mac OS X Mavericks 10.9.X 为例. 先附上如何进入指定目录文件夹,按键盘 Command + Shift + G ,然后输入指定目录名即可. 工具/原料 以 Mac OS X Mavericks 10.9.X 为例. 一.启动 Apache 1 Mac OS X 系统已经集成了 Apache+PHP 环境,依次进入“系统偏好设置 -> 共享” ,开启“Web 共享”,就可以打开 Apache. 但新版的 Mac OS X 中,

MAC OS开发之从入门到崩溃(一)

本文目标:通过xcode,创建一个Mac app程序.点击按钮,弹出Hello World窗口,其效果如下. 我们可以通过4个步骤来实现如上效果. 1.创建一个MAC app工程项目. 2.布局"按钮"控件 3.建立相应"连接" 4.编写弹窗代码. 一.创建Mac App工程项目 1.打开xcode,选择Create a new Xcode project. 2.找到mac os选项,选择Cocoa App 3.填写相关信息. 4.选择文件夹,点击create. 至

在Mac OS上安装Vagrant和Docker的教程

转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/128.html?1455808640 当听到很多人在说Docker是多么多么的棒,很多新潮的孩子都在使用它时,我决定在我的开发环境上也来尝试下.在下面的这篇文章中,我将讲解在Mac OS X怎样建立Postgres,Elasticsearch和Redis. 什么是Docker Docker用轻量容器把一个APP从它运行的OS中隔离开.它把APP放入到一个孤立的盒子中,对外

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