mac 获得进程信息的方法
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 获得进程信息的方法的更多相关文章
- Linux/Unix分配进程ID的方法以及源代码实现
在Linux/Unix系统中.每一个进程都有一个非负整型表示的唯一进程ID.尽管是唯一的.可是进程的ID能够重用.当一个进程终止后,其进程ID就能够再次使用了. 大多数Linux/Unix系统採用延迟 ...
- 【转载】VC获取MAC地址的4种方法
From:http://blog.csdn.net/pdfmaker/article/details/465748 有需求才有创造,有了问题才会想着去解决,那么我这里的获取MAC地址的第4种方法也是在 ...
- Linux常用命令 查看进程信息时 copy的-----温故而知新
1.查进程 ps命令查找与进程相关的PID号: ps a 显示现行终端机下的所有程序,包括其他用户的程序. ps -A 显示所有程序. ps c 列出程序时,显示每个程序真正的 ...
- windows进程的创建方法
1.WinExec(LPCSTR lpCmdLine,UINT uCmdShow) >>参数: lpCmdLine:指定程序的相对路径或绝对路径,命令行参数 uCmdShow:指定窗口的显 ...
- Windows提供了两种将DLL映像到进程地址空间的方法(隐式和显式)
调用DLL,首先需要将DLL文件映像到用户进程的地址空间中,然后才能进行函数调用,这个函数和进程内部一般函数的调用方法相同.Windows提供了两种将DLL映像到进程地址空间的方法: 1. 隐式的加载 ...
- Windows提供了两种将DLL映像到进程地址空间的方法
调用DLL,首先需要将DLL文件映像到用户进程的地址空间中,然后才能进行函数调用,这个函数和进程内部一般函数的调用方法相同.Windows提供了两种将DLL映像到进程地址空间的方法: 1. 隐式的加载 ...
- 设置SVN,Git忽略MAC的.DS_Store文件的方法
设置SVN,Git忽略MAC的.DS_Store文件的方法 I. 显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bo ...
- Linux基础命令---top显示进程信息
top top指令用来显示Linux的进程信息,这是一个动态显示的过程.top提供运行系统的动态实时视图.它可以显示系统摘要信息以及当前由Linux内核管理的任务列表.所显示的系统摘要信息的类型以及为 ...
- python调用win32com.client的GetObject查找进程信息及服务信息
为何不用wmi呢?因为执行很慢,为啥不用winreg?因为winreg在批量获取及遍历服务方面很不方便,于是采用这方法 该方法同命令行下的wmic执行 获取服务信息 #coding=utf8 from ...
随机推荐
- 爬虫5_python2_使用 Beautiful Soup 解析数据
使用 Beautiful Soup 解析数据(感谢东哥) 有的小伙伴们对写正则表达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫Beautiful Soup,有了它我们可以很方便地提取出HT ...
- intellij IDEA版本控制设置
我们开发肯定是有版本控制的,大家以前Eclipse的时候在本地文件和版本库不一致的时候,那么文件以及所在的文件夹都会出现一个〉表示,大家能很轻松的看到本地文件修改了哪一些,但是IntelliJ中默认是 ...
- ewebeditor上传文件大小
做项目大家都少不了要跟html在线编辑器打交道,这里我把我的一些使用经验及遇到的问题发出来和大家交流一下. Ewebeditor使用说明:一.部署方式:1.直接把压缩目录中的文件拷贝到您的网站发布目录 ...
- Tarjan求强联通分量+缩点
提到Tarjan算法就不得不提一提Tarjan这位老人家 Robert Tarjan,计算机科学家,以LCA.强连通分量等算法闻名.他拥有丰富的商业工作经验,1985年开始任教于普林斯顿大学.Tarj ...
- MySQL 资料库概论与MySQL 安装
本文来自:https://www.breakyizhan.com/sql/5648.html 1. 储存与管理资料 储存与管理资料一直是资讯应用上最基本.也是最常见的技术.在还没有使用电脑来管理你的资 ...
- centos 7 中文乱码的解决办法
@@首先查看系统的操作版本,我的版本是centos 7.2 的. @@查看系统是否有安装中文语言包,一般我们在安装的时候系统都会默认的为我们安装上去的. locale -a | grep " ...
- python网络数据采集 Tesseract
使用chrome代替PhantomJS,selennium3不支持PhantomJS,编码用"utf-8",不然会报错.tesseract要添加TESSDATA_PREFIX环境变 ...
- 蓝牙学习 (7) - raspberrryPi 扫描TI sensorTag
前面几篇分别简单涉及了 raspberryPi上bluez BLE sniffer TI SensorTag https://blog.csdn.net/feiwatson/article/detai ...
- Python模块目录
阅读目录 模块 模块语法 常用模块 collections模块 time模块 random模块 os模块 sys模块 序列化模块 shelve模块 pickle模块 json模块 configpars ...
- LeetCode(39) Combination Sum
题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...