Pipe Reading Incorrectly

Hey, I have an assignment in which I have to create two child processes that write to a pipe which the parent process reads. I have the processes set up, and the first child writes to the pipe and the parent reads it just fine, but it won't seem to read what the second child wrote properly. Here's my code:

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <time.h>
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <sstream>

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

using namespace std;

struct mes
{
        int num, id;
};

int main(int seed1, int seed2, int trials)
{
        char write_msg[BUFFER_SIZE];
        char read_msg[BUFFER_SIZE];
        int buffer=sizeof(mes);
        int pfd[2];
        int buf[2];
        int pid1;
        int pid2;
        int status;
        int count = 0;
        int min1=1000, max1=0, min2=1000, max2=0;
        int iterations = 2;
        int max=0;
        int min=1000;
        int* msg = new int[iterations+1];
        int* msg2 = new int[iterations+1];

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

        cout << "Pipe Created" << endl;

        pid1=fork();

        if(pid1==0)
        {
                cout << "Process Forked" << endl;
                msg[0]=getpid();
                cout << "Process ID Stored: " << msg[0] << endl;
                for(int i=1; i<iterations; i++)
                {
                        cout << "Entry " << i <<  " Stored" << endl;
                        msg[i]=(i*2);
                }
                close(pfd[READ_END]);
                cout << "Read End of Pipe Closed" << endl;
                write(pfd[WRITE_END], msg, buffer);
                cout << "Pointer Written to Pipe" << endl;
                close(pfd[WRITE_END]);
                exit(0);
        }
        else
        {
                wait(&status);
                cout << "Child Terminated" << endl;
                close(pfd[WRITE_END]);
                cout << "Write End of Pipe Closed" << endl;
                read(pfd[READ_END], msg, buffer);
                close(pfd[READ_END]);
                cout << "Pointer Read From Pipe" << endl;
                pid1=msg[0];
                cout << "PID Read From Pipe: " << pid1 << endl;
                for(int a = 1; a<=iterations; a++)
                {
                        cout << "Entry " << a << "Read" << endl;
                        if(msg[a]<min1)
                                min1=msg[a];
                        if(msg[a]>max1)
                                max1=msg[a];
                }

                pid2=fork();

                if(pid2==0)
                {
                        msg2[0]=getpid();
                        cout << msg2[0] << "PROCESS ID" << endl;
                        for(int i=1;i<=iterations;i++)
                                msg2[i]=rand()%1000;
                        close(pfd[READ_END]);
                        write(pfd[WRITE_END], msg2, buffer);
                        close(pfd[WRITE_END]);
                        exit(0);
                }

                wait(&status);
                read(pfd[READ_END], msg2, buffer);
                cout << msg2[0] << "NOT PROCESS ID" << endl;
                pid2=msg2[0];
                int i;
                for(i=1; i<=iterations; i++)
                {
                        if(msg2[i]>max2)
                                max2=msg2[i];
                        if(msg2[i]<min2)
                                min2=msg2[i];
                }

                //FINAL OUTPUT/////////////////////////////////////
                cout << "Parent Process ID " << getpid() << endl;
                cout << "-------------------------------------" << endl;
                cout << "Child 1 Process ID " << pid1 << endl;
                cout << "Maximum: " << max1 << endl;
                cout << "Minimum: " << min1 << endl << endl;
                cout << "Child 2 Process ID " << pid2 << endl;
                cout << "Maximum: " << max2 << endl;
                cout << "Minimum: " << min2 << endl;
        }

        delete msg;

        return 0;
}


There are some things that are in there from previous versions that I have yet to remove, but the main problem I'm worried about is getting the second child's data read properly. The output from the cout in the second process after msg2[0] is

18643PROCESS ID


whereas the output from right after msg2 is read from the pipe and msg2[0] is displayed is

1NOT PROCESS ID


How do I get the parent to start reading the pipe correctly after the first time? Thanks!
You closed the pipe
Topic archived. No new replies allowed.