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

Task 1(a) Deadlock - 6 marks This task does not have a Fitchfork component. You

ID: 3885626 • Letter: T

Question

Task 1(a) Deadlock - 6 marks This task does not have a Fitchfork component. You only have to submit this task as part of your nal submission. In this task, you are required to implement methods in the given le deadlock.cpp downloaded from the CS website. Your need to implement these using threads, you may use high level C++ threads or pthreads for this purpose. Methods to implement are dened below: 1. writeToFile(int threadNumber) - this simply writes data to a le called "dead- lock.txt", the data that must be written consists of the following: - The rst 10 iterations of the product of the thread number and i separated by a single space. - Where i is a loop variable from 1 to 10. - Example: if thread number is 2 the data that should be written to the le is: [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]. 2. lock() - implement any type of locking mechanism of your choice. You may use a library for this. 3. unlock() - implement a unlocking mechanism with relation to the way you imple- mented the lock() function. 4. main(...) - You should have one command line argument that accepts the number of threads to create. - For each thread created you need to display a message as well as the thread ID. - Your threads should call the threadFunction() function. Things to note: 1. You may NOT modify the function threadFunction() in this task. 2. Every time the program executes the contents of the le should be overwritten. Your output should look something like this: 4

Explanation / Answer

#include<iostream> // this provides basic input output services
#include<fsstream> //This data type represents the input file stream and is used to read information from files.
using namespace std;
int writeToFile(int threadNo)
{
   ofstream myFile; //stream classes to write on files;
   int threadNo;
   myFile.open("deadLock.txt"); // output file
   if(threadNo==1)
   {
       for(int i=1;i<=20;i=i+2)
       {
           myFile <<value of thread 1 : <<i << endl; // printing condition for thread 1
       }
      
   }
   else if(threadNo==2)
   {
       for(int i=2;i<=20;i=i+2)
       {
           myFile << value of thread 2: <<i <<end2; // printing condition for thread 2
       }
      
   }
   myFile.close(); // closing
   return myFile;
}
/* creates deadlock for thread1*/
void Thread1()
{
   mutex2.lock();
std::cout << "Thread 1 is running" << std::endl;
mutex1.lock();
mutex2.unlock();
mutex1.unlock();
}
/* creates deadlock for thread2*/
void Thread2()
{
   mutex1.lock();
std::cout << "Thread2 is running" << std::endl;
mutex2.lock();
mutex1.unlock();
mutex2.unlock();
}
/* solving deadLock problem*/
void ExecuteThreads()
{
boost::thread t1( Thread1 );
boost::thread t2( Thread2 );

t1.join();
t2.join();

std::cout << "Finished" << std::endl;
}
/* Calling main function*/
int main()
{
   /* Now run your own code to execute writeToFile method,and print the values
   ** You can use switch case
   ** or if else block to take input from the user and
   ** print the thread value accordingly
   ** create a file "deadLock.txt" in any Drive.so that you can see the result*/
}

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