【问题标题】:fetching individual running process Start-Time获取单个正在运行的进程 Start-Time
【发布时间】:2015-07-22 06:51:00
【问题描述】:

我正在使用下面的代码来获取 Android 设备上所有当前正在运行的进程。

// Get running processes
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();

而且我还尝试使用以下代码从以下文件目录中的每个 pid-“进程 ID”获取 Android 设备中的所有单个进程启动时间:“/proc/[PID]/stat”获得来自linux:

public static long getStartTime(final int pid) throws IOException {

final String path = ("/proc/" + pid + "/stat");
final String stat;
final String field2End = ") ";
final String fieldSep = " ";
final BufferedReader reader = new BufferedReader(new FileReader(path));

 try {
        stat = reader.readLine();
        System.out.println("******Stat******"+ stat);
    } finally {
        reader.close();
    }

try {
        final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
        final long processstartTime = .....;
        ...(change processstartTime from clock tick to seconds & return processstartTime value)...
}
}

我确实需要从以下 Linux 目录路径获取进程 StartTime:“/proc/pid/stat”,用于 Android 设备中每个正在运行的进程。此时,当我调试以下 Linux 目录路径的语句时:“/proc/[PID]/stat”,在以下代码行中:System.out.println("******Stat******"+ stat);,我得到的输出调试为:

******Stat******642 (flipboard.app) S 2848 2848 0 0 -1 4194624 126020 0 1019 0 2441 632 0 0 20 0 101 0 7040346 1079652352 7233 4294967295 1 1 0 0 0 0 4612 0 38120 4294967295 0 0 17 1 0 0 0 0 0 0 0 0

此外,我确实知道进程的 start_time 是以时钟滴答为单位测量的,因此要将其转换为秒,我需要将以下内容称为“start_time/hertz”。

现在的问题是,如何在“/proc/[PID]/stat”中获取正在运行的进程开始时间?有人可以帮忙吗?谢谢。

【问题讨论】:

    标签: android linux process linux-kernel runtime


    【解决方案1】:

    单个进程时间的登录可以通过这种方式完成:

    public static long getStartTime(final int pid) throws
            IOException {
        final long SYSTEM_CLK_TCK= 100; //needed as value in /proc/[PID]/stat file driectory is in clock ticks,100 is used to convert clock ticks to secs
        final int fieldStartTime = 20; //column 20 of the /proc/[PID]/stat file driectory
    try {
            System.out.println("******String path******"+ path);
            stat = reader.readLine();
            System.out.println("******String stat******"+ stat);
        } finally {
            reader.close();
        }
    try {
            final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
            final long startTime = Long.parseLong(fields[fieldStartTime]);
            System.out.println("******fieldstarttime based on clock ticks******"+ startTime);
    
            return startTime * msInSec / SYSTEM_CLK_TCK;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多