How to best share an array between parent and child processes after execvp()?

216 Views Asked by At

Suppose you have an array A shared in memory between processes X and Y, where X is focused on the first half of the array and Y is focused on the second half. Could X sort its half of the array "in place" on A at the same time as Y sorts its half? Its the same shared memory segment, but different addresses within that segment. Would this cause undefined behavior?

In other words, my professor said not to have processes change the shared memory at the same time, but is that only if they're changing data in the same location as each other?

If it matters, we are using C89 for our class.

EDIT: Some more context since what I'm asking may be an unnecessary solution (due to the XY problem).

We're writing a MergeSort program using multiple processes, shared memory, and execvp. Currently, we have two files main.c and merge.c where main reads in an input file containing the size and contents of the int array to sort. We are directed to store the array in a shared memory segment and call merge.c with execvp(). Then, merge.c should also use execvp() to call itself to split the array recursively, sorting once the array is two elements long, and "returning" to its parent and merging on the way up.

Here's where my problem comes in: I'm not sure how to "return" the sorted two element (and four element, etc.) array to its parent without using a new shared memory segment. Perhaps there is a simpler way to do this.

My apologies if my initial question was an XY problem.

0

There are 0 best solutions below