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

Previous Task: In this task, you are required to implement methods in the given

ID: 3885627 • Letter: P

Question

Previous Task:

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 de ned 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.

Given Code:

#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();
    writeToFile(threadNumber);
    throw new exception();
    unlock();
}
catch(...){
    cout << "Something went wrong!" << endl;
}
}


int main (int argc, char const *argv[]) {

    return 0;
}

Question: 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().

Explanation / Answer

Our main() program is a single, default thread. All other threads must be explicitly created by the programmer. pthread_create creates a new thread and makes it executable. This routine can be called any number of times from anywhere within our code. pthread_create (pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) arguments: thread: An identifier for the new thread returned by the subroutine. This is a pointer to pthread_t structure. When a thread is created, an identifier is written to the memory location to which this variable points. This identifier enables us to refer to the thread. attr: An attribute object that may be used to set thread attributes. We can specify a thread attributes object, or NULL for the default values. start_routine: The routine that the thread will execute once it is created. void *(*start_routine)(void *) We should pass the address of a function taking a pointer to void as a parameter and the function will return a pointer to void. So, we can pass any type of single argument and return a pointer to any type. While using fork() causes execution to continue in the same location with a different return code, using a new thread explicitly provides a pointer to a function where the new thread should start executing. arg: A single argument that may be passed to start_routine. It must be passed as a void pointer. NULL may be used if no argument is to be passed. The maximum number of threads that may be created by a process is implementation dependent. Once created, threads are peers, and may create other threads. There is no implied hierarchy or dependency between threads. Here is a sample of creating a child thread: // thread0.c #include #include #include void *worker_thread(void *arg) { printf("This is worker_thread() "); pthread_exit(NULL); } int main() { pthread_t my_thread; int ret; printf("In main: creating thread "); ret = pthread_create(&my;_thread, NULL, &worker;_thread, NULL); if(ret != 0) { printf("Error: pthread_create() failed "); exit(EXIT_FAILURE); } pthread_exit(NULL);
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