Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Merge Sort with Unix Shared Memory function MergeSort ( a [ ] , lower _ bound, upper _ bound ) { int middle; if (
Merge Sort with Unix Shared Memory
function MergeSorta lowerbound, upperbound
int middle;
if upperbound lowerbound
if alowerbound aupperbound
swap alowerbound and aupperbound;
return;
now we have more than entries
middle lowerbound upperbound;
MergeSorta lowerbound, middle; recursively sort the left section
MergeSorta middle upperbound; recursively sort the right section
Mergea lowerbound, middle, upperbound;
merge the left and right sections
return;
The two calls to Mergesort recursively split the give array section into two sorted sections.
The job for program merge.cc to do is the following:
When merge.cc runs, it receives the left and right indices Left and Right and other information from its command line arguments.
Then, it splits the array section aLeftRight into two at the middle element aM After the split is obtained, two child processes are created, each of which runs merge.cc using execvp The first child receives Left and M while the second receives M and Right. In this way each child process performs a merge sort on the given array section. The parent then waits until both child processes complete their job.
After this, program merge.cc uses the modified binary merge method to merge the two sorted sections as shown below.
a merge.cc creates a process for each entry in the two array sections, and waits for the completion of all of these child processes.
b Each child process uses the assigned array entry to search the other array in order to find its final position in the merged array. Note that these child processes should not store the assigned entry back to the given array. Find a place to store this result array.
After all child processes complete, the merged and sorted array must be copied back to shared memory, overwriting the original. At this point don't forget to remove those temporary arrays. Then, merge.cc exits.
You may use multiple shared memory segments, and you must use the execvp system call to solve this problem. You must use the indicated modified binary search to determine the position of xi in y and the position of yj in x
The process structure must be as specified.
The input to program main.cc is in a file with the following format:
n the number of elements of array a
a a a an elements of array a
If you use more than one shared memory segments, you should repeat the output of shared memory segment and indicate the use of each.
Your output should look like the following, where XXXXX is a description of the purpose of the created shared memory.
MERGE: shared memory key
MERGE: shared memory created
MERGE: shared memory attached and is ready to use for XXXXX purpose.
Other Output
MERGE: XXXXX shared memory successfully detached
MERGE: XXXXX shared memory successfully removed
NOTE: The output lines from main.cc always starts with MAIN: from
column ie no leading space
Messages from merge.cc have an indentation of three spaces and started with ###
MPROCabcd: where abcd is the PID of this particular merge sort process. Data
values printed by a merge sort process has the same indentation as that of ### MPROCEabcd: and each integer must printed using four positions, right aligned.
Each process created for binary merge has an indentation of six spaces and
must start with $$$ BPROCabcd
A temporary array temp always start with rather than using the same indexsubscript as that of the input array. All programs must be written in C The following approach will be used to compile and test your programs:
make make your program.prog inputfile test your program
Your Makefile should generate two binary files: prog and merge, corresponding to source file main.cc and merge.cc A README.txt file is always required.
A file named README.txt is required to answer the following questions:
The logic of your program
Explain the allocation and use of each shared memory segment.
Are there potential race conditions in your program?
Why you should not save the assigned array entry back to the given array in the binary merge phase?
Explain how you allocate a temporary array to hold the intermediate result in each execution of Mergesort
We assigned a process to each array entry every time when Mergesort is executed to perform binary merge. Mergesort waits for all of these processes before terminates itself. As a result, we repeatedly create and terminate n processes logn times, which is a waste of time and system resource. Suppose you are allowed to use busy waiting. Can you only create n processes at the beginning of the MERGE phase so that they can be used in each binary merge?
filenames:
main.cc
merge.cc
Makefile
README.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started