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 pa3-shared 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:
g++-o thread thread.cc -lpthread
Requirements
Modify thread.cc to spawn eight threads.
The program should assign one-eighth (1/8th) 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 18,2024
simple implementation of mergesort.
Input: from a method that generates random numbers between 1 and 100
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
pa3- Shared Memory
10/30/2024
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* value to size the arrays */
#define MAXNUM 10000
#define NUM_PROCESSES 8// Number of child processes for sorting
/* declare the arrays */
int *in; // pointer to the shared memory segment
std::vector out(MAXNUM); // internal array for merging
sid::vector in(MAXNUM);
struct Thread {
int val1;
int val2;
}
/* Merge function that merges two sorted halves of the array */
void merge(int loc1, int loc2, int loc3, int loc4){
int i = loc1, j = loc3, insert = loc1;
while (i <= loc2 && j <= loc4){
if (in[i]<= in[j])
out[insert++]= in[i++];
else
out[insert++]= in[j++];
}
// Handle any remaining elements
while (i <= loc2)
out[insert++]= in[i++];
while (j <= loc4)
out[insert++]= in[j++];
// Copy merged data back to the original array
for (i = loc1; i <= loc4; ++i)
in[i]= out[i];
}
/* Recursive sort function */
void sort(int loc1, int loc2){
if (loc1>= loc2) return; // base case
int mid =(loc1+ loc2)/2;
sort(loc1, mid); // sort the first half
sort(mid +1, loc2); // sort the second half
merge(loc1, mid, mid +1, loc2); // merge the sorted halves
}
/* Helper function to print the array */
void printar(int NUM){
for (int i =0; i <= NUM; ++i)
std::cout << in[i]<<"";
std::cout << std::endl;
}
/* Function to generate random numbers to fill the array*/
void generateRandomNumbers(int count){
// Seed for random number
srand(static_cast(std::time(nullptr)));
// Filling the array with random numbers between 0 and 100
for(int i =0; i < count; i++){
in[i]= std::rand()%100;
}
}
int main(){
int share_key_in; // ID
pid_t children[NUM_PROCESSES]; // Array of 8 elements
struct shmid_ds item;
/* Get shared memory */
if ((share_key_in = shmget(IPC_PRIVATE, MAXNUM * sizeof(int), IPC_CREAT |0666))<0){
std::cerr << "Cannot get shared memory" << std::endl;
return 1;
}
/* Attach shared memory segment */
if ((in =(int *)shmat(share_key_in, nullptr, SHM_RND))==(void *)-1){
std::cerr << "Cannot attach to shared memory" << std::endl;
return 1;
}
// Calling the method to generate random numbers
generateRandomNumbers(MAXNUM);
// Printing the unsorted array
std::cout << "Unsorted array: "<< std::endl;
printar(MAXNUM -1);
// Determining the segment size for each process
int segment_size = MAXNUM / NUM_PROCESSES;
// Fork child process
for(int i =0; i < NUM_PROCESSES; i++){
if((children[i]= fork())==0){
// Starting index for the segment
int start = i * segment_size;
// End index for the segment
int end = std::min(start + segment_size -1, MAXNUM -1);
if(start < MAXNUM){
// Sorting the segment
sort(start, end);
}
exit(0);
}
}
// Parent process waiting for child processes to finish
for(int i =0; i < NUM_PROCESSES; ++i){
waitpid(children[i], nullptr, 0);
}
// Merging the sorted segments into a single array
for(int segment_size =1; segment_size < MAXNUM; segment_size *=2){
for(int i =0; i < MAXNUM; i +=2* segment_size){
int start1= 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!