<< Chapter < Page Chapter >> Page >

Locking to avoid race condition

We can modify the Generalized Race Condition example to eliminate its race condition. The lock controls access to the shared resource, i.e. , the shared variable z . 1 /* Number of copies of the process to run. */ 2 #define NUM_PROCS 33 4 show int z = 0;5 show bool locked = false; 67 active[NUM_PROCS] proctype increment()8 { 9 show int new_z;10 11 atomic {12 !locked ->/* Lock available? */ 13 locked = true; /* Acquire lock. */14 } 1516 /* Critical section of code. */ 17 new_z = z;18 new_z++; 19 z = new_z;20 21 locked = false; /* Release lock. */22 /* End critical section of code. */ 23 }

As described before, when using a lock, we need to acquire thelock, execute the critical section, and release the lock. Releasing the lock is the final action of the critical section,as it allows another process to acquire the lock. To acquire the lock, clearly we need to check whether it is available.This guard is where each process potentially blocks, waiting for its turn to get the block.

The atomic keyword, as its name implies, makes a group of statements atomic.Without it, two processes could first each determine the lock was available, then eachacquire the lock, defeating the purpose of the lock.

What values of z are possible at the end of this program?

As desired, only the value 3, since there are 3 processes.

Run this code several times and observe its behavior. Also, eliminate the atomic keyword and its associated braces, and repeat.

Assume that the atomic keyword is removed, and there are only two processes, instead of three.Give a trace where both are in the critical section at the same time.What are the differences in the state spaces with and without the atomic ?

The following figure shows one such trace. Notice that z is effectively incremented only once, instead of twice.

An undesired possible trace for increment() , if atomic keyword were removed. For brevity, states show the line number for each process,the z and locked values, and each process' new_z value, respectively.

The critical difference in state spaces is that the atomic keyword prohibits processes interleaving between the guard and its body.This reduces the size of the state space. In this case, as in many others, it also reduces the numberof different end states and, thus, possible result values.

Tiny server and clients

We now have the tools to demonstrate how a tiny server and its clients can be modeled. The server process is a loop that repeatedlywaits for and then processes requests. Clients send requests. For simplicity, in this example, the server won't communicateany results back to the clients, and the clients will only make one request each.

Servers generally allow multiple kinds of requests. Here, we'll use one more new piece of Promela syntax, mtype , to define some named constants, similar to an enumerated type.

/* Number of each type of client. */ #define NUM_CLIENTS 1/* Define the named constants. */ mtype = {NONE, REQUEST1, REQUEST2};/* Declare a shared variable. */ show mtype request = NONE;active proctype server() {do :: request == REQUEST1 ->printf("Processing request type 1.\n"); request = NONE;:: request == REQUEST2 ->printf("Processing request type 2.\n"); request = NONE;od; }active[NUM_CLIENTS] proctype client1(){ atomic{request == NONE ->request = REQUEST1; }} active[NUM_CLIENTS]proctype client2() {atomic{ request == NONE ->request = REQUEST2; }}

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Model checking concurrent programs. OpenStax CNX. Oct 27, 2005 Download for free at http://cnx.org/content/col10294/1.3
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Model checking concurrent programs' conversation and receive update notifications?

Ask