Objective:This assignment will introduce you to interprocess synchronization mechanisms in UNIX using named POSIX semaphores, pthread mutex semaphores, and pthread condition variables.Problem:You must write a C++ program to implement a parallel Fibonacci code generator you created for programming assignment 1 using synchronization mechanisms.Your program will read the input from STDIN.This program reads the information about the alphabet (symbols and frequencies) from STDIN, sorting the symbols in the alphabet in decreasing order based on the frequency. If two or more symbols have the same frequency, you will use the symbol's ASCII value to break the tie (the higher the value, the higher the priority).After assigning a positive integer value (starting from 1) to the symbols in the sorted alphabet, your program must create a child thread per number of symbols in the alphabet. Each child thread will determine the Fibonacci code based on the received integer value from the main thread. After the child threads calculate the Fibonacci code, they will print the information about the symbol, its frequency, and the Fibonacci code, writing the Fibonacci code into a memory location available to the main thread. Finally, the main thread will use the codes generated by the child threads to decompress a file.Each child thread will execute the following tasks:Receive the integer value needed to calculate the Fibonacci code from the main thread.Calculate the Fibonacci code.Print the information about the symbol (symbol, frequency, Fibonacci code). You must use the output message provided in the example below.Write the received information into a memory location accessible by the main thread.Finish its execution.Input Format: The Moodle server will use input redirection using the following input file format:The number of symbols in the alphabet (integer value)n lines (where n is the number of symbols in the alphabet) with the information about the symbols in the alphabet. Each line has one character and one integer representing a symbol and its frequency (separated by a single white space).The name of the compressed file.Example Input File:7C 2O 1S 113 26 10 1compfile1.txtYour program must print the information about the symbols based on their order in the input file. Therefore, you must use synchronization mechanisms to guarantee that child threads print the information about each symbol in the correct order.After receiving the Fibonacci codes from the child threads, the main thread decompresses the contents of a file (sequence of bits represented as a string) and prints the decompressed message.Given the previous input file and the following compressed file:111011001111010110110110001110011The expected output is:Symbol: C, Frequency: 2, Code: 11Symbol: O, Frequency: 1, Code: 1011Symbol: S, Frequency: 1, Code: 0011Symbol: , Frequency: 1, Code: 01011Symbol: 3, Frequency: 2, Code: 011Symbol: 6, Frequency: 1, Code: 00011Symbol: 0, Frequency: 1, Code: 10011Decompressed message = COSC 3360

Respuesta :

#include <iostream>

#include <string>

#include <queue>

#include <cstdlib>

#include <pthread.h>

#include <semaphore.h>

using namespace std;

struct SymbolFrequency

{

   char symbol;

   int frequency;

};

struct FibonacciCode

{

   char symbol;

   int frequency;

   string code;

};

queue<SymbolFrequency> symbolQueue;

queue<FibonacciCode> codeQueue;

sem_t semaphore;

void* generateFibonacciCode(void* arg)

{

   // Wait on semaphore

   sem_wait(&semaphore);

   // Get symbol and its frequency from the queue

   SymbolFrequency sf = symbolQueue.front();

   symbolQueue.pop();

   // Calculate Fibonacci code

   int a = 0;

   int b = 1;

   string code = "";

   for (int i = 0; i < sf.frequency; i++)

   {

       int c = a + b;

       a = b;

       b = c;

       code += '1';

   }

   for (int i = 0; i < sf.frequency - 1; i++)

   {

       code += '0';

   }

   // Create FibonacciCode struct

   FibonacciCode fc;

   fc.symbol = sf.symbol;

   fc.frequency = sf.frequency;

   fc.code = code;

   // Push it to the queue

   codeQueue.push(fc);

   // Print symbol, frequency, and code

   cout << "Symbol: " << fc.symbol << ", Frequency: " << fc.frequency << ", Code: " << fc.code << endl;

   // Post semaphore

   sem_post(&semaphore);

   return nullptr;

}

int main()

{

   // Read number of symbols

   int n;

   cin >> n;

   // Read symbols and their frequencies

   for (int i = 0; i < n; i++)

   {

       SymbolFrequency sf;

       cin >> sf.symbol >> sf.frequency;

       symbolQueue.push(sf);

   }

   // Initialize semaphore

   sem_init(&semaphore, 0, 1);

   // Create n threads

   pthread_t threads[n];

   for (int i = 0; i < n; i++)

   {

       pthread_create(&threads[i], nullptr, generateFibonacciCode, nullptr);

   }

   // Wait for threads to finish

   for (int i = 0; i < n; i++)

   {

       pthread_join(threads[i], nullptr);

   }

   // Read and decompress file

   string fileName;

   cin >> fileName;

   

   ifstream file(fileName);

   string compressedString;

   file >> compressedString;

   file.close();

   string decompressedString = "";

   for (int i = 0; i < compressedString.length(); i++)

   {

       for (int j = 0; j < codeQueue.size(); j++)

       {

           if (codeQueue.front().code[i] == compressedString[i])

           {

               decompressedString += codeQueue.front().symbol;

               break;

           }

           else

           {

               FibonacciCode fc = codeQueue.front();

               codeQueue.pop();

               codeQueue.push(fc);

           }

       }

   }

   cout << "Decompressed message = " << decompressedString << endl;

   return 0;

}

What is symbol frequency?

Symbol frequency is a measure of the number of times a symbol appears in a given sample of data. It is used in coding theory, cryptography, telecommunications, and other areas of mathematics to analyze the structure and complexity of a message or data stream. Symbol frequency can be used to detect patterns and anomalies in data, or to encode and decode messages. In telecommunications, it is used to measure the quality of a signal or data transmission, or to determine the strength of a signal. Symbol frequency analysis is also used to study the language of a document, such as its syntax, grammar, and punctuation.

To learn more about symbol frequency
https://brainly.in/question/2750753
#SPJ4