Homework IV
Due Wednesday, May 18
The intent of this problem is to deepen your understanding of how
processes share the CPU. You will be using threads, a special
type of processes.
Our introductory example of threads is in the program
here. Read the comments, which will explain the
basics of threading. Compile and run the program, varying n and the
number of threads, before you go any further.
In this assignment, you will write a primitive Web probe. Using a
reference list of Web sites, it will repeatedly cycle through the list,
timing how long it takes to access each one. The goal of the program is
to gauge the current speed of the network. Here are the specs:
- Write the program in C or C++.
- The command-line format will be
webprobe urlfile wwidth numthreads
Here numthreads is the number of worker threads,
i.e. not including main(). The program will at all
times keep a record of the access times of most recent
wwidth probes.
- The command-line argument urlfile is a list of
URLs to be repeatedly probed. We'll call it the URL list.
- All but one of the numthreads threads will be
used in probing the URLs. Call these probe threads.
The remaining one, to be called the reporting thread,
will report results.
- When a probe thread finishes a Web access, it will go to the
next site in the URL list. Of course, when reaching the end of the
list, we wrap back to the top. Put the the index of the next URL to
be checked in a shared variable called nexturl.
- The report thread will update every 10.0/numthreads
seconds. (With more threads, we can get a reliable
report more often.) The report will consist first of the mean and
standard deviation of the most recent wwidth ("window
width") Web accesses. It will also report how many Web accesses
each thread has made so far. (The TA will take note of this if
most of these numbers are 0s!)
- A similar (though not quite identical in goal) program, coded in
R, is available here. You may wish to use
this to help formulate your plan to write the code in this
assignment. You don't need to know R to be able to understand the
gist of this code, but if you wish, I have a 5-minute introduction
for R for C programmers
here.
Note that the comments in that code mention a list of Web pages you
may wish to try, at
http://heather.cs.ucdavis.edu/TopList.
- Use system() with wget() as in
the R example. But unlike R, the C system() won't
return the times to you. So, call clock_gettime() before
and after. Here's an example of usage:
#include <time.h>
float timediff(struct timespec t1, struct timespec t2)
{ if (t1.tv_nsec > t2.tv_nsec) {
t2.tv_sec -= 1;
t2.tv_nsec += 1000000000;
}
return t2.tv_sec-t1.tv_sec + 0.000000001 * (t2.tv_nsec-t1.tv_nsec);
}
main()
{ int i;
struct timespec bgn,nd;
clock_gettime(CLOCK_REALTIME, &bgn);
scanf("%d",&i);
clock_gettime(CLOCK_REALTIME, &nd);
printf("%f\n",timediff(bgn,nd));
}
- Try your code both on the CSIF dual-core machines, and on Tetra
(tetra.cs.ucdavis.edu), a quad-core CSIF machine.
- Write a COGENT explanation, along the lines of
the "Gowtham and Noelle" story in class, of how the threads take
turns in this application. Explain why it may be useful to have more
threads than CPU cores for this program but not for the prime
finder above.
You are required to use LaTeX in this writeup.
(You'll be using it heavily in the final exam.) I have a tutorial
site for it
here. Submit both your .tex file and the
.pdf file you generate from it.