Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading /proc/stat btime from user space

354 views
Skip to first unread message

SJC

unread,
Jan 17, 2013, 1:37:20 AM1/17/13
to
I would like to read the value of /proc/stat btime in my user space program. How can I do that?

Joerg Schmitz-Linneweber

unread,
Jan 17, 2013, 3:12:33 AM1/17/13
to
Hi "JobHunter"

Am 17.01.2013 07:37, schrieb SJC:
> I would like to read the value of /proc/stat btime in my user space program. How can I do that?

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

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

Salut, Joerg

Joerg Schmitz-Linneweber

unread,
Jan 17, 2013, 3:17:30 AM1/17/13
to
Ups!

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

SJC

unread,
Jan 17, 2013, 1:02:00 PM1/17/13
to
On Wednesday, January 16, 2013 10:37:20 PM UTC-8, SJC wrote:
> I would like to read the value of /proc/stat btime in my user space program. How can I do that?

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

Tauno Voipio

unread,
Jan 17, 2013, 4:10:48 PM1/17/13
to
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

SJC

unread,
Jan 17, 2013, 5:20:42 PM1/17/13
to
I have a solution (see below). This will give me, for example, "btime 1358425742." 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 the way, if there is a better way to determine if a system has rebooted, please 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;
}

Joerg Schmitz-Linneweber

unread,
Jan 18, 2013, 10:41:10 AM1/18/13
to
Hi!

Am 17.01.2013 23:20, schrieb SJC:
> 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;
> }
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
0 new messages