Complex Atomic Actions and Mutual Exclusion Problem Complex

Complex Atomic Actions and Mutual Exclusion Problem

Complex Atomic Actions and Mutual Exclusion Problem atomic compare - and - swap (common, old, new) {int temp; temp = common; if (common==old) {common = new;} return temp;} Solve the MEP problem using compare-and-swap by filling in the initialization, entry and exit protocols below. Assume that parameters are passed by reference (hence the assignment to common above will be seen by the caller).//NestedGreaterGreater put initialization code here. .. thread P: {while(true){//non-critical section//NestedGreaterGreater put entry protocol here.. .//critical section//NestedGreaterGreater put entry protocol here.. .//non-critical section} thread Q: {while(true){//non-critical section//NestedGreaterGreater put entry protocol here.. .//critical section//NestedGreaterGreater put entry protocol here.. .//non-critical section}

Solution

As per the above metnioned question we are suppose to solve the Mutual Exclusion Probem involving a Complex Atomic Action.

Now Mutual Exclusion states that no two process can enter into the critical section simultaneously. With reference to the above mentioned problem we have to give the code for three segments i.e.

1) Initialization Segement : This basically involves a set of code having a common variable to both threads, whose value decide which thread is subsequently going to enter the critical section.

2) Entry Section: Entry Section for Threads P and Q, which basically decide whether or not the respective threads can enter into their critical section depending upon some condtion.

3) Exit section: Exit Section, i.e when a given thread comes out of its critical section it has to excute some code which will consequently enable the other thread to enter its critcal section.

Since we are suppose to solve the Mutual Exclusion Problem, we can use Peterson\'s Solution.

Initialization Code:

#define Num 2 //Pre-processor directives where Num is givent the value of 2

#define TRUE 1 // True is assigned the value of 1

#define FALSE 0 // False is assigned the value of 2

int interested[Num] = FALSE; /* Here we have declared an array of size 2 as Num is 2, and both are intialized as 0, stating that both the threads are not interested to get into their critical section */

int Turn; // Turn is a random number;

Entry Section:

void Entry_Section(int thread);

void Entry_Section(int Thread) {

int other;

other = 1 - Thread;

interested[Thread] = TRUE;

Trun = Thread;

while( (interested[other] == TRUE) && (Turn == Thread) );

}

Exit Section:

void Exit_Section();

void Exit_Section(int Thread){

interested[Thread] = FALSE;

}

Now lets try and Trace the Peterson\'s Solution:

Given,

interested[] is going to store the inital state of the two thread and Turn is just a random variable.

Let us say Thread 1 enters the Entry Section and starts executing the code,

It calls the Entry_Section(int thread), with thread value as 0. Subsequently setting the value of \"other\" as 1 (Which is the Thread value for Thread 2) and interested[0] as TRUE.

while() is tested and as interested[other] is not equal to TRUE but FALSE and TURN is equal to process so the while loop breaks and the Thread 1 enters the critical section.

Incase if Thread 2 also enters the entry section it get stucks into the while loop until the Thread T1 enters the exit section and makes interested[Thread] = FALSE. Thereby enabling Thread 2 to enter the critical section and break out of the while loop.

Complex Atomic Actions and Mutual Exclusion Problem Complex Atomic Actions and Mutual Exclusion Problem atomic compare - and - swap (common, old, new) {int temp
Complex Atomic Actions and Mutual Exclusion Problem Complex Atomic Actions and Mutual Exclusion Problem atomic compare - and - swap (common, old, new) {int temp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site