Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include
ID: 3741932 • Letter: W
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 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 << "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;
}
--------------------------------------------------------------------------------------------------------------------------
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 << 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[])
{
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 << "Please enter a file name and the number of threads to use!" << endl;
}
return 0;
}
---------------------------------------------------------------------------------------------------------------------------
batman.ascii
*******************
***************************
*********************************
******* * * * * *******
******* *** ** ** *** *******
****** ***** ********* ***** *****
****** ******** ********* ****** *****
**** ********** ********* ********* *****
**** ************** *********** ************ ****
**** ************************************************* ****
**** *************************************************** ****
**** **************************************************** ****
**** **************************************************** ****
**** *************************************************** ****
**** ******* **** *********** **** ********* ****
**** ***** * ******* * ******** ****
***** **** ***** ****** *****
***** ** *** ** ******
****** * * * *******
******* *******
******** *******
*********************************
***************************
*******************
Explanation / Answer
mutex server;
mutex printer;
condition_variable cv;
mutex thread_no;
mutex screen;
Static int thread_run = 0;
void printToScreen(string toPrint)
{
screen.lock();
cout<< toPrint << endl;
screen.unlock();
}
void print(string out)
{
ofStream myfile;
printToScreen("Opening...");
myfile.open("deadlock.txt",ios_base::app);
printToScreen("Writing...");
myfile << out;
myfile.close();
}
void lock(int choice)
{
if(choice==0){
server.lock();
}
elseif(choice==1){
printer.lock();
}
}
void unlock(int choice)
{
if(choice==0){
server.unlock();
}
elseif(choice==1){
printer.unlock();
}
}
void spin(int index)
{
unique_lock<mutex> lck(thread_no);
while(index!=thread_run){
cv.wait(lck);
}
lock(1);
thread_run++;
cv.notifyAll();
}
int main(int argc, char const *argv[])
{
thread mythreads[argv[2]];
if (argv[1] != "" && argv[2] != 0)
{
server = new Server(argv[1], argv[2]);
for(i=0;i<argv[2];i++){
if(i%2==0){
mythreads[i]=thread(evenThread,i);
}
else {
mythreads[i]=thread(oddThread,i);
}
}
for(i=0;i<argv[2];i++){
mythreads[i].join();
}
delete server;
}
else
{
cout << "Please enter a file name and the number of threads to use!" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.