9
我需要一種方法來檢索Mac上所有用戶的所有正在運行的進程(使用Cocoa)。我發現一個實現使用sysctl檢索進程,但我也需要運行用戶。這是我得到進程列表的剪輯,但是有沒有辦法修改它以包含用戶呢?我可以使用`sysctl`來檢索用戶的進程列表嗎?
int err;
kinfo_proc * result;
bool done;
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
size_t length;
// a valid pointer procList holder should be passed
assert(procList != NULL);
// But it should not be pre-allocated
assert(*procList == NULL);
// a valid pointer to procCount should be passed
assert(procCount != NULL);
*procCount = 0;
result = NULL;
done = false;
do
{
assert(result == NULL);
// Call sysctl with a NULL buffer to get proper length
length = 0;
err = sysctl((int *)name,(sizeof(name)/sizeof(*name))-1,NULL,&length,NULL,0);
if(err == -1)
err = errno;
// Now, proper length is optained
if(err == 0)
{
result = malloc(length);
if(result == NULL)
err = ENOMEM; // not allocated
}
if(err == 0)
{
err = sysctl((int *)name, (sizeof(name)/sizeof(*name))-1, result, &length, NULL, 0);
if(err == -1)
err = errno;
if(err == 0)
done = true;
else if(err == ENOMEM)
{
assert(result != NULL);
free(result);
result = NULL;
err = 0;
}
}
} while (err == 0 && !done);
// Clean up and establish post condition
if(err != 0 && result != NULL)
{
free(result);
result = NULL;
}
*procList = result; // will return the result as procList
if(err == 0)
*procCount = length/sizeof(kinfo_proc);
assert((err == 0) == (*procList != NULL));
return err;
「kinfo_proc增加的聲明,你會發現它有一個類型爲struct eproc的kp_eproc成員,該成員又有一個類型爲struct _ucred的e_ucred成員,而該成員又具有一個類型爲uid_t的cr_uid成員「在C – Dani
中獲得了愛情間接感謝感謝您的好回答! – bugfixr
在Lion上,即使使用KERN_PROC_ALL作爲此代碼,上面的sysctl也不會返回所有進程。此代碼在一次測試中返回121,而ps -afx返回149.即使運行sysctl進程的用戶擁有的某些進程也會被忽略。我仔細研究了Bavarious的代碼,並且找不到長度方面的錯誤,例如,這會產生差異。 – ctpenrose