参考链接:

https://www.cnblogs.com/watchdatalearn2012620/p/3182477.html

https://segmentfault.com/q/1010000009396800

NSProcessInfo可以获得当前进程的信息。获得所有活动进程信息可以尝试使用下面的方法。

进程的信息可以通过ps命令得到也可以通过sysctl方法得到。 但是我总是不能获取进程的流量信息,关于这一点很纠结,现在的想法就是如果能够获取进程的网络端口,然后对端口进行监听,统计其流量,但是如何能够获取进程的网络端口? 在linux中可以通过netstat命令来查询进程和其对应的端口,但是在macos中netstat命令和linux中不同,并不能实现这一功能(我没找到,但愿是能够的)。 由于本人学习objective-c不久,不知道是否有这样的api,如果你有什么好的方法可以和我联系。 以下是两种方法的代码:


- (void)processListWithPS
{
_procList = [[NSMutableArray alloc] init]; FILE *fp = popen("ps -eo start,user,pid,pcpu,vsz,rss,etime,utime,stime,msgsnd,msgrcv", "r");
if (fp)
{
char line[4096] = {0};
int row = 0;
while (line == fgets(line, 4096, fp))
{
row++;
if (row > 1)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; char start[20]; //进程开始时间
char user[50]; //拥有进程用户名
char pid[10]; //进程id
char cpu[10]; //进程占用cpu率
char vsz[10]; //vss,虚拟内存
char rss[10]; //rss,物理内存
char etime[20]; //进程持续时间
char utime[20]; //用户占用进程时间
char stime[20]; //系统占用进程时间 sscanf(line, "%s %s %s %s %s %s %s %s %s",
start, user, pid, cpu, vsz, rss, etime, utime, stime); NSString *procStart = [NSString stringWithFormat:@"%s", start];
NSString *procUser = [NSString stringWithFormat:@"%s", user];
NSString *procPid = [NSString stringWithFormat:@"%s", pid];
NSString *procCpu = [NSString stringWithFormat:@"%s", cpu];
NSString *procVss = [NSString stringWithFormat:@"%s", vsz];
NSString *procRss = [NSString stringWithFormat:@"%s", rss];
NSString *procETime = [NSString stringWithFormat:@"%s", etime];
NSString *procUtime = [NSString stringWithFormat:@"%s", utime];
NSString *procStime = [NSString stringWithFormat:@"%s", stime]; ProcessInfo *proc = [[ProcessInfo alloc] init];
proc.startTime = procStart;
proc.user = procUser;
proc.procID = procPid;
proc.cpuRate = [procCpu floatValue];
proc.vss = [procVss integerValue];
proc.rss = [procRss integerValue];
proc.usedTime = procETime;
proc.utime = procUtime;
proc.stime = procStime;
proc.upFlow = [procMsgsnd integerValue];
proc.downFlow = [procMsgrcv integerValue]; [_procList addObject:proc];
[pool release];
}
}
pclose(fp);
}
}
 
 
//返回所有正在运行的进程的 id,name,占用cpu,运行时间
//使用函数int sysctl(int *, u_int, void *, size_t *, void *, size_t)

- (NSArray *)runningProcesses
{
//指定名字参数,按照顺序第一个元素指定本请求定向到内核的哪个子系统,第二个及其后元素依次细化指定该系统的某个部分。
//CTL_KERN,KERN_PROC,KERN_PROC_ALL 正在运行的所有进程
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL ,0}; size_t miblen = 4;
//值-结果参数:函数被调用时,size指向的值指定该缓冲区的大小;函数返回时,该值给出内核存放在该缓冲区中的数据量
//如果这个缓冲不够大,函数就返回ENOMEM错误
size_t size;
//返回0,成功;返回-1,失败
int st = sysctl(mib, miblen, NULL, &size, NULL, 0); struct kinfo_proc * process = NULL;
struct kinfo_proc * newprocess = NULL;
do
{
size += size / 10;
newprocess = realloc(process, size);
if (!newprocess)
{
if (process)
{
free(process);
process = NULL;
}
return nil;
} process = newprocess;
st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM); if (st == 0)
{
if (size % sizeof(struct kinfo_proc) == 0)
{
int nprocess = size / sizeof(struct kinfo_proc);
if (nprocess)
{
NSMutableArray * array = [[NSMutableArray alloc] init];
for (int i = nprocess - 1; i >= 0; i--)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
NSString * proc_CPU = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_estcpu];
double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec;
NSString * proc_useTiem = [[NSString alloc] initWithFormat:@"%f",t]; //NSLog(@"process.kp_proc.p_stat = %c",process.kp_proc.p_stat); NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:processID forKey:@"ProcessID"];
[dic setValue:processName forKey:@"ProcessName"];
[dic setValue:proc_CPU forKey:@"ProcessCPU"];
[dic setValue:proc_useTiem forKey:@"ProcessUseTime"]; [processID release];
[processName release];
[proc_CPU release];
[proc_useTiem release];
[array addObject:dic];
[dic release]; [pool release];
} free(process);
process = NULL;
//NSLog(@"array = %@",array); return array;
}
}
} return nil;
}

