Aaron Mason
Wed, 13 Jan 2010 15:24:54 -0800
On Thu, Jan 14, 2010 at 7:28 AM, patrick keshishian <pkesh...@gmail.com>
wrote:
> On Wed, Jan 13, 2010 at 2:07 AM, Sebastian Spies
> <sebastian.sp...@de-cix.net> wrote:
> [dd]
>> Could you
>> please comment on the following code?
>>
>> int main(int argc, char** argv)
>> {
>> kvm_t *kd;
>> struct kinfo_proc2 *info;
>> int cnt;
>>
>> if(argc != 2)
>> {
>> printf("Please provide pid\n");
>> exit(1);
>> }
>>
>>
>> kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "error");
>> if(!kd)
>> exit(1);
>>
>> if(cnt != 1)
>
> Not related to what you are asking about, but, you are checking cnt
> before it is set.
>
>
>> {
>> printf("No process found\n");
>> exit(1);
>> }
>>
>> info = kvm_getproc2(kd, KERN_PROC_PID, atoi(argv[1]),
>> sizeof(struct kinfo_proc2), &cnt);
>>
>> printf("%x\n",(double)info->p_cpticks);
>>
>> exit(0);
>> }
>
>
And if you want to use this in a script, you might want to set a different exit value for each error condition. It's usually good practice to send a numeric error code and let your script handle the error itself. By all means keep the message there, but don't make it impossible to use in a script. </offtopic> Try info->p_pctcpu instead. -- Aaron Mason - Programmer, open source addict I've taken my software vows - for beta or for worse