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. Linux/Unix分配进程ID的方法以及源代码实现

    在Linux/Unix系统中.每一个进程都有一个非负整型表示的唯一进程ID.尽管是唯一的.可是进程的ID能够重用.当一个进程终止后,其进程ID就能够再次使用了. 大多数Linux/Unix系统採用延迟 ...

  2. 【转载】VC获取MAC地址的4种方法

    From:http://blog.csdn.net/pdfmaker/article/details/465748 有需求才有创造,有了问题才会想着去解决,那么我这里的获取MAC地址的第4种方法也是在 ...

  3. Linux常用命令 查看进程信息时 copy的-----温故而知新

    1.查进程    ps命令查找与进程相关的PID号:    ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程序.    ps c 列出程序时,显示每个程序真正的 ...

  4. windows进程的创建方法

    1.WinExec(LPCSTR lpCmdLine,UINT uCmdShow) >>参数: lpCmdLine:指定程序的相对路径或绝对路径,命令行参数 uCmdShow:指定窗口的显 ...

  5. Windows提供了两种将DLL映像到进程地址空间的方法(隐式和显式)

    调用DLL,首先需要将DLL文件映像到用户进程的地址空间中,然后才能进行函数调用,这个函数和进程内部一般函数的调用方法相同.Windows提供了两种将DLL映像到进程地址空间的方法: 1. 隐式的加载 ...

  6. Windows提供了两种将DLL映像到进程地址空间的方法

    调用DLL,首先需要将DLL文件映像到用户进程的地址空间中,然后才能进行函数调用,这个函数和进程内部一般函数的调用方法相同.Windows提供了两种将DLL映像到进程地址空间的方法: 1. 隐式的加载 ...

  7. 设置SVN,Git忽略MAC的.DS_Store文件的方法

    设置SVN,Git忽略MAC的.DS_Store文件的方法 I. 显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bo ...

  8. Linux基础命令---top显示进程信息

    top top指令用来显示Linux的进程信息,这是一个动态显示的过程.top提供运行系统的动态实时视图.它可以显示系统摘要信息以及当前由Linux内核管理的任务列表.所显示的系统摘要信息的类型以及为 ...

  9. python调用win32com.client的GetObject查找进程信息及服务信息

    为何不用wmi呢?因为执行很慢,为啥不用winreg?因为winreg在批量获取及遍历服务方面很不方便,于是采用这方法 该方法同命令行下的wmic执行 获取服务信息 #coding=utf8 from ...

随机推荐

  1. softmax_loss

    softmax_loss中的ignore_label是来自于loss layer,而不是softmax_loss的参数

  2. javascript基本数据类型问题汇总

    isNaN()检测是否是NaN: 比较浮点相等,用绝对值,是否小于某一个阈值 Math.abs(1/3 - (1-2/3))<0.0000001: 字符串多行显示\n,ES6中使用反引号``: ...

  3. 浅谈js的sort()方法

    如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码(字符串Unicode码点)的顺序进行排序.要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以 ...

  4. sstable, bigtable,leveldb,cassandra,hbase的lsm基础

    先看懂文献1和2 1. 先了解sstable.SSTable: Sorted String Table [2] [10] WiscKey:  类似myisam, key value分离, 根据ssd优 ...

  5. shell脚本,文件里面的英文大小写替换方法。

    [root@localhost wyb]# cat daxiaoxie qweBNMacb eeeDFSmkl svdIOPtyu [root@localhost wyb]# cat daxiaoxi ...

  6. css去除链接 input 虚框

    /* css去掉虚框 */ :focus{-webkit-outline-style:none;-moz-outline-style:none;-ms-outline-style:none;-o-ou ...

  7. 优化mysql查询

    mysql提供了一个特别的explain语句,用来分析查询语句的性能 : explain select ... 1.在所有用于where,order by,group by的列上添加索引 创建索引 添 ...

  8. (37)zabbix snmp类型 无需安装agent也能监控

    概述 如果我们需要监控打印机.路由器.UPS等设备,肯定不能使用zabbix agentd,因为他们不能安装软件的,还好他们一般都支持SNMP协议,这样我可以使用SNMP来监控他们.如果你希望使用SN ...

  9. (转)WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    [root@bak1 bak]# scp gwsyj.sql.gz root@192.168.21.65:/data/dbdata/ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  10. Lex与Yacc学习(八)之变量和有类型的标记(扩展计算器)

    变量和有类型的标记 下一步扩展计算器来处理具有单个字母名字的变量,因为只有26个字母 (目前只关心小写字母),所以我们能在26个条目的数组(称它为vbltable)中存储变量. 为了使得计算器更加有用 ...