Reading /proc/stat btime from user space

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

Reply to
SJC
Loading thread data ...

Hi "JobHunter"

Am 17.01.2013 07:37, schrieb SJC:

How can I do that?

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

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

Salut, Joerg

Reply to
Joerg Schmitz-Linneweber

Ups!

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

Reply to
Joerg Schmitz-Linneweber

How can I do that?

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

Reply to
SJC

How can I do that?

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
Reply to
Tauno Voipio

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

Reply to
SJC

Hi!

Am 17.01.2013 23:20, schrieb SJC:

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

Reply to
Joerg Schmitz-Linneweber

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.