Within the Linux file system there is an unusual directory.
It contains apparently empty (0 bytes) files, and includes several numbered sub-directories.
So what's that all about?
Unix based systems such as Linux have a file system that is quite different to those found in operating systems such as Windows.
Everything is a File
In Linux, the file system not only includes regular files (e.g. text, graphics, executables & so on), but also devices and other data streams, such as serial ports, video cameras and drives.
The /proc directory is a special virtual file system which acts like a window into the kernel, exposing information about system processes (hence "proc"), loaded kernel modules, memory usage and so on.
This makes the /proc directory particularly interesting for system diagnostics, or to anyone writing Linux programs.
There are many Linux command line tools available which draw on the information from /proc. If you want to know how long your computer has been running since it was last turned on or re-booted, just open a terminal and type:-
uptime
...the output may look something like this:-
07:38:12 up 44 min, 2 users, load average: 0.09, 0.12, 0.11
...which includes the current time and the uptime.
But you can also read this information by accessing the /proc/uptime file, either by opening it in a text editor or (more usually) by using the terminal command:-
cat /proc/uptime
...the output may look like this:-
3018.51 5780.35
...where the first figure is the system uptime in seconds, and the second figure is the cpu idle time in seconds.
Now I know what you are thinking; "why is the idle time greater than the uptime?" It is simply because the idle time is the total cpu idle time. If you have a dual core or dual processor system, the idle time is the total for both processors.
One of the benefits of this "everything is a file" arrangement is that all Linux systems have a common way of accessing system information. For example you should be able to use commands like:-
cat /proc/net/wireless
...to access wifi details on any Linux computer, irrespective of the hardware platform.
Programming with /proc
If you are writing a program and you want to use or display your system uptime, there are at least two ways of implementing this.
You could execute the uptime command, capture the output and then strip out the required data. In Gambas your code may look something like this:-
Public Function UpTime() As String
'return uptime as a string
Dim strTemp As String
Exec ["cat", "/proc/uptime"] To strTemp
strTemp = Mid(strTemp, Instr(strTemp,"up")) 'find start
strTemp = Mid(strTemp, 1, Instr(strTemp,",") - 1) 'find end
Return strTemp
End
...which gives you a string e.g. "up 1:42"
Or you could read /proc/uptime and obtain the data, something like this:-
Public Function UpTime() As Long
'return uptime in hours
Dim strUptimeFile As String
Dim hFile As File
Dim strFile As String
Dim lngRunTime As Long
strUptimeFile = "/proc/uptime"
If Exist(strUptimeFile) Then
hFile = Open strUptimeFile For Read
Read #hFile, strFile, -512
Close #hFile
lngRunTime = CLong(CFloat(Mid(strFile, 1, InStr(strFile, " ") - 1)) / 3600)
Endif
Return lngRunTime
End
...which gives uptime in hours as a long integer.
I have used both methods in the past, but now I generally prefer to access the required file in /proc.
Exploring with the cat
As already mentioned, probably the easiest way to view the contents of a /proc file is to use the cat command in a terminal. This allows you to quickly determine if the contents are of any use for your purposes.
Here are a few to check out:-
- /proc/cpuinfo (includes cpu id, speed, number of cores)
- /proc/loadavg (cpu load over 1, 5 & 15min intervals)
- /proc/meminfo (total memory, free memory, buffers)
- /proc/acpi/ (laptop battery, lid status)
- /proc/net/wireless (quality, level, noise)
Further reading: http://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html
No comments:
Post a Comment