天天看点

android 根据pid 获取进程名

c/c++

status_t getProcessName(int pid, String8& name) {

1259    IPCThreadState* ipc = IPCThreadState::self();
1260    const int pid = ipc->getCallingPid();
1261    const int uid = ipc->getCallingUid();
1262
1263    OEXUNUSED(pid);
1264    OEXUNUSED(uid);      

    FILE *fp = fopen(String8::format("/proc/%d/cmdline", pid), "r");

    if (NULL != fp) {

        const size_t size = PROC_LINE_SIZE_MAX;

        char proc_name[PROC_LINE_SIZE_MAX];

        memset(proc_name, 0x0, sizeof(proc_name));

        fgets(proc_name, size, fp);

        fclose(fp);

        name = proc_name;

        return NO_ERROR;

    }

    return INVALID_OPERATION;

}

java