Managing Children Processes

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

using namespace std;

int main()
{
	int pid1;
	int pid2;
	int status;

	cout << "Beginning" << endl;
	cout << "#########" << endl << endl;

	pid1 = fork();
	pid2 = fork();

	if(pid1 == 0) //the first child
	{
		cout << "Process Child 1" << endl;
		cout << "PID: " << pid1 << endl << endl;
		exit(0);
	}

	if(pid2 == 0) //the second child
	{
		wait(&status);
		cout << "Process Child 2" << endl;
		cout << "PID: " << pid2 << endl << endl;
		exit(0);
	}

	else //the parent
	{
		wait(&status);
		cout << "Process Parent" << endl;
	}

	return 0;
}


The output is


Beginning
#########

Process Child 2
Process Child 1
Process Child 1
PID: 0
PID: 0


PID: 0

Process Parent


A lot of the code came from the professor, with some modifications. Any advice or help is greatly appreciated!
¿what's your question?

1
2
3
4
5
6
7
8
9
	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.
Thus far I have gotten this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

#define READ_END	0
#define WRITE_END	1
#define BUFFER_SIZE	25

using namespace std;

int main(int seed1, int seed2, int numRan)
{
	char write_msg[BUFFER_SIZE];
	char read_msg[BUFFER_SIZE];
	int fd[2];
	pid_t pid;
	int pid1;
	int pid2;
	int status;
	int num1;
	int num2;

	cout << "Beginning" << endl;
	cout << "#########" << endl << endl;

	pid1 = fork();//create the first child

	if(pipe(fd) == -1)//create the pipe
	{
		fprintf(stderr, "Pipe Failed");
	}

	if(pid1 == 0) //the first child
	{
		cout << "Process Child 1" << endl;
		cout << "PID: " << pid1 << endl << endl;

		close(fd[READ_END]); //close unused pipe end

		write_msg = getpid();

		write_msg = *write_msg + num1;

		write(fd[WRITE_END], write_msg, strlen(write_msg)+1);

		cout << "Wrote message: " << write_msg << endl;

		close (fd[WRITE_END]);

		exit(0);
	}

	else //the parent
	{
		wait(&status);
		cout << "Process Parent" << endl;

		pid2=fork();

		if(pid2 == 0) //second child
		{
			cout << "Process Child 2" << endl;
			cout << "PID: " << pid2 << endl << endl;
			exit(0);
		}

		wait(&status);
		cout << "Process Parent Again" << endl << endl;
	}

	return 0;
}


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.
Topic archived. No new replies allowed.