Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The aim of this task is to remove the deadlock from the code implemented in : (h

ID: 3744289 • Letter: T

Question

The aim of this task is to remove the deadlock from the code implemented

in : (https://www.chegg.com/homework-help/questions-and-answers/write-cpp-program-serverh-ifndef-serverh-define-serverh-include-include-include-include-us-q30759618)

You will implement this in the file mutex.cpp which is given below.

You may reuse your code in the implementation of the functions from the above task. Please note that the main function for this task MUST accept input from the user.

The input should be the thread count (number of threads to use) and then the filename (an example is given above the screenshot below). Also remove ALL sleep functions from your code. Make sure your deadlock is gone before submitting.

Things to note before completing this task:

/*Mutex.cpp*/

#include <iostream>

#include <unistd.h>

#include "../Server.h"

using namespace std;

Server *server;

void printToScreen(string toScreen)

{

   /* Implement this function so that printing from each thread to stdout (i.e. using cout) doesn't clash with each other. */

}

void spin(int index)

{

   /* Wait until it is "index's" turn to write to the file. */

}

void print(string out)

{

   /* print to file called mutex.txt */

}

void lock(int choice)

{

   /* Based on the choice, lock either the server or printer */

}

void unlock(int choice)

{

   /* Based on the choice, unlock either the server or printer */  

}

void evenThread(int index)

{

   try

   {

       spin(index);

       lock(0); // server

       printToScreen("Thread " + to_string(index) + ": Lock acquired for Server ");

       string piece = server->getPiece(index);

       print(piece);

      

       unlock(0);

       printToScreen("Thread " + to_string(index) + ": Lock released for Server ");

      

       unlock(1); // printer

       printToScreen("Thread " + to_string(index) + ": Lock released for Printer ");

   }

   catch (const char *msg)

   {

       cerr << msg << endl;

       throw new exception();

   }

}

void oddThread(int index)

{

   try

   {

       lock(0); // server

       printToScreen("Thread " + to_string(index) + ": Lock acquired for Server ");

       string piece = server->getPiece(index);

       spin(index);

       print(piece);

      

       unlock(0);

       printToScreen("Thread " + to_string(index) + ": Lock released for Server ");

      

       unlock(1); // printer

       printToScreen("Thread " + to_string(index) + ": Lock released for Printer ");

   }

   catch (const char *msg)

   {

       cout << msg << endl;

       throw new exception();

   }

}

int main(int argc, char const *argv[])

{

   /* cin here */

   if (/*input 2*/ != 0 && /*input 1*/ != 0)

   {

       server = new Server(/*input 2*/, /*input 1*/);

      

       /* Fill in the main function code here */

      

       delete server;

   }

   else

   {

       cout << "Please enter a file name and the number of threads to use!" << endl;

   }

   return 0;

}

// End of mutex.cpp

2. The lines stated above throw a new exception after catching the 500 Internal Server Error. You are required to handle this newly thrown exception and unlock successfully to avoid the newly resulting deadlock. Don't forget that after handling this exception the thread still needs to send its piece of the ASCII art to the printer so that the art can be written to the file. Please note that this new exception may be thrown multiple times during the same execution but will not happen every time you run your file (due to the probability of throwing the 500 Internal Server roT 3. The file that you will need to write data to should be called "mutex.txt". Your output should look similar to the following: The only difference between the screenshot below and the altered output is the removal of the command-line parameters and instead the insertion of the following line: Enter thread count and filename: 3 batman.ascii cal 3$ -/mut batman.ascii 3 esk Thread 0: Lock acquired for Server Getting piece Thread 0: Lock released for Server Thread 0: Lock acquired for Printer Opening.-- riting. .- Thread0: Lock released for Printer hread 1: Lock acquired for Server Getting piece number 1 Thread 1 Lock released for Server hread 1: Lock acquired for Printer Opening" . Writing... Thread 1: Lock released for Printer Thread 2: Lock acquired for Server 500 Internal Server Error cat mutex.txt Caught and Continuing... Thread 2:Lock released for Server Thread 2: Lock acquired for Server 500 Internal Server Error Caught and Continuing... hread 2: Lock released for Server Thread 2: Lock acquired for Server Getting piece number 2 hread 2: Lock released for Server Thread 2: Lock acquired for Printer Opening... Writing Thread 2:Lock released for Printer esktop/Practical 3 (b) Mutex File Output (printer) (a) mutex.cpp Screen Output

Explanation / Answer

#ifndef SERVER_H
#define SERVER_H

#include <iostream>
#include <iostream>
#include <thread>
#include <mutex>
#include <unistd.h>

using namespace std;

class Server
{
public:
Server();
Server(string, int);
~Server();
string getPiece(int);
private:
string *ascii;
mutex access;
};

//server.ccp

#include "Server.h"
#include
#include

Server::Server(){}

Server::Server(string filename, int threads)
{
vector loaded;
ascii = new string[threads];
ifstream in;
string line;
in.open(filename);
if (!in.is_open())
{
cout << "Could not open file " << filename << endl;
exit(1);
}
while(!in.eof())
{
getline(in, line);
loaded.push_back(line);
}
in.close();

int step = loaded.size()/threads;
string piece = "";

for (int i = 0; i < threads; ++i)
{
for (int j = step*i; j < ((step*i) + step); ++j)
{
if (j + 1 < loaded.size())
piece += loaded.at(j) + " ";
else
piece += loaded.at(j);
}
ascii[i] = piece;
piece = "";
}
}

Server::~Server()
{
delete []ascii;
}

string Server::getPiece(int piece)
{
srand(time(NULL));
if (rand()/static_cast(RAND_MAX) > 0.6)
throw "500 Internal Server Error";
cout << "Getting piece number " << piece << endl;
string toReturn = ascii[piece];
return toReturn;
}

// mutex.cpp

#include <iostream>

#include <unistd.h>

#include "Server.h"

#include <iostream>

#

include <fstream>

#include <string>

#include <cstdlib> // system()

#include <conio.h>

using namespace std;

Server *server;

void printToScreen(string toPrint)

{

/* Implement this function so that printing from each thread to stdout (i.e. using cout) doesn't clash with each other. */

}

void print(string out)

{

/* Output to file called deadlock.txt */

ofstream printer ("LPT1");

if(!printer)

{ return 1;

}

printer.puts(out);

printer.putc(' ');

printer.close();

}

void lock(int choice)

{

}

void unlock(int choice)

{

/* Based on the choice, unlock either the server or printer */

  

}

void spin(int index)

{

/* Wait until it is "index's" turn to write to the file. */

}

void evenThread(int index)

{

try

{

spin(index);

lock(0); // server

printToScreen("Thread " + to_string(index) + ": Lock acquired for Server ");

string piece = server->getPiece(index);

print(piece);

  

unlock(0);

printToScreen("Thread " + to_string(index) + ": Lock released for Server ");

  

unlock(1); // printer

printToScreen("Thread " + to_string(index) + ": Lock released for Printer ");

}

catch (const char *msg)

{

cerr << msg << endl;

}

}

void oddThread(int index)

{

try

{

lock(0); // server

printToScreen("Thread " + to_string(index) + ": Lock acquired for Server ");

string piece = server->getPiece(index);

spin(index);

print(piece);

  

unlock(0);

printToScreen("Thread " + to_string(index) + ": Lock released for Server ");

  

unlock(1); // printer

printToScreen("Thread " + to_string(index) + ": Lock released for Printer ");

}

catch (const char *msg)

{

cout << msg << endl;

}

}

int main(int argc, char const *argv[])

{

string fileString;

ifstream myfile ("mutex.txt");

if (myfile.is_open())

{

while ( getline (myfile,fileString) )

{

if (fileString != "" && argc!= 0)

{

server = new Server(fileString, argc);

  

/* Fill in the main function code here */

  

delete server;

}

else

{

cout << "Please enter a file name and the number of threads to use!" << endl;

}

}

myfile.close();

}

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote