Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

CProducerConsumerOptimizationusingParallelProgramming

"Using Producer/Consumer Techniques in the attached "Patterns of Parallel Programming", optimize a program that reads the attached file (message.dat) in encrypted format and displays the decrypted message. Using trial and error find the optimal block size for the 8 threads found in the computers in BE1034 (Intel I7 processors). That is, find the block size that produces the fastest decryption of the attached message.dat file."

So I am working on a C# program that needs to read a .dat file in encrypted format and display the decrypted message and find an "optimal" block size that it returns.

I'm supposed to use parallel programming to achieve this and was given: https://download.microsoft.com/download/3/4/D/34D13993-2132-4E04-AE48-53D3150057BD/Patterns_of_Parallel_Programming_CSharp.pdf to read through and reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.IO;
using DecryptionClass;


namespace Assign20
{
    class Program
    {
        static Int32[] bytes = new Int32[34214];

        public static void Main(string[] args)
        {
            FileStream fileStream;
            BinaryReader binaryReader;
            Stopwatch watch;

            try
            {
                fileStream = new FileStream("C:/Users/secret/source/repos/ConsoleApp31/ConsoleApp31/bin/Debug/message.dat", FileMode.Open, FileAccess.Read);
                binaryReader = new BinaryReader(fileStream);

                for (int i = 0; i < 34214; i++)
                {
                    bytes[i] = binaryReader.ReadInt32();
                }

                watch = Stopwatch.StartNew();
                String[] s = new String[1];

                message m = new message();
                s[0] = m.convert(1, 34214);

                System.Console.WriteLine(s[0]);
                long elapsed = watch.ElapsedMilliseconds;

                System.Console.WriteLine("\n\nElapsed Time: " + elapsed + " milliseconds\nNoBlocks: 1");

                binaryReader.Close();
                fileStream.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Error: " + e);
                System.Console.WriteLine("Could not open file message.dat!");
            }
        }


        public class message
        {
            public String convert(int j, int blocksize)
            {
                int start = 0;
                int end = 34214;
                String s = "";
                for (int i = start; i < end; i++)
                {
                    key key = new key();
                    char c = key.decrypt(bytes[i], 34214);  //parameters are byte_to_decrypt, block_size
                    s = s + c;
                }
                return s;
            }
        }


    }
}

How can I implement parallel programming to accomplish this goal?

Comments