Reading /proc/stat btime from user space

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I would like to read the value of /proc/stat btime in my user space program.
How can I do that?

Re: Reading /proc/stat btime from user space
Hi "JobHunter"

Am 17.01.2013 07:37, schrieb SJC:
Quoted text here. Click to load it
How can I do that?

btime=$(awk '/btime/ {print $2}')

Of course your "user space prog" is written in sh, right?
:-)

Salut, Joerg

Re: Reading /proc/stat btime from user space
Ups!

Should read
btime=$(awk '/btime/ {print $2}' /proc/stat)

Re: Reading /proc/stat btime from user space
On Wednesday, January 16, 2013 10:37:20 PM UTC-8, SJC wrote:
Quoted text here. Click to load it
How can I do that?

It is written in C.  How do I get that value into a C variable?

Re: Reading /proc/stat btime from user space
On 17.1.13 8:02 , SJC wrote:
Quoted text here. Click to load it
How can I do that?
Quoted text here. Click to load it


Are you asking how to read a file from C code?

The /proc pseudo-files read just in the same way as regular files
from the file system.

For details, please re-read your C programming manual.

--  

Tauno Voipio


Re: Reading /proc/stat btime from user space
I have a solution (see below).  This will give me, for example, "btime 1358
425742."  This is fine because I don't need the actual number.  I just need
 to compare boottime strings to determine if the system has rebooted.  By t
he way, if there is a better way to determine if a system has rebooted, ple
ase let me know.

static void get_boottime(char *boottime) {

        FILE *fp;
        int status;

        fp = popen("awk '/^btime/' /proc/stat", "r");
        if (fp == NULL) {
                DEBUG("get_boottime: popen failed");
        }
        else {
                fgets(boottime, BOOTTIME_MAX_LEN, fp);
                DEBUG("get_boottime: Boottime is %s", boottime);
                status = pclose(fp);
                if (status == -1) {
                        DEBUG("get_boottime: Could not close");
                }
        }
        return;
}


Re: Reading /proc/stat btime from user space
Hi!

Am 17.01.2013 23:20, schrieb SJC:
Quoted text here. Click to load it
You're kidding, aren't you? :-)

Why not read in the file line-by-line and check if the actual line
starts with 'btime'? (fopen, fgets, strncmp, fclose)

You are starting a new process and creating two pipes to read something
from a (text) file?

Perhaps Tauno's tip was more suitable... ;-)

Salut, Jörg

Site Timeline