make sure no ai is being used at all
This assignment will introduce you to interprocess synchronization mechanisms in UNIX using named POSIX semaphores, pthread mutex semaphores, and pthread condition variables.
Problem:
For this assignment, you will modify your solution for programming assignment 1 to comply with the restrictions explained below.
Your multithreaded decoder for a new compression algorithm created by Dr. Rincon must execute the following steps:
-
Read the symbols and the encoded message from STDIN (the Moodle server will implement input redirection to send the information from a file to STDIN).
-
Sort the alphabet symbols in descending order based on their frequency. If two or more symbols have the same frequency, sort them according to lexicographical order (ascending based on ASCII value).
-
Create n POSIX threads (where n is the number of unique symbols in the original message). The main thread must pass the encoded message, the symbol to decode, and the memory location to store the decoding results, ensuring that the first child thread prints the first symbol in the alphabet, the second child thread prints the second symbol in the alphabet, and the nth child thread prints the nth symbol in the alphabet.
-
Print the decoded message.
Each child thread executes the following tasks:
-
Receives the encoded message, the symbol to decode, and the memory location to store the decoding results.
-
Determines the positions of the assigned symbol in the original message.
-
Stores the assigned symbol, using the determined positions, into the memory address shared with the main thread that contains the decoded message.
-
Prints the information about the assigned symbol in descending order based on the symbols’ frequency (if two or more symbols have the same frequency, you will print the symbols based on their lexicographical order).
Input example:
4
A 5
C 2
B 2
D 3
10100010000010000001010001100011100010010010100010110110001100
The expected output is:
Symbol: A, Frequency: 5
Positions: 0 1 3 7 9
Bits to represent the position(s): 23
Symbol: D, Frequency: 3Positions: 5 6 8 Bits to represent the position(s): 17
Symbol: B, Frequency: 2Positions: 4 10 Bits to represent the position(s): 12
Symbol: C, Frequency: 2Positions: 2 11 Bits to represent the position(s): 10
Decoded message: AACABDDADABC
NOTES:
-
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 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.
-
To achieve synchronization, you can only use named POSIX semaphores, pthreads mutex semaphores, or pthreads condition variables. You are not allowed to use pthread_join or sleep to synchronize your threads (you must use pthread_join to guarantee that the parent thread waits for all its child threads to end before ending its execution). Submissions using the previous system calls to synchronize the child threads will be penalized 100%.
-
You cannot use different memory addresses to pass the information from the parent to the child threads.
-
You must use the output statement format based on the example above. The child threads must print the encoded messages in the order in which they were created.
ASSIGNMENT RUBRIC:
- 15 points for each test case with a correct output (max 30 points).
- 25 points for using a single memory location for thread argument passing.
- 5 points for code readability (modularity + comments).
- 10 points for taking full advantage of multithreading.
- 30 points for solving the two synchronization problems correctly (correct usage of critical sections, semaphores, pthreads mutex semaphores, pthreads condition variables, pthread_join).
Penalties:
- 100% for using global variables.
- 100% for not using the pthread library.
- 100% for presenting a solution that does not compile.
Requested files
main.cpp
assignment 1 that it is refereeing to is below including the code I submitted
You need to develop the decoder for a new compression algorithm created by Dr. Rincon to evaluate the effectiveness of Elias-Gamma encoding for larger integer values. The proposed compression algorithm works as follows:
-
Determine the frequency of symbols in the message.
-
Sort the alphabet symbols in descending order based on their frequency. If two or more symbols have the same frequency, sort them according to lexicographical order (ascending based on ASCII value).
-
Create the encoded message by appending the positions of the symbols (based on the sorted alphabet) in the original message. Since Elias-Gamma encoding is used to represent the positions, the values for the positions start from one.
Given the following original message: AACABDDADABC
The alphabet is sorted as A, D, B, and C, and the positions in the encoded message are: 1, 2, 4, 8, 10, 6, 7, 9, 5, 11, 3, and 12 (represented as Elias-Gamma codes).
Your decoder takes the following information as input from the standard input (STDIN):
-
An integer representing the alphabet size (m).
-
m symbols. Each symbol is represented by a single character and an integer value indicating its frequency.
-
A binary string with positions encoded using Elias-Gamma code. The position values are stored according to the order of symbols in the sorted alphabet.
For this assignment, you will create a multithreaded program to find the positions of the symbols in the original message, reconstruct the original message (based on the encoded message), and determine the number of bits required to represent the positions using Elias-Gamma encoding.
Given the following input:
4
A 5
C 2
B 2
D 3
10100010000010000001010001100011100010010010100010110110001100
The expected output is:
Symbol: A, Frequency: 5
Positions: 0 1 3 7 9
Bits to represent the position(s): 23
Symbol: D, Frequency: 3Positions: 5 6 8 Bits to represent the position(s): 17
Symbol: B, Frequency: 2Positions: 4 10 Bits to represent the position(s): 12
Symbol: C, Frequency: 2Positions: 2 11 Bits to represent the position(s): 10
Decoded message: AACABDDADABC
Process:
Your solution must execute the following steps:
-
Read the input lines from STDIN.
-
-
Create m POSIX threads (where m is the alphabet size). Each child thread performs the following tasks:
-
– Receives the encoded message, the symbol to decode, and the memory location to store the decoding results.
– Determines the positions of the assigned symbol in the original message.
– Calculates the number of bits used to represent the symbol’s position using Elias Gamma encoding.
– Stores the assigned symbol’s information in a memory location accessible by the main thread.
– Stores the assigned symbol, using the determined positions, into the memory address shared with the main thread that contains the decoded message.
-
Print the information for each symbol to STDOUT (sorted according to the compression algorithm rules).
-
Print the decoded message.
Notes:
-
The position values for the symbols must start from one (not from zero), because Elias Gamma encoding can only represent positive integers starting from one.
-
You can safely assume that the input will always be in the proper format.
-
You must use the output statement format shown in the example above.
-
You can define additional functions if needed.
-
You must take full advantage of multithreading.
-
You must present code that is readable and has comments explaining the logic of your solution. A 10% penalty will be applied to submissions that do not follow this guideline.
-
You cannot use global variables. A 100% penalty will be applied to submissions using global variables.
-
Not using multiple POSIX threads (the pthread.h library) to implement your solution will result in a 100% penalty. DO NOT USE THE THREAD LIBRARY FROM C++.
-
A penalty of 100% will be applied to solutions that do not compile.
-
A penalty of 100% will be applied to solutions that hardcode the output.
Assignment rubric:
-
Correct output: 10 points per test case (20 points total).
-
Correct implementation of the thread function: 30 points.
-
Taking full advantage of multithreading (no pthread_join or sleep in the same loop as pthread_create): 30 points.
-
Creating the correct number of threads: 10 points.
-
Having clean and commented code: 10 points.
Penalties:
-
Presenting a solution that does not compile: -100 points.
-
Not using POSIX threads: -100 points.
-
Hardcoding the output: -100 points.
-
Using global variables: -100 points.
-
Using AI tools without following the rules stated in the syllabus: -100 points.
and the code below I got a 100 on #include <iostream>#include <vector>#include <string>#include <algorithm>#include <pthread.h>#include <sstream>using namespace std;//Structure used to store results for each symbolstruct SymbolResult { char symbol; int frequency; vector<int> positions; // posiitons start at 0 int bits_used;};// Data passed to each threadstruct ThreadData { char symbol; int frequency; int start_index; const vector<int>* decoded_positions; // decoded positions are counted started from 1 SymbolResult* result; string* decoded_message;};// Elias-Gamma decoder for one integerint decode_gamma(const string& bits, int& index, int& bits_consumed) { int zero_count = 0; //count the number of leading zeros while (index < (int)bits.size() && bits[index] == ‘0’) { zero_count++; index++; } //read the next zeros+1 bits int length = zero_count + 1; int value = 0; for (int i = 0; i < length; i++) { value = (value << 1) + (bits[index] – ‘0’); index++; } bits_consumed = zero_count + length; return value;}// Thread functionvoid* thread_function(void* arg) { ThreadData* data = static_cast<ThreadData*>(arg); data->result->symbol = data->symbol; data->result->frequency = data->frequency; data->result->bits_used = 0; for (int i = 0; i < data->frequency; i++) { int pos_1_based = (*(data->decoded_positions))[data->start_index + i]; int pos_0_based = pos_1_based – 1; data->result->positions.push_back(pos_0_based); // Write into shared decoded message (*(data->decoded_message))[pos_0_based] = data->symbol; // Calculate Elias-Gamma bit cost int temp = pos_1_based; int binary_length = 0; while (temp > 0) { binary_length++; temp >>= 1; } data->result->bits_used += (binary_length – 1) + binary_length; } pthread_exit(nullptr);}// Sorting rule: Descending frequency, ascending ASCIIbool compare_symbols(const SymbolResult& a, const SymbolResult& b) { if (a.frequency != b.frequency) return a.frequency > b.frequency; return a.symbol < b.symbol;}int main() { int m; string line; // Read m from its own line getline(cin, line); m = stoi(line); vector<SymbolResult> symbols(m); for (int i = 0; i < m; i++) { while (getline(cin, line)) { if (line.size() >= 3) break; } symbols[i].symbol = line[0]; // Frequency is the last whitespace-delimited token on the line // Find it by scanning from the end size_t end = line.find_last_not_of(” trn”); size_t start = line.find_last_of(” t”, end); symbols[i].frequency = stoi(line.substr(start + 1, end – start)); } sort(symbols.begin(), symbols.end(), compare_symbols); // Read the bit string string bit_string; cin >> bit_string; int total_positions = 0; for (const auto& s : symbols) total_positions += s.frequency; vector<int> decoded_positions(total_positions); int bit_index = 0; for (int i = 0; i < total_positions; i++) { int bits_used; decoded_positions[i] = decode_gamma(bit_string, bit_index, bits_used); } string decoded_message(total_positions, ‘ ‘); vector<pthread_t> threads(m); vector<ThreadData> thread_data(m); int start_index = 0; // Create all threads for (int i = 0; i < m; i++) { thread_data[i].symbol = symbols[i].symbol; thread_data[i].frequency = symbols[i].frequency; thread_data[i].start_index = start_index; thread_data[i].decoded_positions = &decoded_positions; thread_data[i].result = &symbols[i]; thread_data[i].decoded_message = &decoded_message; pthread_create(&threads[i], nullptr, thread_function, &thread_data[i]); start_index += symbols[i].frequency; } for (int i = 0; i < m; i++) pthread_join(threads[i], nullptr); // Output for (const auto& s : symbols) { cout << “Symbol: ” << s.symbol << “, Frequency: ” << s.frequency << endl; cout << “Positions: “; for (int pos : s.positions) cout << pos << ” “; cout << endl; cout << “Bits to represent the position(s): ” << s.bits_used << endl << endl; } cout << “Decoded message: ” << decoded_message << endl; return 0;}
Leave a Reply
You must be logged in to post a comment.