Question: Instruction Modify your solution from the pa 3 - shared memory assignment to use threads instead of shared memory. You will need to redeclare the
Instruction
Modify your solution from the pashared memory assignment to use threads instead of shared memory. You will need to redeclare the array in as a local storage instead of shared memory. An example of a thread call is provided in thread.cc
You will need to compile your code as:
go thread thread.cc lpthread
Requirements
Modify thread.cc to spawn eight threads.
The program should assign oneeighth th of the array in content to each thread.
Each thread should be responsible for sorting its assigned portion of the shared memory.
Shared.cc:
Essa Imhmed
August
simple implementation of mergesort.
Input: from a method that generates random numbers between and
Output: the data in sorted order
Notes: Since mergesort actually relies upon external storage to
store the merge of two segments, we use an additional array to
store the merge called "out and then place the results
back into in for subsequent merges.
Alena Fisher
pa Shared Memory
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
value to size the arrays
#define MAXNUM
#define NUMPROCESSES Number of child processes for sorting
declare the arrays
int in; pointer to the shared memory segment
std::vector outMAXNUM; internal array for merging
sid::vector inMAXNUM;
struct Thread
int val;
int val;
Merge function that merges two sorted halves of the array
void mergeint loc int loc int loc int loc
int i loc j loc insert loc;
while i loc && j loc
if ini inj
outinsert ini;
else
outinsert inj;
Handle any remaining elements
while i loc
outinsert ini;
while j loc
outinsert inj;
Copy merged data back to the original array
for i loc; i loc; i
ini outi;
Recursive sort function
void sortint loc int loc
if loc loc return; base case
int mid loc loc;
sortloc mid; sort the first half
sortmid loc; sort the second half
mergeloc mid, mid loc; merge the sorted halves
Helper function to print the array
void printarint NUM
for int i ; i NUM; i
std::cout ini;
std::cout std::endl;
Function to generate random numbers to fill the array
void generateRandomNumbersint count
Seed for random number
srandstaticcaststd::timenullptr;
Filling the array with random numbers between and
forint i ; i count; i
ini std::rand;
int main
int sharekeyin; ID
pidt childrenNUMPROCESSES; Array of elements
struct shmidds item;
Get shared memory
if sharekeyin shmgetIPCPRIVATE, MAXNUM sizeofint IPCCREAT
std::cerr "Cannot get shared memory" std::endl;
return ;
Attach shared memory segment
if in int shmatsharekeyin nullptr, SHMRNDvoid
std::cerr "Cannot attach to shared memory" std::endl;
return ;
Calling the method to generate random numbers
generateRandomNumbersMAXNUM;
Printing the unsorted array
std::cout "Unsorted array: std::endl;
printarMAXNUM ;
Determining the segment size for each process
int segmentsize MAXNUM NUMPROCESSES;
Fork child process
forint i ; i NUMPROCESSES; i
ifchildreni fork
Starting index for the segment
int start i segmentsize;
End index for the segment
int end std::minstart segmentsize MAXNUM ;
ifstart MAXNUM
Sorting the segment
sortstart end;
exit;
Parent process waiting for child processes to finish
forint i ; i NUMPROCESSES; i
waitpidchildreni nullptr, ;
Merging the sorted segments into a single array
forint segmentsize ; segmentsize MAXNUM; segmentsize
forint i ; i MAXNUM; i segmentsize
int start i; Starting index of the first se
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
