Android中的PID,UID,TID

PID 指进程ID.

PID是进程的身份标识,程序一旦运行,就会给应用分配一个独一无二的PID(ps:一个应用可能包含多个进程,每个进程有唯一的一个PID)

进程终止后PID会被系统收回,再次打开应用,会重新分配一个PID(新进程的PID一般比之前的号要大)

命令:adb shell  +  ps|grep(图1)   或   adb shell ps (图2)

图1:

图2:

 

UID 指用户ID.

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

命令:adb shell  cat  /proc/PID号/status(图3)

图3

 

因此在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

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值