Hey, I'm currently working on an assignment in which we have to create two child processes, have them each generate random numbers, write those numbers to a pipe with their process id, then have the parent process show the highest and lowest numbers produced by each process. I'm working on handling the two child processes right now, and this is what I have:
pid1 = fork(); //creates a child
pid2 = fork(); //both the parent and the child execute this
//so now you've got 4 process
if(pid1 == 0)
{
//if you are here then pid1 is 0, ¿what's the purpose on printing that?
cout << "Process Child 1" << endl;
cout << "PID: " << pid1 << endl << endl;
I'm just not sure how to go about making only two child processes, and then having them do different things. That's the code from the professor I was using as a base, but I can see now it isn't working so well.
Now, the processes have been separated, but now I'm starting on the pipe, on which the children are going to write their PID and a random number, which the parent will read. To be honest, don't really know what the code is doing or how it works, the professor just gave us some example files to use, and the book isn't helping either. I'm just really lost in this class, and any help or explanation is appreciated.
I'm just not sure how to go about making only two child processes, and then having them do different things.
You create a parent program that starts two other programs.
And that's what your professor's sample code does. Your code creates one child.
I'm currently working on an assignment in which we have to create two child processes, have them each generate random numbers, write those numbers to a pipe with their process id, then have the parent process show the highest and lowest numbers produced by each process.
A pipe is a suitable IPC mechanism for parent/child communication. Go back to the sample provided. You need to:
1. create the a pipe for each child process.
2. as both children do the same thing (generate random numbers), you can use the same code for both children. Make each process write to one enc of their respective pipes.
3. The parent reads each pipe and processes the results.