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

In c++ The aim of this task is to remove the deadlock. Given files: //server.h #

ID: 3744333 • Letter: I

Question

In c++

The aim of this task is to remove the deadlock.

Given files:

//server.h

#ifndef SERVER_H

#define SERVER_H

#include <iostream>

#include <vector>

#include <fstream>

#include <mutex>

using namespace std;

class Server

{

public:

Server();

Server(string, int);

~Server();

string getPiece(int);

private:

string *ascii;

mutex access;

};

#endif

//server.cpp

#include "Server.h"

#include <unistd.h>

#include <time.h>

Server::Server(){}

Server::Server(string filename, int threads)

{

vector<string> 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<float>(RAND_MAX) > 0.80)

throw "500 Internal Server Error";

cout << "Getting piece number " << piece << endl;

string toReturn = ascii[piece];

return toReturn;

}

file to implement:

//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;

}

//batman.ascii

*******************
***************************
*********************************
******* * * * * *******
******* *** ** ** *** *******
****** ***** ********* ***** *****
****** ******** ********* ****** *****
**** ********** ********* ********* *****
**** ************** *********** ************ ****
**** ************************************************* ****
**** *************************************************** ****
**** **************************************************** ****
**** **************************************************** ****
**** *************************************************** ****
**** ******* **** *********** **** ********* ****
**** ***** * ******* * ******** ****
***** **** ***** ****** *****
***** ** *** ** ******
****** * * * *******
******* *******
******** *******
*********************************
***************************
*******************

Things to note before completing this task: 1. Plcase note that there is an added line to the even Thread and oddThread functions (in the catch). Do not remove any lines from these functions or you will be penalised. Hint: you may add lines to or move lines around within these functions

Explanation / Answer

//server.h

#ifndef SERVER_H

#define SERVER_H

#include <iostream>

#include <vector>

#include <fstream>

#include <mutex>

using namespace std;

class Server

{

public:

Server();

Server(string, int);

~Server();

string getPiece(int);

private:

string *ascii;

mutex access;

};

#endif

//server.cpp

#include "Server.h"

#include <unistd.h>

#include <time.h>

Server::Server(){}

Server::Server(string filename, int threads)

{

vector<string> 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<float>(RAND_MAX) > 0.80)

throw "500 Internal Server Error";

cout << "Getting piece number " << piece << endl;

string toReturn = ascii[piece];

return toReturn;

}

file to implement:

//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;

}

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