Posted: November 30th, 2022
Place your order now for a similar assignment and have exceptional work written by our team of experts, At affordable rates
For This or a Similar Paper Click To Order Now
I have the right code for this assignment, I just need to change it.
code is below
Given a message (string or char array) as input, you need to implement a multithreaded version of the Shannon-Fano-Elias code generator based on the following steps:
Read the input from STDIN (the Moodle server will implement input redirection to send the information from a file to STDIN). The input has a single line with the message (string or char array).
Example Input File: AAAAAABBBBBCCCCDDDEE
Create n POSIX threads (where n is the number of symbols to encode). Each child thread executes the following tasks:Receives the probabilities of the symbols from the main thread.
Implements the Shannon-Fano-Elias encoding algorithm to determine the code for the assigned symbol.
Print the Shannon-Fano-Elias code for the assigned symbol.
Given the previous input, the expected output is:
SHANNON-FANO-ELIAS Codes:
Symbol A, Code: 001
Symbol B, Code: 011
Symbol C, Code: 1010
Symbol D, Code: 1101
Symbol E, Code: 11110NOTES:
You can safely assume that the input files will always be in the proper format.
You cannot use global variables. A 100% penalty will be applied to submissions using global variables.
You must define the critical sections following the guidelines that we discussed in class.
You must use POSIX threads. A penalty of 100% will be applied to submissions using a thread library other than the pthread library.
You can only use named POSIX semaphores, pthreads mutex semaphores, or pthreads condition variables to achieve synchronization. Using pthread_join or sleep to synchronize your threads is not allowed (you must use pthread_join to guarantee that the parent thread waits for all its child threads to end before ending its execution). A penalty of 100% will be applied to submissions using the previous system calls to synchronize the child threads.
You cannot use different memory addresses to pass the information from the parent thread to the child threads.
You must use the output statement format based on the example above.
this is the right i just need to change it
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// Struct for Child thread
struct childThreadArg {
char CTsymbol;
double CTfrequency;
double CTcumulativeFrequency;
int CTsize;
int CTchildIndex; // Stores the child index
sem_t* CTsemaphore; // Stores the address of parent semaphore from main()
pthread_mutex_t* CTmutex; // Stores the address of parent mutex from main()
int* CTturnCounter; // Stores the address of parent counter turn from main()
pthread_cond_t* CTlockCondition; // Stores the address of parent lock condition variable from main()
};
// Function declaration
double calc(double, double); // using location in probabilty vector to calculate fbar
int length(double ); // returns length of desired code using log and ceil
string decToBin(double, int); //converts decimal to binary using l most significant digits
// Thread declaration
void *childThread(void *);
// main()
int main(int argc, char *argv[]) {
// File opening
string str, temp;
getline(cin,str);
sort(str.begin(), str.end());
for (int i = 0; i < str.size(); i++) {
if(temp.find(str[i]) == string::npos)
temp += str[i];
}
// Child thread declaration
pthread_t *child = new pthread_t[temp.size()];
childThreadArg *childArg = new childThreadArg[temp.size()]; // to pass arguments to child thread
// P.2 – Processing Stage-1: 2.2 – Semaphore declaration and initialization for Child thread
sem_t mainSemaphore; // declaring a Semaphore in main()
// P.2 – Processing Stage-1: 2.3 – Mutex declaration and initialization for Child thread
static pthread_mutex_t mainMutex; // declaring a Mutex in main()
// P.2 – Processing Stage-1: 2.4 – Turn Counter declaration and initialization for Child thread
static int mainTurnCounter = 0; // declaring and initializing a Turn Counter in main()
// P.2 – Processing Stage-1: 2.5 – Lock Condition declaration and initialization for Child thread
static pthread_cond_t mainLockCondition = PTHREAD_COND_INITIALIZER; // declaring and initializing a Lock Condition in main()
// Calculations
int counter = 0;
double frequency = 0, cumulativeFrequency = 0;
for(int i = 0; i < str.size(); i++) {
frequency++;
if(str[i]!=str[i+1]){
childArg[counter].CTfrequency= ((frequency)/str.size());
childArg[counter].CTsymbol= str[i];
childArg[counter].CTcumulativeFrequency = (cumulativeFrequency/str.size());
cumulativeFrequency+=frequency;
frequency=0;
counter++;
}
}
// Printing Output
cout << "SHANNON-FANO-ELIAS Codes:" << endl;
// Creating and Initializing Child threads
for (int i = 0; i < temp.size(); i++) {
childArg[i].CTchildIndex = i;
childArg[i].CTsize = temp.size();
childArg[i].CTsemaphore = &mainSemaphore; // passing address of the Semaphore in main() to child as argument
sem_init(childArg[i].CTsemaphore, 0, 0); // initializing the Semaphore in main() using child argument
childArg[i].CTmutex = &mainMutex; // passing address of the Mutex in main() to child as argument
pthread_mutex_init(childArg[i].CTmutex, NULL); // initializing the Mutex in main() using child argument
childArg[i].CTturnCounter = &mainTurnCounter; // passing address of the Turn Counter in main() to child as argument
childArg[i].CTlockCondition = &mainLockCondition; // passing address of the Lock Condition in main() to child as argument
if (pthread_create(&child[i], nullptr, childThread, &childArg[i])) {
cout << "Error creating thread" << endl;
return 1;
}
sem_wait(childArg[i].CTsemaphore);
}
for (int i = 0; i < temp.size(); i++) {
// P.4 – Processing Stage-3 – Semaphore destruction for Child thread
sem_destroy(childArg[i].CTsemaphore);
}
// P.5 – Processing Stage-4 – Joining Child Threads
for (int i = 0; i < temp.size(); i++) {
if (pthread_join(child[i], nullptr)) {
fprintf(stderr, "Error joining threadn");
return 2;
}
}
// P.6 – Clear Memory
delete[] child;
return 0;
}
// Child Thread defination
void* childThread(void *ptr) {
struct childThreadArg CTptr = *((struct childThreadArg *)ptr);
// * Critical Section A – 1st Critical Section of Child Thread * ENDS
sem_post(CTptr.CTsemaphore); // Critical Section A – Exit call for the 1st Critical Section in Child Thread to pass arguments
char symbol = CTptr.CTsymbol;
double frequency = CTptr.CTfrequency;
double cumulativeFrequency = CTptr.CTcumulativeFrequency;
int index = CTptr.CTchildIndex;
int size = CTptr.CTsize;
// C.6 – Processing Stage-6 – Using Condition Variables to control the output order
pthread_mutex_lock(CTptr.CTmutex); // locking the mutex object referenced by Child Thread mutex
while(CTptr.CTchildIndex != *CTptr.CTturnCounter) { // Critical Section B – Enter call for the 2nd Critical Section of Child Thread to display output
pthread_cond_wait(CTptr.CTlockCondition, CTptr.CTmutex); // blocking the Child Thread lock condition variable with Child Thread mutex
}
// * Critical Section B – 2nd Critical Section of Child Thread * BEGINS
double preCode = calc(frequency, cumulativeFrequency);
int l = length(frequency);
string code = decToBin(preCode,l);
string messageT = symbol+code;
int stringSize = messageT.size();
char message[stringSize+1];
strcpy(message,messageT.c_str());
char *buffer = new char[stringSize + 1];
string result(buffer);
result = message;
cout << endl << "Symbol "<< symbol << ", Code: " << result.substr(1);
// * Critical Section B – 2nd Critical Section of Child Thread * ENDS
(*CTptr.CTturnCounter)++; // Critical Section B – Exit call for the 2nd Critical Section of Child Thread to display output
pthread_cond_broadcast(CTptr.CTlockCondition); // unblocking all threads currently blocked on the Child Thread lock condition variable
pthread_mutex_unlock(CTptr.CTmutex);
return nullptr;
}
// Function defination
double calc(double f, double cf) { // using location in probabilty vector to calculate fbar
double temp;
temp = (f*.5)+cf;
return temp;
}
int length(double f) { // returns length of desired code using log and ceil
f = ceil(log2((1/f))+1);
return f;
}
string decToBin(double x, int l) { //converts decimal to binary using l most significant digits
int temp;
string result;
for(int i =0; i=1) {
x-=1;
x*=2;
}
else
x *=2;
temp = int(x);
result+=to_string(temp);
}
return result;
}
Place an order in 3 easy steps. Takes less than 5 mins.