Nothing Special   »   [go: up one dir, main page]

CS333 OS Lab06 BSCS5C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Department of Computing

CS 330: Operating Systems


BSCS: 5C
Lab 6: Exploring /Proc File
System
CLO1(Explain & summarize OS Services and Abstractions)

Date: 30-10-2017
Time: 02:00 PM – 05:00 PM
Instructor: Dr. Muhammad Ali Tahir

CS330: Operating Systems Page 1


Lab 6: Exploring /Proc File System
Introduction

Ultimately the OS is just a program with many different data structures. A common requirement
for any program is to measure performance while it is running. In order to get a better
understanding of the organization and behavior of the Linux Kernel we will write a program that
will report on the state of the data structures inside the OS. Linux, Solaris and some versions of
UNIX, provide the /proc File System to collect information from the Kernel.

Objectives

➢ Get an insight into Linux OS using the /proc File System

➢ Write a C/C++ program to sample OS state

Tools/Software Requirement

➢ Linux OS installed on laptops or systems.


➢ GCC

Description

Start off by looking around in the /proc directory. You will find a large directory structure that has
information regarding everything from device drivers, current memory usage as well as subtree's
for each process currently running -- more information than we'll need for this lab. These “files”
are simple ASCII, so you can use 'cat' to view them from the command line, or open them in a text
editor. (“Everything in Linux is a file” is a common phrase.) It is important to note that these are
not actually files you can find on the physical hard disk, but actually virtual files mirroring various
kernel data structures. But, because they behave like text files, you can easily read them with the
stdio functions like fgets(), or fscanf() functions. In some cases they will actually show as being 0
bytes, and will be written as you open or access it.

But how can we find out which files have the information we need? How can we know what the
format of these files will be in order to parse them?

Again, the 'man' utility will provide all the documentation we need. Because most manual pages
are larger than it can fit on the screen at once, you can use the following to scroll through a large
file:

>man proc | less

CS330: Operating Systems Page 2


The “|” symbol means to pipe the output from one command (man proc) into the next (less). So,
we are saying to take all the man page output and give it to the 'less' utility which lets us scroll
through the files as we read.You may also dump the output to a file and read it in a text editor by
doing:

>man proc > proc.txt

Here the “>” redirects the output from 'man' to a newly created file proc.txt. For more advanced
Linux users you will want to pipe to 'grep' to search thought this file for keywords. So, a simple
search for the word “memory” would be:

>man proc | grep memory

The 'grep' utility searches through the input text and outputs only those lines containing the
searched text. There are many options to use with grep, but this is a simple way to search for
keywords, and will help you find what files in /proc with have the information you need. Also you
might need to further pipe the output if there are so many matches that you must scroll through it
all. Such as:

>man proc | grep memory | less

You can also search within a manual page when you visualize it with the 'less' tool by typing
'/keyword' (a slash character followed by the keyword you want to search for). Use 'q' to escape
from the 'less' utility at any time. After finding the information you need you can test it simply
with 'cat'. It is up to you to locate the files you need, but as an example, you can get the current
Linux kernel distribution number by typing:

> cat /proc/sys/kernel/osrelease

2.6.15-28-386

In addition to proc, other useful commands that help in monitoring OS state are “ps” and “top”.
Try out the following commands:

> ps aux

> top

These commands will likely be very useful whenever you use Linux for any serious task.

CS330: Operating Systems Page 3


Tasks

After getting a tour of the /proc directory structure and examining these files the rest is straight
forward programming. Your program will default to printing out:

a. Processor Manufacturer and Model

b. Kernel version

c. The amount of time since it was last booted.

d. The amount of time the processor has spent in user mode, system mode, and idle time

e. The amount of memory configured for this computer

f. The amount of memory currently available

(To find help regarding above requirements, follow the links given at the end). Start your code by
reading in the /proc files you need like this:

fid = fopen(“/proc/sys/kernel/foo”, "rb");

fscanf(fid, “...”, “...”);

... // parse

printf(“Processor type :%s\n”, processorType );

Then simply parse it based on the information you found above with the man page for proc and
print them to stdout. Your program should produce a well formatted, easy to read report to the
terminal with all the above information. As you can see this information could easily be sent to a
graphing program to be plotted. Have a look at the Performance Monitor on Linux, and similar
tools on OSX and Windows – you get an up to date set of graphs of the state of the OS which can
be very useful for tracking down problems as the computer is running.

Links

• http://www.linuxhowtos.org/System/procstat.htm

• https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-iomem.html

CS330: Operating Systems Page 4


• https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-stat.html

• https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-devices.html

• https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html

Deliverables

Submit the code.

CS330: Operating Systems Page 5

You might also like