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
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:
1. Please note that there is an added line to the evenThread and oddThread functions (in the catch). Hint: you may add lines to or move lines around within these functions.
/*Mutex.cpp*/
#include
#include
#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
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
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
}
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 OutputStep 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