简体   繁体   English

Linux中如何读取proc/pid/stat中的开始时间数据来计算经过的时间?

[英]How to read the start time data in “proc/pid/stat” to calculate the elapsed time in Linux?

My aim is to write a system-call that finds the time passed from the starting time of an ongoing process.我的目标是编写一个系统调用来查找从正在进行的进程的开始时间经过的时间。 I am researching still trying to understand.我正在研究仍然试图理解。 Firstly I tried the code below but it returned -1, I am not sure this was the correct way to trace the processes.首先,我尝试了下面的代码,但它返回 -1,我不确定这是跟踪进程的正确方法。

asmlinkage int sys_deneme(pid_t pid)
{
    struct task_struct *task;
    struct tms *tms;
    int returnValue = 0;

//Is this correct to find a specific process?
//The code returns -1. Why it never enters if part?
    for_each_process(task){ 
        if((int)task->pid == pid){
            times(tms); 
            returnValue = returnValue + (int) tms->tms_utime +(int) tms->tms_stime+(int)tms->tms_cutime+(int)tms->tms_cstime;
            return returnValue;
        }
        else{
         return -1;
        }
    }
}

Then I decided to use the data in proc/pid/stat , but I don't know how to read the start time of given pid and return.然后我决定使用proc/pid/stat 中的数据,但我不知道如何读取给定 pid 的开始时间并返回。

asmlinkage int sys_deneme(pid_t pid)
{
    struct task_struct *task;
    struct tms *tms;
    int returnValue = 0;

        struct kstat *stat;

    for_each_process(task){
        if((int)task->pid == pid){
            returnValue = (int)stat->btime->tv_sec;
            return returnValue;
        }
        else{
            return -1;
        }
    }
}

Edit编辑

According to advices and research, I have succeed to pass the parameter pid then printed pid and name.根据建议和研究,我成功地传递了参数 pid 然后打印了 pid 和 name。 Now trying to find start time/elapsed time.现在试图找到开始时间/经过时间。

{
struct task_struct *task;

task = pid_task(find_vpid(pid),PIDTYPE_PID);

printk(KERN_INFO "pid %d \n",pid);

printk(KERN_INFO "Name: %s\n",task->comm);
}

(Posted the solution on behalf of the question author, to move it from the question post) . (代题作者贴出解决方案,从题帖中移出)

The code I wrote is added below.我写的代码添加在下面。 I have searched where the Process Control Block is stored and found a struct named task_struct which stores many useful fields https://docs.huihoo.com/doxygen/linux/kernel/3.7/structtask__struct.html .我搜索了进程控制块的存储位置,找到了一个名为 task_struct 的结构,它存储了许多有用的字段https://docs.huihoo.com/doxygen/linux/kernel/3.7/structtask__struct.html

I have noticed that I was not able to pass the argument to the system call.我注意到我无法将参数传递给系统调用。 Thanks to the answer on this topic How to pass parameters to Linux system call?感谢关于这个主题的答案如何将参数传递给 Linux 系统调用? I have done it.我已经做了。 Lastly returned the elapsed time in seconds.最后以秒为单位返回经过的时间。

SYSCALL_DEFINE1(get_elapsed_time, int, pid)
{
    struct task_struct *task; //(1)

    for_each_process(task){
        if(pid == task->pid){
            return (((ktime_get_ns())-(task->start_time))/1000000000);
        }
    }

    return -1;
}
//or 
//struct task_struct *task;
//task = pid_task(find_vpid(pid),PIDTYPE_PID);
//return (((ktime_get_ns())-(task->start_time))/1000000000);
//found ktime_get_ns() from timekeeping.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM