Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

objective c - Is there a way to check if process is 64 bit or 32 bit?

I am trying to find process type (32 bit/ 64bit) from process pid?

I get the process information and process list from using GetBSDProcessList method described here.

how can we get the process type information? Any Ideas?

I can use defined(i386) or defined(x86_64) but only if we are in process. I am out of the process.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

GetBSDProcessList returns a kinfo_proc. The kinfo_proc has a kp_proc member which is of type extern_proc. The extern_proc has a p_flag member, which one of the flags is P_LP64, indicating "Process is LP64"). So you should be able to check with:

int is64bit = proc->kp_proc.p_flags & P_LP64;

(Note: As shown in the comment, you need to use the B_get_process_info found in http://osxbook.com/book/bonus/chapter8/core/download/gcore.c:

static int
B_get_process_info(pid_t pid, struct kinfo_proc *kp)
{
    size_t bufsize      = 0;
    size_t orig_bufsize = 0;
    int    retry_count  = 0;
    int    local_error  = 0;
    int    mib[4]       = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };

    mib[3] = pid;
    orig_bufsize = bufsize = sizeof(struct kinfo_proc);

    for (retry_count = 0; ; retry_count++) {
        local_error = 0;
        bufsize = orig_bufsize;
        if ((local_error = sysctl(mib, 4, kp, &bufsize, NULL, 0)) < 0) {
            if (retry_count < 1000) {
                sleep(1);
                continue;
            }
            return local_error;
        } else if (local_error == 0) {
            break;
        }
        sleep(1);
    }

    return local_error;
}

)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...