Sure... I can help you a little with your homework!
When you use
fork(), a process
literally "splits itself in two." One of them perceives that the function-call returned
true while the other one perceives that it returned
false.
So, the
first part of your assignment is to figure out how to correctly split-off
n copies of yourself.
The second part of your assignment is to figure out how to cause the various split-off copies to be able to accomplish a task cooperatively,
i.e. without smashing into each other because they are all trying to access the same shared data-structures (and what not...) at the same time. Here is where the idea of "semaphores" comes into play. A semaphore is like those "take a number" gadgets that you find at popular meat-counters in popular grocery stores: everybody "takes a number" so that everybody gets served, "fairly, but one-at-a-time." The mechanism allows each process to "take a number, wait patiently in line, then proceed when their 'turn' arrives."
At
this point, I turn you over to the
vast amount of material that is at your beck-and-call in this instantaneous worldwide research library called "the Internet." Or, as the case may be, to your instructor.
(And let me be clear on this:
I have been an instructor myself. If
you are confused, you're not the only one.
Go to your instructor and talk to him or her. This will serve, in part, as extremely valuable feedback and guidance to him or her.
Don't feel embarrassed or reticent: this process is called
learning, and it's a two-way street. Do your part to expedite the process.)