Remember that you can check that your attendance has been recorded properly at https://wwwcgi.teach.cs.toronto.edu/~ajr/cgi-bin/auth/present
Run it twice at once with a command like "./a.out aaaaa & ./a.out bbbbb".
Which one "wins"? How much do you have to increase the number of lines by to get a file containing some of each output interspersed? (not necessarily reliably)
You might find the program "bfreq" which we used in lab 03 to be helpful in evaluating the "output" file, especially once it gets large.
alias bfreq=/u/csc209h/summer/pub/lab/03/bfreq
1b) Use the flock() function to block until no one else is accessing the file. Does this help? What is happening?
1c) After flock() returns but before writing to the file, seek to the end of the file with "fseek(fp, 0, SEEK_END)". Does this help? What's happening here? Is this a reliable solution?
You will write a program to produce input to anagrammer. Your program will be called "speaker.c". It will be linked with wordlist.c. It outputs a random selection from "wordlist" in wordlist.c every second for ten seconds as follows:
read(fd, &r, 1)where fd is the value returned from open at the beginning. 'r' must be unsigned char, not just char. Check that read() returns 1; else this is a fatal error.
Please see shell scripts 1, 2, 3, and 4 in /u/csc209h/summer/pub/lab/12/computation .
Each one of these performs some sort of calculation. The idea is that some
of the calculations are time-consuming, but we have a multi-CPU computer so we
want to execute them in parallel.
(The use of "sleep" here is just faking the time-consuming calculation.)
Copy these files to a directory of your own. Write a shell script named "compute" which executes "sh 1", "sh 2", "sh 3", and "sh 4", all in parallel, with their outputs redirected to files named "1-result", "2-result", etc. (For this exercise we will cheat and write files in the current directory.)
After all of the processes have terminated, add the four output values together and output the sum.
The output of your shell script should be just this single number. And try doing
date; sh compute; dateto make sure that it only takes about five seconds to run (the max of all the running times), not nine seconds (the total of all the running times).
(Note: Your solution should be extremely short. Mine is six lines, although yours doesn't have to be THAT short.)
4a) Attempt to protect it against concurrency problems with a lock file:
Does this work any better? How well does it protect against concurrency problems? Why is there still an issue?
4b) Examine the C program /u/csc209h/summer/pub/lab/12/makelockfile.c
(compiled in /u/csc209h/summer/pub/lab/12/makelockfile)
Its exit status is backwards so that it is convenient to do
"while makelockfile"
Use this to do a lock file for addten properly.
Does this work? Why is it so much better than the previous version?
4c) Use the "flock" command-line tool (see "man flock") to do a lock properly without using a custom external C program. (It's still using an external C program, but it's one which "comes with linux".)
First of all, you must run "/u/csc209h/summer/present", during the tutorial time, on the console of a tutorial lab workstation, or get the TA to mark you as present.
Then, by the end of Friday August 5, submit both speaker.c and compute using the usual 'submit' command, with "assignment" name "lab12". As always, speaker.c must compile with "gcc −Wall" with no error or warning messages.