HomeWork 2

advertisement
Advanced Operating systems
Homework 2
ECE Dept., Univ. of Tehran
1-Write a program to use the "/Proc" mechanism to inspect various kernel variables that reflect the
machine's load average, process resource utilization, and so on. After observing the kernel
status, prepare a report of the cumulative behavior that you observed.
Background: The /Proc file system, according to McKusick, et al [1998], comes from UNIX
Eight Edition and has been used with 4.4 BSD. "In the /proc system, the address space of
another process can accessed with "read" and "write" system calls, which allows a debugger to
access a process being debugged with much greater efficiency. The page (or pages) of interest
in the child process is mapped into the kernel space. The requested data can then be copied
directly from the kernel to the parent address space." In addition, /proc can be used to collect
information about processes in the system, even though that is not done in 4.4 BSD. [1]
The /proc file system is an OS mechanism whose interface appears as a directory in the
conventional UNIX file system (in the root directory). You can change to /proc just as you
change to any other directory. For example,
% cd /proc
makes /proc the current directory. A file in /proc or one of its subdirectories is actually a
program that reads kernel variables and reports them as ASII strings. Some of these routines
read the kernel tables only when the pseudo file is open, whereas others read the tables each
time that the file is read. Files in /proc are read just like ordinary ASCII files. For example,
when you type to the shell a command such as
% cat /proc/version
You will get a message printed to stdout that resemble the following:
% Linux version 2.4.20-20.9 (bhcompile@stripples.devel.redhat.com) (gcc
version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Mon Aug 18 11:45:58 EDT
2003
To read a /proc pseudo file's contents, you open the file and then use stdio library routines such
as fgets() or fscanf() to read the file. The exact files read depends on the specific Linux version
that you are using. To find out exactly which file interfaces are available to you through /proc,
read the /proc man page on the system.
Other materials are:
1. LKP Appendix C: The Proc File System, pp 406-422
2. The Linux Kernel Module Programming Guide, Chapter 5
[1] Kernel Projects for Linux, Gary Nutt, Addison Wesley 2001
Problem Statement
Write a program to report the behavior of the Linux kernel by inspecting kernel status. The
program should print the following values on stdout:



CPU type and model
kernel version
amount of time since the system was last booted, in the form dd:hh:mm:ss



the number of processes that have been created since the system was booted
a list of load averages (each averaged over the last minute for 10 minutes)
report date (gettimeofday) and machine hostname
2- This problem asks you to solve a concurrency control situation using semaphores. Please do not
use other concurrency control tools, such as mutexes, condition variables, atomic memory,
telapathy, &c. Your solution should enforce the policies described below while avoiding
deadlock, starvation, livelock, and other bad things.
At dinner time, students must visit three different stations in order to collect everything needed
for a full meal:



The station for the tray and utensils (TrayAndUtensils)
The station for the salad or other side-dish (SaladOrSide)
The station for the entree (Entree)
Each station can accomodate some number, which is known in advance, of concurrent students.
Other students must wait in a single line at each station and advance in FCFS order. This gives
the configuration of the stations a set of a parameters



Number of concurrent students at TrayAndUtensils (NTU)
Number of concurrent students at SaladOrSide (NSaSu)
Number of concurrent students at Entree (NE)
Please assume that each station is capable of reaching the concurrency level specified by the
parameters. For example, please assume that any underlying buffer data structure can support
the level of concurrency specified above.
The student thread calls a function, getMeal() to obtain a meal. The meat of this function is
sketched out below:
void getMeal()
{
// Pre condition: Student is hungry, but empty handed student
// This must be first -- or SPILL!
getTrayAndUtensils();
// The order of these really doesn't matter, but the code
// imposes this arbitrary order
getSaladOrSide();
getEntree();
// Post condition: Time to chow down! The student has everything needed
//
This function returns, and the student grabs dinner
}
You should implement the following four functions, which you saw in use in getMeal() above:

void init (int Nta, int Nsasu, int Ne)
This function initializes any necessary state and will be called before any significant portion
of the cafeteria simulation. It should create and initialize semaphores, counters, &c.

void getTrayAndUtensils();
This function is called by a student when s/he wants to get the tray and utensils. The "work"
within the function should be a simple comment, "// Get tray and utensils". Your real task is
to manage the semaphores that ensure that the specified level of concurrency is not
exceeded.

void getSaladOrSide();
This function is called by a student when s/he wants to get the salad or side. The "work"
within the function should be a simple comment, "// Get salad or side". Your real task is to
manage the semaphores that ensure that the specified level of concurrency is not exceeded.

void getEntree();
This function is called by a student when s/he wants to get the entree. The "work" within the
function should be a simple comment, "// Get entree". Your real task is to manage the
semaphores that ensure that the specified level of concurrency is not exceeded.
3- Write a C program to create a two consumer two producer paradigm where the producers (say,
P1 and P2) and the consumers (say, C1 and C2) communicate using a single unnamed pipe. You
will use unbuffered read()/write() and pipe() system calls in this program The producers and
consumers work at different speeds; each value produced by a producer is passed to (one of) the
consumers via the pipe. A producer process should produce its values by reading them from its
own input file. And, a consumer should append the values that it consumes to its own output file.
After processing, say, 100 values, from the input files of the two producers, the output files of
consumers C1 and C2 will have these values, randomly distributed into one of the output files.
Use the sleep() function call with random values to simulate the producers and consumers
working at different speeds.
Download