Question

In: Computer Science

Consider the following program

Consider the following program:
boolean blocked [2];
int turn;
void P (int id)
{
while (true) {
blocked[id] = true;
while (turn != id) {
while (blocked[1-id])
/* do nothing */;
turn = id;
}
/* critical section */
blocked[id] = false;
/* remainder */
}
}
void main()
{
blocked[0] = false;
blocked[1] = false;
turn = 0;
parbegin (P(0), P(1));
}

Solutions

Expert Solution

Consider the case in which turn equals 0 and P(1) sets blocked[1] to true and then finds blocked[0] set to false. P(0) will then set blocked[0] to true, find turn = 0, and enter its critical section. P(1) will then assign 1 to turn and will also enter its critical section. The error was pointed out in [RAYN86].


 P(1) will then assign 1 to turn and will also enter its critical section. The error was pointed out in [RAYN86].

Related Solutions

Consider the following program. There are 11 statements in the program starting with int x; Show...
Consider the following program. There are 11 statements in the program starting with int x; Show the memory allocated for the program in the stack and heap for each statement. Also, show any values assigned to each of these memory locations. In other words, you will show 11 stacks and 11 heaps as the answer to your question. #include <iostream> using namespace std; int main() { int x; // Stmt 1 int * y; // Stmt 2 y = new...
Consider the following program. // This program averages 3 test scores. It repeats as // many...
Consider the following program. // This program averages 3 test scores. It repeats as // many times as the user wishes. #include <iostream> using namespace std; int main() { int score1, score2, score3; // Three scores double average; // Average score char again; // To hold Y or N input do { // Get three scores. cout << "Enter 3 scores and I will average them: "; cin >> score1 >> score2 >> score3; // Calculate and display the average....
Consider the following program that creates an ArrayList of objects of a type A, and sorst...
Consider the following program that creates an ArrayList of objects of a type A, and sorst them. Supply the missing code. Sample output when you run the program is shown below. import java.util.*; class A { int i, j, k; public A(int i, int j, int k){ this.i=i; this.j=j; this.k=k; } public String toString(){ return "A("+i+","+j+","+k+")"; } } class SortA { public static void main(String[] args){ ArrayList aL=new ArrayList(); Random rand= new Random(1000); //1000 is a seed value for (int...
Parameter Mystery. Consider the following program. List below the exact output produced by the above program...
Parameter Mystery. Consider the following program. List below the exact output produced by the above program (we do not claim that every line of output makes sense ;-). public class Mystery { public static void main(String[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; statement(george, mary, john); statement(fun, "george", "work"); statement(mary, john, fun); statement(george, "john", "dance"); } public static void statement(String mary, String john, String fun) { System.out.println(john + "...
A statistical program is recommended. Consider the following data for a dependent variable y and two...
A statistical program is recommended. Consider the following data for a dependent variable y and two independent variables, x1 and x2. x1 x2 y 30 12 94 47 10 108 25 17 112 51 16 178 40 5 94 51 19 175 74 7 170 36 12 117 59 13 142 76 16 211 (a) Develop an estimated regression equation relating y to x1. (Round your numerical values to one decimal place.)ŷ =   Predict y if x1 = 43. (Round...
Consider the following linear program:    MAX Z = 25A + 30B    s.t. 12A +...
Consider the following linear program:    MAX Z = 25A + 30B    s.t. 12A + 15B ≤ 300    8A + 7B ≤ 168 10A + 14B ≤ 280    Solve this linear program graphically and determine the optimal quantities of A, B, and the    value of Z. Show the optimal area.
Consider the following Java program. Describe what it does in response to specific operations of the...
Consider the following Java program. Describe what it does in response to specific operations of the mouse, and how it does it. (You are encouraged to run the program for yourself to test its behavior. Then read through the program carefully to understand how that behavior arises.) import java.awt.event.*; import javax.swing.*; public class MouseWhisperer extends JFrame implements MouseListener {     MouseWhisperer() {         super("COME CLOSER");         setSize(300,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         addMouseListener(this);         setVisible(true);     }     public void mouseClicked(MouseEvent e)...
Consider the following code section of a parallel program that computes the sum of n values...
Consider the following code section of a parallel program that computes the sum of n values using p processors: my_sum = 0; my_first_i = ... ; my_last_i = ... ; for(my_i = my_first_i; my_i < my_last_i; my_i++) { my_x = Compute_next_value(...); my_sum += my_x; } Here the prefix my_ indicates that each core is using its own, private variables, and each core can execute this block of code independently of the other cores. Devise formulas for the functions that calculate...
Consider the following C++ program that makes use of many features that are unique to C++...
Consider the following C++ program that makes use of many features that are unique to C++ and did not exist in C. #include <iostream> #include <string> #include <cstdint> #include <vector> using namespace std; enum LetterGrade { A = 4, B = 3, C = 2, D = 1, F = 0 }; // type T must be castable into a double template<class T> double getArrayAverage(vector<T>& vec) { double sum = 0; for (const auto& value : vec) { sum +=...
A statistical program is recommended. Consider the following data for two variables, x and y. x...
A statistical program is recommended. Consider the following data for two variables, x and y. x 22 24 26 30 35 40 y 12 20 33 35 40 36 (a) Develop an estimated regression equation for the data of the form ŷ = b0 + b1x.  (Round b0 to one decimal place and b1 to three decimal places. ŷ = (b) Use the results from part (a) to test for a significant relationship between x and y. Use α = 0.05....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT