NSProcessInfo进程类

A collection of information about the current process.
当前进程的信息集合
Each process has a single, shared NSProcessInfo object, known as a process information agent.
每个进程都有一个共享的NSProcessInfo对象,称为进程信息代理
The process information agent can return information such as arguments, environment variables, host name, and process name. The processInfo class method returns the shared agent for the current process—that is, the process whose object sent the message. For example, the following line returns the NSProcessInfo object, which then provides the name of the current process:
进程信息代理可以返回参数,环境变量,主机名和进程名称等信息。 processInfo类方法返回当前进程的共享代理 - 即对象发送消息的进程。例如,以下行返回NSProcessInfo对象,然后该对象提供当前进程的名称
Listing 1
NSString *processName = [[NSProcessInfo processInfo] processName];
Note
NSProcessInfo is thread-safe in macOS 10.7 and later.
The NSProcessInfo class also includes the operatingSystem method, which returns an enum constant identifying the operating system on which the process is executing.
NSProcessInfo在macOS 10.7及更高版本中是线程安全的。 NSProcessInfo类还包括operatingSystem方法,该方法返回一个枚举常量,用于标识正在执行进程的操作系统。

NSProcessInfo objects attempt to interpret environment variables and command-line arguments in the user’s default C string encoding if they cannot be converted to Unicode as UTF-8 strings. If neither the Unicode nor C string conversion works, these values are ignored by the NSProcessInfo object.
如果NSProcessInfo对象无法作为UTF-8字符串转换为Unicode,则会尝试在用户的默认C字符串编码中解释环境变量和命令行参数。如果Unicode和C字符串转换都不起作用,则NSProcessInfo对象将忽略这些值。

实际例子
YYKit中的应用

- (int64_t)memoryTotal {
    // physicalMemor:The amount of physical memory on the computer in bytes.
    int64_t mem = [[NSProcessInfo processInfo] physicalMemory];
    if (mem < -1) mem = -1;
    return mem;
}
- (NSDate *)systemUptime {
    //The amount of time the system has been awake since the last time it was restarted.
    // 自上次重新启动以来系统处于唤醒状态的时间。
    NSTimeInterval time = [[NSProcessInfo processInfo] systemUptime];
  return [[NSDate alloc] initWithTimeIntervalSinceNow:(0 - time)];
}
 - (NSUInteger)cpuCount {
    /*
     The number of active processing cores available on the computer.
     计算机上可用的激活进程核数
     Whereas the processorCount property reports the number of advertised processing cores, the activeProcessorCount property reflects the actual number of active processing cores on the system. There are a number of different factors that may cause a core to not be active, including boot arguments, thermal throttling, or a manufacturing defect. This property value is equal to the result of entering the command sysctl -n hw.logicalcpu on the current system.
     虽然processorCount属性报告通告的进程核数,但activeProcessorCount属性反映了系统上激活进程核的实际数量。有许多不同因素可能导致核心无法激活,包括引导参数,热量限制或制造缺陷。此属性值等于在当前系统上输入命令sysctl -n hw.logicalcpu的结果。
     */
    return [NSProcessInfo processInfo].activeProcessorCount;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值