Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CSC 209H Summer 2004: Assignment 2 (10% of final mark)

Due date: June 23rd 2004


Forks() on the Road
This assignment consists of two tasks that will give you your first exposure to systems
and multiprogramming. In both programs, if you detect an error, you must display the
string ERROR to standard output. You do not have to worry about errors in input for
this assignment.
Task 1: Rock-Paper-Scissors with Multiprogramming
The game of rock-paper-scissors has provided generations of student with hours of
entertainment. The game involves two players, each of whom simultaneously pick one of
three artifacts using their hands: rock, paper or scissors. Rock beats scissors, scissors
beats paper and paper beats rock. All other combinations result in ties.
Your task is to write a program (a2t1.c) that plays this game with the following
specification:
The main process of your program acts as a referee. It creates two child processes using
fork() and these represent the two players in the game. Each of the child processes uses a
random number generator to pick one of rock, paper or scissors. The child processes end
immediately with an exit() call that relays a status value back to the parent (i.e. referee).
This exit status conveys which of the three artifacts were picked by the child. You can
pick some set of integer values to represent each of the three but do remember to make
these symbolic constants using #define. The parent waits for both child processes to
terminate and then reports the winner on standard output (stdout). The referee prints one
of the following three:
CHILD PROCESS 1
CHILD PROCESS 2
TIE
How can we figure out which process caused wait() to unblock? Use the return value of
wait() to make this determination.
Task 2: Distributed Downloading of files on the Web
UNIX and LINUX systems have a very cool utility called wget that allows one to easily
download files from the web. For example, the following command (that you can run
from the shell) will download the UofT logo that is on the course web page and store the
file in the current directory:
wget http://www.cs.toronto.edu/~iq/csc209s/uoft.gif

Your task is to write a program (a2t2.c) that downloads a set of specified files using
fork() and the wget command. The main process of your program starts by obtaining an
integer from standard input. This represents the number of file URLs that will specified
by the user subsequently and will never exceed 10. The user will enter the URLs on
separate lines and these will never exceed 100 characters each. After reading this
information into your program, the main process will fork() the same number of children
as there are files to download and waits for all of these to complete before it terminates.
Each of the child process downloads one of the files using the wget command. To invoke
a command from within your program, use the system call system(). This function
takes in a string containing a command and executes it. For example, the following call
works similarly to the previous command:
system("wget http://www.cs.toronto.edu/~iq/csc209s/uoft.gif");
Note that this task requires basic string handling that will be covered in week 5. You will
have to read and store strings from standard input. Also, you will have to create the string
that specifies the command in the system() library function call by concatenating wget
and the URL you receive from the user.
Finally, in your paper submission, answer the following four questions:
1) Consider the statement: the distributed manner in which we are downloading
files will always improve performance as opposed to the case where we download
them sequentially within the same process. Is the statement true? Explain your
answer.
2) What happens if a child process never terminates and we had use wait() in the
parent process? How can we handle this problem? (You do not have to code this!)
3) Suppose the parent created 10 processes and waited for them sequentially. That is,
wait for the first child, the second child, and so on. Just before the first child exits,
what state are the other children in when child 1 is downloading a very large file
that takes a lot of time to download while the rest are downloading small files that
download quickly?
4) If we wanted to see orphan processes, how would we modify the specification of
the assignment?
Submission
All assignments in this course require you to submit all your source code electronically.
You are also required to hand-in a printed version of your code and the testing you
performed to the TA in the tutorial when the assignment is due. Failure to follow this
may result in a grade of 0 on the assignment. A marking scheme and specific submit
commands will be posted on the website prior to the due date. In addition, we may
perform electronic testing on your program. The conventions specified in this handout
should be sufficient but keep checking the newsgroup for updates/corrections.
Enjoy!

You might also like