Operating System Tutorial
Operating System Tutorial
Augustine
Department of Computing & Information Technology
COMP 2604 - Semester II, 2022/2023
Tutorial #2
The Linux operating system has a section of its file system (under /proc) that is used for
inspecting the kernel state.
Files in the /proc directory are not regular files, they are pseudo-files. In fact, they are
actually programs that read kernel variables and report them as ASCII strings. However,
note this fact for information purposes only. It will not affect your exercises in this tutorial.
cd /proc [ENTER]
. List the files in this directory with the command
ls [ENTER]
Files within /proc contain information about different aspects of the internal operation of
the operating system kernel. For example, there is a file called cpuinfo (which you should
have seen in the directory listing) that contains among other things the cpu model of the
computer system.
. View the contents of the cpuinfo file by entering the following command
In general, given a text file named file.txt, you can display its contents with the command
cat file.txt.
You can write a C program to extract kernel information from these files.
1
Reading a File Line by Line
It would be better to make your program work without having to know the exact number of
lines in the cpuinfo file. This can be done by making use of the function
This function returns TRUE if the last file operation terminated because of an END OF
FILE condition. It returns FALSE otherwise.
Note that the C language does not have any boolean data type. However, 0 is treated
as “false”, and any non-zero integer is treated as “true”. It is common for C programs to
use the following defined constants to simulate a boolean data type.
#define TRUE 1
#define FALSE 0
2
Looking for a Specific Line in a File
Now that you are able to read each line of the file, regardless of how many lines exist, you
can search through each line to find a particular piece of information that you want. For
example, if you wanted to know what kind of CPU your computer is using, you would look
in the cpuinfo file for the line containing “model name”.
. Within the while loop, use the strstr() function to search the current line for the
substring “model name”. If the current line contains that substring, then print the
line, and break out of the loop. (You may wish to review Example #30 to see how
strstr() works.)
. Compile and run this new version of getinfo.c.
You now need to extract the type of CPU from that particular line of the file. To do this,
note that a colon (:) separates the substring “model name” from the actual CPU type. You
can use the strchr() function to search for the colon in the line. The strchr() function
will return a char * pointer to the location in the string where the : character was found.
In effect you would now have a pointer to a substring of the line beginning with :. If you
increment that pointer by 1, then you would have a pointer to the substring containing the
type of CPU. This is what you are trying to obtain.
. In your code, outside of the while loop, use strchr() to search the line of text for
’:’.
. Capture the return value of the function in a variable named substring of type char
*.
. Use a printf() statement to print this substring. (You may wish to review Example
#27 to see how strchr() works.)
. Compile and run this new version of getinfo.c.
. Modify your code so that before the last printf() statment, the pointer substring
is incremented by 1.
. Compile and run this new version of getinfo.c.
3
Adding Command Line Parameters to Your Program
Now that you have experience extracting information from a text file, modify your program
so that it can take command line parameters. You should cater for the following parameters:
To get started, you should re-work your current code to cater for the -c option, since you’ve
already written code to get the type of CPU on the computer.
Responding to -c
. Move all of the code from the main() function into a function called
print cpu info().
. Re-write the main() function to use the signature
./a.out -c [ENTER]
Now you can cater for the second command line parameter. To determine the version of the
Linux kernel in use, look at the file /proc/version.
Responding to -k
. In your source code, copy and paste the print cpu info() function. Call the new
copy print kernel info().
. Modify the code in print kernel info() so that the desired information is extracted
from the file /proc/version.
4
. In the main() function, after the point where you checked to see if args[1] was equal
to -c, add an else statement. In the else statement, check to see if args[1] is equal
to -k. If it is, then call print kernel info()
. Compile and run this new version of getinfo.c. Test it in two different ways using
the following commands:
./a.out -c [ENTER]
./a.out -k [ENTER]
Finally, you can cater for the last command line parameter. To determine the amount of
time since the system was booted (known as the “uptime”), look at the file /proc/uptime
Responding to -u
. In your source code, copy and paste the print cpu info() function. Call the new
copy print uptime info().
. Modify the code in print uptime info() so that the desired information is extracted
from the file /proc/uptime.
. In the main() function, after the point where you checked to see if args[1] was equal
to -k, add a second else statement. In the else statement, check to see if args[1]
is equal to -u. If it is, then call print uptime info()
. Compile and run this new version of getinfo.c. Test it in three different ways using
the following commands:
./a.out -c [ENTER]
./a.out -k [ENTER]
./a.out -u [ENTER]