
- Modify the pthread-data-sharing-mutex-os-call.cpp program to apply a Pthread mutex solution, i.e., you will use Linux system calls to control access to the critical region.
Note: Mutex initialization can be done in two ways:
- Using a mutex initialization function (https://linux.die.net/man/3/pthread_mutex_init) which is more powerful if you need to set up your mutex in special ways.
- Using the mutex initialization macro introduced in the lecture material, which initializes a mutex with default settings [sufficient for the purposes of this assignment].
The necessary changes will be very small, i.e., not a lot of code is needed.
- Build and execute the updated program several times.
Expected Output:
Your program should produce output similar to the following (Note: the order of threads may be different in your case and all iterations for a given thread need not be together):
Take a screenshot of a sample output and upload the picture as part of your assignment submission.
-----------------------
Code:
#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#define TOTAL_THREADS 2
int count;
int turn; // Shared variable, indicates
// whose turn it is to execute
bool interested[TOTAL_THREADS]; // Shared variable, indicates
// processes interested in executing
// The thread_id will be either 0 or 1
void enter_region(int thread_id)
{
int other; // ID of the other thread
other = 1 - thread_id; // The oposite of thread_id
// TODO: Add the code to indicate the
// thread's interest in executing.
// TODO: Indicate the thread's turn to execute next
// TODO: Busy wait until it is the thread's turn to execute
}
void leave_region(int thread_id)
{
// TODO: Add the code to set the flag
// indicating that the thread has
// exited the critical region.
}
void* myFunction(void* arg)
{
int thread_id = *((int*) arg);
for(unsigned int i = 0; i < 10; ++i) {
// TODO:
// Make sure that the thread waits for its turn
// before it enters the critical region.
//
// HINT: You need one function call
// Beginning of the critical region
count++;
std::cout << "Thread #" << thread_id << " count = " << count << std::endl;
// End of the critical region
// TODO:
// Make sure that the other thread gets a turn
//
// HINT: You need one function call
// Random wait - This code is just to ensure that the threads
// show data sharing problems
int max = rand() % 1000000;
for (int x = 0; x < max; x++);
// End of random wait code
}
pthread_exit(NULL);
}
// HINT: It is not necessary to make any changes in main()
int main()
{
int rc[TOTAL_THREADS];
pthread_t ids[TOTAL_THREADS];
int args[TOTAL_THREADS];
count = 0;
for(unsigned int i = 0; i < TOTAL_THREADS; ++i) {
args[i] = i;
rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);
}
for(unsigned int i = 0; i < TOTAL_THREADS; ++i) {
pthread_join(ids[i], NULL);
}
std::cout << "Final count = " << count << std::endl;
pthread_exit(NULL);
}
OUTPUT IN SCREENSHOT


Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images

- Please make a script in C that follows the picture's instruction, BUT with one change. Please write the script so that the computation of the distance between vertices is performed with a UDF. ALSO, PLEASE please please only utilize the stdio.h and math.h header files, please. NO iostream or cacio or anything like that, please. Thank you!!arrow_forwardUse stacks to implement a postfix-to-infix translator. Until the user closes the programme, it should continually read a postfix expression and output the identical infix expression. If the postfix expression entered is incorrect, throw an exception.arrow_forwardWhat is the implementation of each instruction located in the main function of the following code? This is a sample program, i am trying to teach myself linked list and am having trouble ------------------------------------------------------------------------------- #include <iostream>#include <string> /*** node* * A node class used to make a linked list structure*/ template<typename type>struct node {node() : data(), next(nullptr) {}node(const type & value, node * link = nullptr) : data(value), next(link) {}type data;node * next;}; /*** print_list* @param list - the linked-list to print out* * Free function that prints out a linked-list space separated.* Templated so works with any node.* Use this for testing your code.*/ template<typename type>void print_list(node<type> * list) { for(node<type> * current = list; current != nullptr; current = current->next) {std::cout << current->data << ' ';}std::cout << '\n';} /***…arrow_forward
- TRUE OR FALSE We can use Generics to define a custom generic exception.arrow_forwardProvide full C++ Code: Your code must have the following three files: Playlist.hpp - Class declaration for a linked list of Nodes (very similar to lab 08 but with some changes to methods, as described below) with following private data members: private:Node* first;Node* last;int size; Playlist.cpp - Class definition main.cpp - main() function Checkpoint A Build a playlist (of songs) by reading data members of several songs from a file and printing them. Implement the following methods: PlayList(); //Default constructorPlayList(string filename); //Parameterized constructor that reads data members of song from a file and builds the PlayList~PlayList(); //Deletes all nodes of the linked listbool InsertNodeLast(Node *myNewNode); //Inserts myNewNode at the end of the linked listbool InsertNodeFirst(Node *myNewNode); //Inserts myNewNode at the start of the linked listbool DeleteFirst(); //Deletes the first node of the linked listint Size() const { return size; }friend ostream&…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





