Question
code given #include #include #include #define TOTAL_THREADS 2 int count; int turn; // Shared variable, indicates // whose turn it is to execute bool interested[TOTAL_THREADS];
code given
#include
#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
// 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
./peterson Part 1: Peterson's Solution (15 points) 1. Execute the peterson program several times. 2. Examine the output carefully. You should notice a problem in the implementation. Make sure to follow the logic in main() and to read the comments carefully 3. Review Peterson's solution to achieve mutual exclusion. Pay special attention to the algorithm and code used to implement it. You may want to refer to the prep materials for background info (section 2.3.3 in the textbook) 4. Correct the problem. Look for the // TODO comments and address them (i.e., implement the functionality described in the comments) 5. Build and run your program and make sure that it works correctly. 90 Option: Peterson's solution [sw) #define FALSE 0 #define TRUE ! #define N 2 /* number of processes * int turn; int interested[N]; /whose turn is it? / /* all values initially O (FALSE) / void enter_region (int process); /* process is O or 1*/ int other; /* number of the other process */ other= 1-process; interested[process] TRUE; turn = process; while (turn == process && interested[other] == TRUE) /* null statement */ ; /* the opposite of process* /show that you are interested /* set flag */ void leave_regio (int process) /* process: who is leaving */ interested[process] FALSE; /* indicate departure from critical region Pros: No strict alternation; Cons: Busy waiting ./peterson Part 1: Peterson's Solution (15 points) 1. Execute the peterson program several times. 2. Examine the output carefully. You should notice a problem in the implementation. Make sure to follow the logic in main() and to read the comments carefully 3. Review Peterson's solution to achieve mutual exclusion. Pay special attention to the algorithm and code used to implement it. You may want to refer to the prep materials for background info (section 2.3.3 in the textbook) 4. Correct the problem. Look for the // TODO comments and address them (i.e., implement the functionality described in the comments) 5. Build and run your program and make sure that it works correctly. 90 Option: Peterson's solution [sw) #define FALSE 0 #define TRUE ! #define N 2 /* number of processes * int turn; int interested[N]; /whose turn is it? / /* all values initially O (FALSE) / void enter_region (int process); /* process is O or 1*/ int other; /* number of the other process */ other= 1-process; interested[process] TRUE; turn = process; while (turn == process && interested[other] == TRUE) /* null statement */ ; /* the opposite of process* /show that you are interested /* set flag */ void leave_regio (int process) /* process: who is leaving */ interested[process] FALSE; /* indicate departure from critical region Pros: No strict alternation; Cons: Busy waiting
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started