Previous Task Mutual Exclusion - 6 marks Make sure your deadlock is gone before
ID: 3885628 • Letter: P
Question
Previous Task Mutual Exclusion - 6 marks
Make sure your deadlock is gone before submitting this task, since deadlocked programs will not return feedback and many such uploads can cause the server to slow down.
In this task, you are required to implement methods in the given file mutex.cpp down-
loaded from the CS website. You may use the previous tasks code for this. Your need
to implement these using threads, you may use high level C++ threads or pthreads
for this. 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.
3. The le that you write data to should be called "mutex.txt".
4. In this task there is an exception thrown in the threadFunction() you need to nd
a way to overcome this and unlock successfully in order to avoid the deadlock,
without editing the threadFunction().
Question: File Locks - 8 marks
This task does have a Fitchfork component. Make sure your program works and
that it does not hang before uploading.
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 threadFunction() in this task either.
3. Every time the program executes the contents of the le should be overwritten.
4. The le that you write data too should be called " lelock.txt".
5. You need to perform signi cant testing and error handling.
Hint: make use of hidden les (https://www.le.ac.uk/oerresources/bdra/unix/
page_39.htm) to check if the le is locked or not. Also make use of a counter variable
like a semaphore in the hidden le to have the data written to the le in order.
Given file:
#include <iostream>
#include <unistd.h>
using namespace std;
void writeToFile(int threadNumber)
{
}
void lock()
{
}
void unlock()
{
}
void threadFunction(int threadNumber)
{
int x = 0;
int y = 0;
try{
lock();
if (threadNumber % 2 == 0)
sleep(rand() % 4 + 1);
writeToFile(threadNumber);
throw new exception();
unlock();
}
catch(...){
cout << "Something went wrong!" << endl;
throw new exception();
}
}
int main (int argc, char const *argv[]) {
return 0;
}
Explanation / Answer
The following task requires you to write short answers to the questions below, you may be penalized for lengthy answers. • How did deadlock occur in Task 1(a)? • Deadlocks should not occur in practice, how should a programmer safeguard a system in order to prevent a deadlock situation to occur as seen in Task 1(a)? • When writing data to a file which is a better type of lock mechanism to use? • Give one advantage and one disadvantage of using file locks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.