CSCI 237
Computer Organization
Home | Lectures | Labs | CS@Williams
Lab 7: A Simple Dictionary Client
Assigned | May 14/15, 2025 |
---|---|
Due Date | Sun, May 18, 2025 by 11:59pm. Submit your final
version of dictclient.c .Late submissions will not be accepted!!! |
Files | Starter code: lab7.tar |
Submissions | Submit your solutions using submit237 7 dictclient.c . If you work with
a partner, only one submission per pair is required. |
Overview
In this lab, you will gain a little experience with sockets and
threads. You will complete the implementation of a simple multi-threaded client
that supports the dict protocol.
I encourage you to work with a partner on this lab. Groups of three or more are not permitted.
If you choose to work with a partner, make sure that both members of the group
are contributing equally.
Instructions
Start by extracting lab7.tar
to a
directory where you plan to do your work:
$ wget http://www.cs.williams.edu/~jeannie/cs237/labs/lab7/lab7.tar
$ tar xvf lab7.tar
This will cause a number of files to be unpacked in a directory
called lab7. The only file you will modify and turn in
is dictclient.c
. You can compile your code with the provided Makefile.
The dict procotol is fully described in this RFC. In a nutshell, you can connect to one of their servers (which mostly run on port 2628), and use a simple text-based protocol to look up the definition of words in various dictionaries (or databases). For example, after connecting to dict.org at port 2628 using telnet, you can ask for the definition of the word "dog":
-> telnet www.dict.org 2628 Trying 216.18.20.172... Connected to dictionary-server.dict.org. Escape character is '^]'. 220 pan.alephnull.com dictd 1.12.1/rf on Linux 4.4.0-1-amd64<21101961.7401.1525352621@pan.alephnull.com> define wn dog
This will return the following results:
150 1 definitions retrieved 151 "dog" wn "WordNet (r) 3.0 (2006)" dog n 1: a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds; "the dog barked all night" [syn: {dog}, {domestic dog}, {Canis familiaris}] 2: a dull unattractive unpleasant girl or woman; "she got a reputation as a frump"; "she's a real dog" [syn: {frump}, {dog}] 3: informal term for a man; "you lucky dog" 4: someone who is morally reprehensible; "you dirty dog" [syn: {cad}, {bounder}, {blackguard}, {dog}, {hound}, {heel}] 5: a smooth-textured sausage of minced beef or pork usually smoked; often served on a bread roll [syn: {frank}, {frankfurter}, {hotdog}, {hot dog}, {dog}, {wiener}, {wienerwurst}, {weenie}] 6: a hinged catch that fits into a notch of a ratchet to move a wheel forward or prevent it from moving backward [syn: {pawl}, {detent}, {click}, {dog}] 7: metal supports for logs in a fireplace; "the andirons were too hot to touch" [syn: {andiron}, {firedog}, {dog}, {dog- iron}] v 1: go after with the intent to catch; "The policeman chased the mugger down the alley"; "the dog chased the rabbit" [syn: {chase}, {chase after}, {trail}, {tail}, {tag}, {give chase}, {dog}, {go after}, {track}] . 250 ok [d/m/c = 1/0/16; 0.000r 0.000u 0.000s] q 221 bye [d/m/c = 0/0/0; 461.000r 0.000u 0.000s]
Type "q" to disconnect. Similarly, you can say:
define moby-thesaurus computer
This will return the following results:
150 1 definitions retrieved 151 "computer" moby-thesaurus "Moby Thesaurus II by Grady Ward, 1.0" 50 Moby Thesaurus words for "computer": IBM machine, IDA, Teleplotter, Telereader, abacist, accountant, actuary, adder, analog computer, analytical control unit, analyzer, bookkeeper, calculator, coder, collator, compiler, computer hardware, computer unit, data processor, decoder, detector, differential, differential analyzer, digital computer, digital graph plotter, divider, electronic brain, electronic computer, estimator, figurer, hardware, information machine, integrator, memory tubes, multiplier, phase discriminator, position coder, printer, printing calculator, receptor, reckoner, relay, selective calculator, selector, statistician, storage unit, telecomputer, thinking machine, transmitter, tristimulus computer . 250 ok [d/m/c = 1/0/15; 0.000r 0.000u 0.000s]
I encourage you to try a few examples on your own to better understand how the protocol works.
Part A
Though there are other word databases, for this lab, we will be using the moby-thesaurus word database. In particular, your task for Part A is to take a single word as input and find its first synonym as returned by the server. Here is an example output:
-> ./dictclient Enter a word: computer Synonym: IBM machine
The dictclient.c
file I have given you contains some
helper functions for parsing the output from the server. In particular, you will
find the find_synonym() function to be very useful. Your task is to fill in
setup_socket() and part_a() functions to perform the following tasks:
Part B
Finding synonyms is fun, but it's more fun to generate entire phrases or even sentences using the results returned from moby-thesaurus. Thus rather than using a single word as input, in Part B you will take a simple phrase and substitute each word in the phrase with its first synonym as returned by the server. The result will be some pretty silly and weird sentences.
Of course, there is a catch. Rather than iteratively looking up each word in the phrase, we want to exploit parallelism. Thus each word should be sent to a separate thread. Each thread will create a socket, lookup the word passed to it, and store/return the result. We must make sure we read the results in the same order as the threads were created, however. Here is some sample output:
-> ./dictclient Enter phrase: The green car is fast New phrase: The Astroturf Pullman is Barmecidal feast -> ./dictclient Enter phrase: the quick brown fox jumped over the lazy dog New phrase: the Daedalian Argos brown African hunting dog jumped SOL the Micawberish Afghan hound
Using your solution from Part A, only a few minor changes are necessary to implement Part B. Specifically, you should:
pthread_join
to force your main
thread to wait for all peer threads to complete.
Remember that we have to avoid data races. Be careful when passing
shared variables around between threads. We malloc space on the heap for each word.
We also copy the resulting synonyms back
to the heap as well. Note that use of memcpy
for copying results
to specific locations in memory. Be sure to free
as needed.
You might find it useful to look at echoserver-threads.c, a multi-threaded server, echoclient.c, a simple client, and hello.c, a simple threaded program, for guidance. Note that you are making a multi-threaded client, not a server.
You should be able to complete this lab during the lab session if you use your time wisely.
Evaluation
Your grade will be determined by the quality of your code (style) as well as correctness.
Getting Started