Академический Документы
Профессиональный Документы
Культура Документы
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