Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.embedded > #432 > unrolled thread
| Started by | SJC <jobhunts02@aol.com> |
|---|---|
| First post | 2013-01-16 22:37 -0800 |
| Last post | 2013-01-18 16:41 +0100 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.os.linux.embedded
Reading /proc/stat btime from user space SJC <jobhunts02@aol.com> - 2013-01-16 22:37 -0800
Re: Reading /proc/stat btime from user space Joerg Schmitz-Linneweber <joerg@schmitz-linneweber.name> - 2013-01-17 09:12 +0100
Re: Reading /proc/stat btime from user space Joerg Schmitz-Linneweber <joerg@schmitz-linneweber.name> - 2013-01-17 09:17 +0100
Re: Reading /proc/stat btime from user space SJC <jobhunts02@aol.com> - 2013-01-17 10:02 -0800
Re: Reading /proc/stat btime from user space Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2013-01-17 23:10 +0200
Re: Reading /proc/stat btime from user space SJC <jobhunts02@aol.com> - 2013-01-17 14:20 -0800
Re: Reading /proc/stat btime from user space Joerg Schmitz-Linneweber <joerg@schmitz-linneweber.name> - 2013-01-18 16:41 +0100
| From | SJC <jobhunts02@aol.com> |
|---|---|
| Date | 2013-01-16 22:37 -0800 |
| Subject | Reading /proc/stat btime from user space |
| Message-ID | <2ddadfb4-bd08-4ffa-9153-a009b722eb56@googlegroups.com> |
I would like to read the value of /proc/stat btime in my user space program. How can I do that?
[toc] | [next] | [standalone]
| From | Joerg Schmitz-Linneweber <joerg@schmitz-linneweber.name> |
|---|---|
| Date | 2013-01-17 09:12 +0100 |
| Message-ID | <alpq5gFiuouU1@mid.dfncis.de> |
| In reply to | #432 |
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
[toc] | [prev] | [next] | [standalone]
| From | Joerg Schmitz-Linneweber <joerg@schmitz-linneweber.name> |
|---|---|
| Date | 2013-01-17 09:17 +0100 |
| Message-ID | <alpqeoFivtcU1@mid.dfncis.de> |
| In reply to | #433 |
Ups!
Should read
btime=$(awk '/btime/ {print $2}' /proc/stat)
[toc] | [prev] | [next] | [standalone]
| From | SJC <jobhunts02@aol.com> |
|---|---|
| Date | 2013-01-17 10:02 -0800 |
| Message-ID | <a8c97d63-2856-45ca-802d-a539739c5343@googlegroups.com> |
| In reply to | #432 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Tauno Voipio <tauno.voipio@notused.fi.invalid> |
|---|---|
| Date | 2013-01-17 23:10 +0200 |
| Message-ID | <kd9pco$96j$1@dont-email.me> |
| In reply to | #435 |
On 17.1.13 8:02 , SJC wrote: > 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? 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
[toc] | [prev] | [next] | [standalone]
| From | SJC <jobhunts02@aol.com> |
|---|---|
| Date | 2013-01-17 14:20 -0800 |
| Message-ID | <c9e0e710-d63f-40f9-891b-96bcbffcd571@googlegroups.com> |
| In reply to | #432 |
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;
}
[toc] | [prev] | [next] | [standalone]
| From | Joerg Schmitz-Linneweber <joerg@schmitz-linneweber.name> |
|---|---|
| Date | 2013-01-18 16:41 +0100 |
| Message-ID | <alt8qkFcldcU1@mid.dfncis.de> |
| In reply to | #437 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.os.linux.embedded
csiph-web