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

C++ Programming using Filelocks You will notice in the previous task that the or

ID: 3883641 • Letter: C

Question

C++ Programming using Filelocks

You will notice in the previous task that the order in which the data written to the file is incorrect, this is due to the OS scheduling, sometimes threads may be scheduled sequentially but not always. It is good practice to never rely that the OS will schedule the threads sequentially thus you need to implement file locks in order to make sure that the order in which the threads are writing to the file is preserved. You are required to implement methods in the given file filelock.cpp downloaded from the CS website. You have to implement a file-locking mechanism for this. Things to note: 1. You can make use of the code you have written in Task1 (b). 2. You may NOT modify the function threadFunction0 in this task either. 3. Every time the program executes the contents of the file should be overwritten. 4. The file that you write data too should be called "filelock.txt" 5. You need to perform significant testing and error handling.

Explanation / Answer

#include <iostream>
#include <unistd.h>
#include <fstream>
#include <pthread.h>
#include <thread>
#include <cstdlib>

using namespace std;

bool isBusy = false;

void writeToFile(int threadNumber)
{
   ofstream outputFile;

   outputFile.open("dead-lock.txt");

   outputFile << "[" << threadNumber;
   for (int a=2; a<=10; a++)
       outputFile << ", " << threadNumber*a;
   outputFile << "] ";
}

void lock()
{
   isBusy = true;
}

void unlock()
{
   isBusy = false;
}
void threadFunction(int threadNumber)
{
int x = 0;
int y = 0;

try{
    lock();
    writeToFile(threadNumber);
    throw new exception();
    unlock();
}
catch(...){
    cout << "Something went wrong!" << endl;
}
}

int main (int argc, char const *argv[])
{
   int numThreads = 0;

   if (argc==2)
       numThreads = atoi(argv[1]);
   else
       exit(EXIT_FAILURE);

   thread **threads = new thread*[numThreads];

   for (int a=0; a<numThreads; a++)
   {
       threads[a] = new thread(threadFunction, a+1);
       cout << "Created thread: " << threads[a]->get_id() << endl;
   }

   for (int a=0; a<numThreads; a++)
   {
       cout << threads[a]->get_id() << endl;
       if (threads[a]->joinable())
           threads[a]->join();
       else
       {
           cout << "Error occured ";
           exit(EXIT_FAILURE);
       }
   }

   for (int a=0; a<numThreads; a++)
       delete threads[a];
   delete [] threads;

   thread thread1(threadFunction, 1), thread2(threadFunction, 2);

   cout << "Created thread: " << thread1.get_id() << endl;
   cout << "Created thread: " << thread2.get_id() << endl;

   thread1.join();
   thread2.join();

   return 0;
}

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