Question: Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public: Server(); Server(string, int); ~Server(); string

Write a cpp program

Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include

#include 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

#ifndef SERVER_H #define SERVER_H

#include #include #include #include

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 #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

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

for (int i = 0; i

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

--------------------------------------------------------------------------------------------------------------------------

deadlock.cpp

#include #include #include "../Server.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 */ }

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

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

int main(int argc, char const *argv[]) { if (/*filename argument*/ != "" && /*thread count argument*/ != 0) { server = new Server(/*filename argument*/, /*thread count argument*/); /* Fill in the main function code here */ delete server; } else { cout

return 0; }

---------------------------------------------------------------------------------------------------------------------------

batman.ascii

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

The Server Class The Server class is simply there to emulate a server in the elient-server model/arehitecture and includes the following functions: Server(string filename, int threads) - This is the constretor and l read in a file specified by filename and split the content of the file into threads number of pieces and store it in an array called ascii. Keep nd that the indexes start from 0 and not . "Serve The destructor which deletes the dynamically allocated ascii array. getPiece(int piece) - A method which returns the piece (a string) frt array specified by piece. asii In this task you arrequired to implement a number of functions in the file deadlock.cpp The implementation of these functions should provide you with two deadlock situa tions. You will need to use threads when implementing these functions. You may use high level C threads OR pthreads for this purpose. You will also need to use some locking mechanism to achieve mutual exclusion between threads. The methods you need to implement are as follows: 1. printToScreen(string toScreen) - Implement this function to deal with the race condition on stdout (i.e. printing to the screen). If multiple threads try and output to the screen concurently, much of the text being output will collide with each other and create a line, or multiple lines, of difficult to interpret text. This is what is mant by the race condition on stdout 2. output(string toPrint) - This funetion writes the string toPrint to a file called "deadlock.txt. Bofore opening the file you will need to output, to the screen, the string "Opening... and before writing to the file output, to the screen, the string "Writing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!