如何获取MAC的进程数的更多相关文章

  1. curl命令,curl实现post,curl监控网页shell脚本,curl多进程实现并控制进程数,

    cURL > Docs > Tutorial:  http://curl.haxx.se/docs/httpscripting.html 下载单个文件,默认将输出打印到标准输出中(STDO ...

  2. php-fpm进程数优化

    php-fpm未优化网友反映的问题 1.最近将Wordpress迁移至阿里云.由于自己的服务器是云服务器,硬盘和内存都比较小,所以内存经常不够使,通过ps ax命令查看后,发现启动php-fpm进程数 ...

  3. nginx和fpm的进程数配置和502,504错误

    502 和 php-fpm.conf 1.php-cgi进程数不够用.php执行时间长,导致没有空闲进程处理新请求. 2.php-cgi进程死掉.php-fpm超时时间短,当前进程执行超时关闭连接. ...

  4. php-fpm进程数优化方法

    原文地址:https://www.douban.com/note/315222037/ 背景最近将Wordpress迁移至阿里云.由于自己的服务器是云服务器,硬盘和内存都比较小,所以内存经常不够使,通 ...

  5. c# 获取MAC IP TCP列表

    转载自baidu:http://hi.baidu.com/jackeyrain/item/ff94efcfd5cf3a3099b498e9 namespace Public { public clas ...

  6. PHP-FPM进程数的设定

    近日,服务器出现异常,网站不能正常访问.经排查是php的问题. 在重启php-fpm时,恢复正常.1分钟之后又出现故障.查看php日志文件 /usr/local/php/var/log 后提示 WAR ...

  7. 使用 SendARP 获取 MAC 地址(使用SendARP API函数,很多相关文章)

    ARP 协议地址解析协议(ARP)是通过解析网路层地址来找寻数据链路层地址的一个在网络协议包中极其重要的网络传输协议.ARP 最初在 1982 年的 RFC 826 中提出并纳入互联网标准 STD 3 ...

  8. linux上限制用户进程数、cpu占用率、内存使用率

    限制进程CPU占用率的问题,给出了一个shell脚本代码如下: renice +10 `ps aux | awk '{ if ($3 > 0.8 && id -u $1 > ...

  9. 【转载】获取MAC地址方法大全

    From:http://blog.csdn.net/han2814675/article/details/6223617 Windows平台下用C++代码取得机器的MAC地址并不是一件简单直接的事情. ...

随机推荐

  1. RAID阵列搭建

    RAID0 2个或2个以上磁盘,称为条带卷,无容错,可提高读写效率,其中一个磁盘损坏,所有文件不可读磁盘大小尽量统一,或者以最小的空间为标准,可用空间=N*min RAID1 2个或2个磁盘以上,称为 ...

  2. tomcat关闭钩子

    转 http://501565246-qq-com.iteye.com/blog/1733575 21,tomcat关闭钩子 博客分类: tomcat   在很多环境下,在关闭应用程序的时候需要做一些 ...

  3. 标准C++(4)继承

    一.继承的作用 若A类继承了B类,可以使A类获得B类中的部分成员变量和成员函数,这能使程序员在已有类的基础上重新定义新的类.继承是类的重要特性,当A类继承了B类,我们称A类为派生类或子类,B类为基类或 ...

  4. 在windows7 32ibt安装MongoDB数据库的方法及连接失败解决方案

    参考 https://www.cnblogs.com/cnblogs-jcy/p/6734889.html http://yunkus.com/mongodb-install-config-in-wi ...

  5. R-codes-tips

    1. 在shell执行R文件 chmod 0755 file.R Rscript file.R 2.  载入数据 data(dune) 3. attach() 将data.frame添加到R的搜索路径 ...

  6. Erasing and Winning UVA - 11491 贪心

    题目:题目链接 思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可 AC代码: #include <iostream> #include <cstdio& ...

  7. install mongodb on macos

    Update Homebrew’s package database. In a system shell, issue the following command: brew update 2 In ...

  8. 由浅入深学习PBR的原理和实现

    目录 一. 前言 1.1 本文动机 1.2 PBR知识体系 1.3 本文内容及特点 二. 初阶:PBR基本认知和应用 2.1 PBR的基本介绍 2.1.1 PBR概念 2.1.2 与物理渲染的差别 2 ...

  9. PHP 函数 ignore_user_abort()

    ignore_user_abort 设置与客户机断开是否会终止脚本的执行.   本函数返回 user-abort 设置的之前的值(一个布尔值). int ignore_user_abort ([ st ...

  10. Policy-Based Reinforcement Learning

    Policy-based Approach policy-based 强化学习通常是要学习一个actor, actor可以用\(\pi_\theta (S)\) 来确定.如果我们用actor来玩游戏, ...