0% found this document useful (0 votes)
34 views

Operating System Tutorial

The document describes a tutorial on extracting system information from the Linux /proc directory using C programs. It discusses reading files line by line and searching for specific strings. It then provides instructions on modifying a sample C program to print the CPU model, Linux kernel version, or system uptime when passed different command line arguments. The program uses functions like fgets(), feof(), strstr(), strchr() to process lines from files like /proc/cpuinfo, /proc/version, and /proc/uptime.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Operating System Tutorial

The document describes a tutorial on extracting system information from the Linux /proc directory using C programs. It discusses reading files line by line and searching for specific strings. It then provides instructions on modifying a sample C program to print the CPU model, Linux kernel version, or system uptime when passed different command line arguments. The program uses functions like fgets(), feof(), strstr(), strchr() to process lines from files like /proc/cpuinfo, /proc/version, and /proc/uptime.

Uploaded by

Daniel McDougall
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

The University of the West Indies at St.

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.

Viewing Information in the /proc Directory

. Open a shell window.


. Change directories to the /proc directory. You can do this by entering the command

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

cat cpuinfo [ENTER]

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

. Using Example #46 as a template, write a C program to read the /proc/cpuinfo


file. Save your program as getinfo.c
. Change the filename string from "sample.txt" to "/proc/cpuinfo".
. Go back to the shell window in which you viewed the contents of the cpuinfo file.
. Count the number of lines in the file.
. In your getinfo.c program, change the limit of the for loop to the number of lines
in the file.
. Compile and run getinfo.c

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

int feof(FILE *fp)

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

. Replace the for loop with an infinite while loop.


. After the fgets() function call, make a call to feof(). If it returns a non-zero value,
break out of the loop (it means you’re at the end of the file).
. Compile and run this new version of getinfo.c.

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.

Extracting Part of a Line

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:

Parameter Action to be taken


-c Print the type of CPU on the computer.
-k Print the version of the Linux kernel in use.
-u Print the amount of time since the system was booted.

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

int main(int argc, char *argv[])


. In the main() function, use strcmp() to see if args[1] is equal to -c. If it is, then
call the print cpu info() function.
. Compile and run this new version of getinfo.c. Test it by entering the command:

./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]

You might also like