SHARE
TWEET

Untitled

a guest Nov 17th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool wantp = false;
  2. bool wantq = false; // global variables that shares the intention for entering critical section of the 2 processes
  3. int turn = 1; // global variable that sets the priority to one process, intially set on process p
  4.  
  5. //process p
  6. active proctype p()
  7. {
  8.     //outer while loop
  9.     do
  10.         ::true ->
  11.         wantp = true; // process p shares its intetion to enter critical section
  12.         do
  13.             :: wantq == true -> // checks if process q is also inteding to enter critical section
  14.             if
  15.                 :: turn == 2 -> // in the case when both processes intend to enter critical section, check for priority
  16.                 wantp = false; // if priority is set on process q then set off process p intention
  17.                 (turn == 1); // wait for priority to be set on process p
  18.                 wantp = true; // once priority set on process p, process p intention to enter critical section is set on true
  19.             :: else -> skip // if priority not set on process q, then skip
  20.             fi;
  21.         :: else -> break // if process q not intending to enter critical section, than go to own critical section
  22.         od;
  23.         printf("critical section p"); // execute critical section
  24.         turn = 2; // set priority to the other process
  25.         wantp = false; // set off intention to enter critical section
  26.     od;
  27.  }
  28.  
  29. //process p
  30. active proctype q()
  31. {
  32. //outer while loop
  33. do
  34.     ::true ->
  35.     wantq = true; // process q shares its intetion to enter critical section
  36.     do
  37.         :: wantp == true -> // checks if process p is also inteding to enter critical section
  38.         if
  39.             :: turn == 1 -> // in the case when both processes intend to enter critical section, check for priority
  40.             wantq = false; // if priority is set on process p then set off process q intention
  41.             (turn == 2); // wait for priority to be set on process q
  42.             wantq = true; // once priority set on process q, process q intention to enter critical section is set on true
  43.         :: else -> skip // if priority not set on process p, then skip
  44.         fi;
  45.     :: else -> break // if process p not intending to enter critical section, than go to own critical section
  46.     od;
  47.     printf("critical section q"); // execute critical section
  48.     turn = 1; // set priority to the other process
  49.     wantq = false; // set off intention to enter critical section
  50. od;
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top