Visual|Android中的PID,UID,TID

PID 指进程ID.
PID是进程的身份标识,程序一旦运行,就会给应用分配一个独一无二的PID(ps:一个应用可能包含多个进程,每个进程有唯一的一个PID)
进程终止后PID会被系统收回,再次打开应用,会重新分配一个PID(新进程的PID一般比之前的号要大)
命令:adb shell+ps|grep(图1)或adb shell ps (图2)
图1:
Visual|Android中的PID,UID,TID
文章图片

图2:
Visual|Android中的PID,UID,TID
文章图片


UID 指用户ID.
UID在linux中就是用户的ID,表明时哪个用户运行了这个程序,主要用于权限的管理。而在android 中又有所不同,因为android为单用户系统,这时UID 便被赋予了新的使命,数据共享,为了实现数据共享,android为每个应用几乎都分配了不同的UID,不像传统的linux,每个用户相同就为之分配相同的UID。(当然这也就表明了一个问题,android只能时单用户系统,在设计之初就被他们的工程师给阉割了多用户),使之成了数据共享的工具。
命令:adb shellcat/proc/PID号/status(图3)
图3
Visual|Android中的PID,UID,TID
文章图片


【Visual|Android中的PID,UID,TID】因此在android中PID,和UID都是用来识别应用程序的身份的,但UID是为了不同的程序来使用共享的数据。

package com.inanwong.main; import android.app.Activity; import android.os.Bundle; import android.os.Process; import android.util.Log; public class MainActivity extends Activity { private static final String TAG = MainActivity.class.getSimpleName(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** * Returns the identifier of this process's user. */ Log.e(TAG, "Process.myUid() = " + Process.myUid()); /** * Returns the identifier of this process, which can be used with * killProcess and sendSignal. */ Log.e(TAG, "Process.myPid() = " + Process.myPid()); /** * Returns the identifier of the calling thread, which be used with * setThreadPriority(int, int). */ Log.e(TAG, "Process.myTid() = " + Process.myTid()); /** * Returns the thread's identifier. The ID is a positive long generated * on thread creation, is unique to the thread, and doesn't change * during the lifetime of the thread; the ID may be reused after the * thread has been terminated. */ Log.e(TAG, "Thread.currentThread().getId() = " + Thread.currentThread().getId()); Log.e(TAG, "getMainLooper().getThread().getId() = " + getMainLooper().getThread().getId()); /** * Returns the thread's identifier. The ID is a positive long generated * on thread creation, is unique to the thread, and doesn't change * during the lifetime of the thread; the ID may be reused after the * thread has been terminated. */ Log.e(TAG, "((getApplication().getMainLooper()).getThread()).getId() = " + ((getApplication().getMainLooper()).getThread()) .getId()); /** * Return the identifier of the task this activity is in. This * identifier will remain the same for the lifetime of the activity. */ Log.e(TAG, "getTaskId() = " + getTaskId()); /** * The kernel user-ID that has been assigned to this application; * currently this is not a unique ID (multiple applications can have the * same uid). */ Log.e(TAG, "getApplicationInfo().uid = " + getApplicationInfo().uid); /** * The name of the process this application should run in. From the * "process" attribute or, if not set, the same as packageName. */ Log.e(TAG, "getApplicationInfo().processName = " + getApplicationInfo().processName); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Log.e(TAG, "Thread.currentThread().getId() = " + Thread.currentThread().getId()); } }).start(); } }

Log information: 10-12 01:57:30.885: ERROR/MainActivity(253): Process.myUid() = 10032 10-12 01:57:30.885: ERROR/MainActivity(253): Process.myPid() = 253 10-12 01:57:30.885: ERROR/MainActivity(253): Process.myTid() = 253 10-12 01:57:30.885: ERROR/MainActivity(253): Thread.currentThread().getId() = 1 10-12 01:57:30.885: ERROR/MainActivity(253): getMainLooper().getThread().getId() = 1 10-12 01:57:30.885: ERROR/MainActivity(253): ((getApplication().getMainLooper()).getThread()).getId() = 1 10-12 01:57:30.885: ERROR/MainActivity(253): getTaskId() = 3 10-12 01:57:30.885: ERROR/MainActivity(253): getApplicationInfo().uid = 10032 10-12 01:57:30.885: ERROR/MainActivity(253): getApplicationInfo().processName = com.inanwong.main 10-12 01:57:30.914: ERROR/MainActivity(253): Thread.currentThread().getId() = 8





    推荐阅读