Quantcast
Channel: Active questions tagged linux-kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 12244

How to read the start time data in "proc/pid/stat" to calculate the elapsed time in Linux?

$
0
0

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.

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. Can you please help me?

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. 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);
}

Viewing all articles
Browse latest Browse all 12244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>