Question
given code: 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:
given code:
server.h
#ifndef SERVER_H #define SERVER_H
#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
Server::Server(){}
Server::Server(string filename, int threads) { vector 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 //file to implement deadlock.cpp #include 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started