ehrugo 0 Newbie Poster

The program I'm doing randomly generates 10 numbers per group. While there will always be 10 numbers in each group, how many groups are to be made is user-specified and is thus static.

I can generate the numbers from the userinput just fine, I ran a test and had it print out each element of the generated character array and all the numbers came out nice and unique.

The number groups are being generated in two child processes. If a user specifices 2 number groups need to be generated, each child will generate 2 number groups and send it to the parent, resulting in a total of 4 number groups.

However, this needs to be done using a pipe variable and the read and write function. This is where I get lost. Each child will write the numbers it generates into the pipe which will be read by the parent. Each number group can be written at once, or one individual number at a time.

Here's one of my child processes:

if (pid == 0) // child
{
int k = 0;
int numGroups = atoi(argv[4]); // from the command-line that location specifies how many groups to make
int numRun = 10 * numGroups; // for-loop parameter, generates all the number groups at once
seed1 = atoi(argv[2]));
srand(seed1);
                           
for (k = 0; k < numRun; k++)
{
          array2[k] = rand();
          buffer[k] = array2[k];
          write(pipeName[1], &buffer[k], sizeof(buffer[k]));
}
}

How do I begin to differentiate between the numbers the parent reads? I need to differentiate between them in to find the lowest and highest number in each group, and if I use the same loop for the other child with a different seed, it will overwrite everything in buffer.

Any help you can offer is greatly appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.