CS360 Final Exam. Question 4

December 13, 1997

18 points

Your game program is such a success that you put the code on the internet, and people are playing it all over the country. You'd like to maintain a high-score server, which records the top 500 scores of all time. To do this, you put the following code into your game program:
process_high_score(char *name, int score, int month, int day, int year)
{
  int fd;
  char hsstring[100];
  char s[100];

  sprintf(hsstring,  "%10d %-10s %02d/%02d/%04d\n", score, name, 
                      month, day, year);
  /* Note that this string will have exactly 34 characters, including the
     null character */

  fd = request_connection("games.cs.utk.edu", 8000);
  if (fd >= 0) {
     write(fd, hsstring, 34);
     while (1) {
       j = read(fd, s, 100);
       if (j > 0) {
         write(1, s, j); 
       } else {
         close(fd);
         return;
       }
     }
   }
}
What this does is create a high score string, and send it to a high score server. The server receives the high score, and then sends back a big string which is the top 500 scores. The game program prints out this string.

Part 1

You write the high score server that is serving port 8000 at games.cs.utk.edu. Make it threaded. It should manage a data structure that holds the top 500 scores, and it should service connections from the above clients.

Hint: if process_high_score("Jim", 100000, 12, 13, 1997) is called, the string:

"    100000 Jim        12/13/1997\n"
will be sent to the server. Note that if you call atoi() on that string, it will return 100000.

Part 2

I am assuming that your program has a mutex that you are locking whenever you mess with the high-score data structure. This probably means that you are locking the mutex while you write the contents of the data structure to a socket connection. Why is this a bad thing performance-wise, and how could you fix it? (don't write code, just explain it).