$29
This is the story of the stash of truffles and the 5 little piggies (and 1 mama pig).
Now the mama pig and the 5 little piggies went for a stroll in the woods one day, looking for some truffles to eat (or anything for that matter, they are pigs…). Lo and behold they found someone’s stash of truffles. The little piggy eyes grew huge with desire. But, mama pig knew that their little piggy eyes were much bigger than their little piggy tummies, so she set some rules…
P.S. Not quite as sophisticated as the five dining philosophers, but it will suffice. And, it will require some REALLY deep thinking (Like what are truffles? Why are they good to eat? Why pigs?).
We will simulate the feast as follows:
The mama pig will get the data filename as a command-line argument. The mama pig will then create the 5 pipes, 1 semaphore and 1 shared memory (initialized to 0). The mama pig will then make sure the semaphore is free (unlocked). The mama pig will then fork & exe the five little piggies and pass on to them the data filename, the pipe identifiers, semaphore identifier, and the shared memory identifier as command-line arguments.
Note that since pigs are not very creative, the mama pig simply named her little piggies: _p1, _p2, _p3, _p4, & _p5.
Note that the data file will just be a file of characters, so you might want to test on your code on something like:
Once upon a midnight dreary, fingers cramped and vision bleary,
System manuals piled high and wasted paper on the floor,
Longing for the warmth of bedsheets,
Still I sat there, doing spreadsheets:
Having reached the bottom line,
I took a floppy from the drawer.
Typing with a steady hand, I then invoked the SAVE command But got instead a reprimand: it read "Abort, Retry, Ignore"
Whenever a little piggy attempts to run it will first check the semaphore and if it is free (unlocked) it will lock the semaphore and continue; otherwise, it must wait until the semaphore is unlocked. A little piggy that gets to continue will then generate a random number N between 1 and 10 (inclusive) and read N characters from the file, pass its name (_p1, etc) followed by those characters to the mother pig, and add the value of N to the value in the shared memory.
The mama pig will extract that data from the pipe, write it to a file (hw10.out), and free the semaphore.
Thus, the hw10.out file will have, for example, characters such as:
_p1hel_p4lo
Once all of the truffles (data) have been consumed, the little piggy who got the last morsel, needs to pass the characters back to the mama pig, acquire the value from the shared memory and pass it back to the mama pig (who will then append it to the file), and then somehow signal the rest of the pigs that the truffles (data) are all gone so they can ALL shut down (take a siesta?). Note that this piggy may generate a random number larger than the number of characters left, you will need to watch for EOF to prevent a seg fault. Also, since you could encounter the EOF before reading all of the characters, the number returned to the mama pig will always be equal or greater than the actual number of characters read.
So on completion, the hw10.out file might look like:
_p1hel_p4lo6
This way the mama pig will have a record of who is the biggest pig of the bunch!
REQUIREMENTS:
----------------------
Your program must run on the cscluster (cscluster.cs.und.edu).
Your full name must appear as a comment at the beginning of your program. Your parent code must be named hw10-yourname.c or hw10-yourname.cpp
You really only need one child, named p.c, but you can create 5 children (named pN.c or pN.cpp,
where N is 1 through 5) if you want.
You must make a tarball of the 6 programs and name that tarball hw10-yourname.tar Email your source (subject hw10-yourname) to rmarsh@cs.und.edu