Вы находитесь на странице: 1из 4

Homework 1

This assignment is due 2016-11-08.


(The starred questions are optional and will count toward extra credit.)
NB: The work must be done individually and submitted in hard copy typeset
with LATEX 2 using Latin Modern. Each question carries 20 points.
Add \usepackage{algorithm2e} in your preamble (to invoke the
algorithm2e.sty package) for typesetting pseudocode.
1. On a certain Linux system, the e-mail for a user Harry Bovik is stored
in /home/bovik/mail. Harry is also an admin on that system (which is
left on 247 even when no one is using it), and decides to take a weekly
backup of his e-mail as a contingency against possible loss. He specifically decides that his e-mail should be backed up starting at 23:59 local
time each Saturday, and saved in a single compressed tarball bearing
the current ISO 8601 date as its filename (for example, a file saved on
November 5th, 2016 being called 2016-11-05.tar.bz2). The tarball
created is also to be written to a device named /media/backup, which
is a network drive, and all earlier backup files are to be erased to avoid
unnecessary duplication. Give a clear working procedure that shows
how Harry can accomplish this.
NB: The easiest way to do this is to use a shell script (which you will
have to create) invoked by a cron job. Show how this can be done.
2. As discussed in class, strict alternation (using a variable turn that
could take the values 0 or 1) can be used to enforce mutual exclusion
among two processes p0 and p1 .
A. Generalize the idea to get a procedure for mutual exclusion among
n processes p0 through pn1 .
B. Generalize further, to create a procedure for l-exclusion, where
n > l 2.
For both of the above, write suitable pseudocode, and prove if the
three desired properties are satisfied with it.
3. A file is to be shared among different processes, each of which has a
unique process id. The file can be accessed simultaneously by several
1

processes, subject to the constraint that the sum of all unique pids
associated with the processes currently accessing the file must be less
than some n. Write pseudocode for a controller that coordinates access
to the file.
4. Consider a modified producer-consumer problem where there are m
producers and n consumers, where m, n 2; producers and consumers
have unique ids 0 i m 1 and 0 j n 1 respectively. Each
producer has a separate buffer that it fills; when a consumer wants to
consume, it checks the buffers of producers starting from 0, and gets
an item from the first available non-empty buffer. In case all buffers
are empty, it sends a wakeup signal to all producers. If a buffer is
full, the relevant producer sends a wakeup signal to all consumers.
A consumer sleeps when all buffers are empty, and a producer sleeps
when its own buffer is full.
A. Give nave pseudocode (without semaphores or other synchronization primitives) of the code for producer i, and similar pseudocode for consumer j.
B. Now carefully analyze all the problems that can occur with the
above. (Note that there are at least three new problems that did
not arise with the single-producer/single-consumer case.) Give a
sketch of an improved solution that addresses these problems.
5. The Sleeping Barber Problem is a standard IPC problem. Consider
a sleeping-barber scenario where barbers may also join and leave the
shop, subject to the constraint that the number of barbers (awake and
sleeping) present in the shop must be no less than 1 and no greater
than some n > 1. Give pseudocode and an explanation for a solution
of this problem.
6. (*) Consider the following code which uses fork():
main()
{
int i = 0;
if (fork()) {
int j;
for (j = 0;j < 10; j++) { puts( "x" ); }
exit();
2

} else {
int j;
for (j = 0;j < 10; j++) { puts( "y" ); }
exit();
}
}
A. Understand what this code does, and explain its working.
B. Indicate how an equivalent of the same can be implemented without fork(), using Pthreads.
7. (*) Look up the standard C rand() function. Using the same, create
a modified function zrand() which does not have the srand feature
at all, but internally uses srand to seed the random function with the
pid of the calling process. Use this function in a test program and
submit the code and results of the same.
8. (*) Write a C procedure getTime() that can be used to return the
execution time of code segments by calling it before the code segment
is executed and again after the code segment is executed; for example:
double getTime(int);
start = getTime(-3);
/* code segment being timed goes here */
stop = getTime(-3);
elapsedTime = stop - start; // in milliseconds
Use the Unix kernel call gettimeofday() to read the clock on your
host system. The parameter (-3 in this case) specifies the resolution
of the time. If the parameter is i, then the length of time for one time
unit is 10i . (If i = 3 then the time is in milliseconds, and if i = 6,
it is in microseconds.)
A. Experiment with your routine to find out the smallest clock resolution that can be obtained on your machine, and report the
results (including the values obtained). What happens when an
even smaller resolution is attempted?
B. Write a simple program to generate the first 10 million prime
numbers. Generate statistics (based on at least 20 trials) using
your routine for the execution time of this program, such as minimum, maximum, average, and standard deviation.
3

9. (*) Run the CoreMark benchmarking software (available by registration from http://www.eembc.org/coremark/download.php) on your
machine running Linux (any distribution, kernel 3.* or later), and on
a VM running Linux (kernel 3.* or later, any host OS). Compare and
report the results obtained.
10. (*) The factor program is a small part of the larger set forming
the GNU Coreutils (http://www.gnu.org/software/coreutils/),
which itself is standard on most distros.
A. Use gcov and gprof to test this program (with several large input
values as needed) and report the results, and your conclusions
drawn from them.
B. Use the factor source code carefully as part of a larger program
that takes inputs m and n from stdin, and gives the prime factors
of all numbers in the range from m to n. Use this larger program
to compute in one stroke the prime factors of all numbers from
1020 + 1 to 1020 + 100.
C. Does the factor source code adhere to the ISO/IEC 9899:1990
(aka C90) standard? (Hint: Look up the -ansi and -pedantic
flags with GCC.) If not, modify the source code appropriately so
that it is within the standard.
srao@iiitb.ac.in

Вам также может понравиться