2

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.

zq_Aux
  • 21
  • 7
  • If the processes don't interact via the memory then why used shared memory in the first place? The point your professor is making probably refers to actual practical cases where the memory truly is shared in their access patterns. If there are no overlapping accesses ever then the two processes are operating distinctly and there won't be any concurrency issues. – kaylum Oct 17 '20 at 03:08
  • @kaylum Hopefully I'm not falling into an XY problem... I am attempting to provide the parent of X and Y a means of accessing the data its children modified. – zq_Aux Oct 17 '20 at 03:11
  • Well then you have three processes and the parent accesses need to be synchronised with the child accesses. But the child processes don't need to worry about each other if they are accessing distinct portions of the shared memory. – kaylum Oct 17 '20 at 03:12
  • @kaylum So my method should work, correct? The reason I'm not doing something more simple is we're told to use execvp for each fork, so I'm not sure how else to communicate between processes. – zq_Aux Oct 17 '20 at 03:16
  • Maybe this is an XY problem. Perhaps you should state the original problem. Simple communication between parent/child processes commonly use [pipe](https://linux.die.net/man/2/pipe). – kaylum Oct 17 '20 at 03:18
  • Thanks @kaylum, I've added more context. – zq_Aux Oct 17 '20 at 03:26
  • 1
    You are right - if you can guarantee that no two processes access the same part of the shared memory at the same time then your implementation will be fine. Which you can do with merge sort as long as each parent does not continue until the two children complete. – kaylum Oct 17 '20 at 03:36

0 Answers0