Your programming buddy notices that your code has some race conditions, and draws up the following locking mechanism: int locked = 0; void lock() { while (locked == 1) { } continue; // keep looping locked = 1; void unlock() { } locked = 0; Because the load-increment-store sequence is the critical section of your program, you place a call to lock() immediately before the i++ line, and a call to unlock() immediately after. However, running your supposedly-now-threadsafe program again, you discover that the output is still nondeterministic. Turns out your buddy's clever locking scheme doesn't do a very good job protecting the critical section after all. (Note: unlike semaphore locking and unlocking functions, lock() and unlock() DO NOT execute atomically.) C. Give an execution sequence of two threads in the lock() function that would end up with both threads holding the lock at the same time.
Your programming buddy notices that your code has some race conditions, and draws up the following locking mechanism: int locked = 0; void lock() { while (locked == 1) { } continue; // keep looping locked = 1; void unlock() { } locked = 0; Because the load-increment-store sequence is the critical section of your program, you place a call to lock() immediately before the i++ line, and a call to unlock() immediately after. However, running your supposedly-now-threadsafe program again, you discover that the output is still nondeterministic. Turns out your buddy's clever locking scheme doesn't do a very good job protecting the critical section after all. (Note: unlike semaphore locking and unlocking functions, lock() and unlock() DO NOT execute atomically.) C. Give an execution sequence of two threads in the lock() function that would end up with both threads holding the lock at the same time.
Related questions
Question
![Your programming buddy notices that your code has some race conditions, and draws up the following
locking mechanism:
int locked = 0;
void lock() {
while (locked == 1) {
}
continue; // keep looping
locked = 1;
void unlock() {
}
locked = 0;
Because the load-increment-store sequence is the critical section of your program, you place a call to
lock() immediately before the i++ line, and a call to unlock() immediately after. However, running
your supposedly-now-threadsafe program again, you discover that the output is still nondeterministic.
Turns out your buddy's clever locking scheme doesn't do a very good job protecting the critical section
after all. (Note: unlike semaphore locking and unlocking functions, lock() and unlock() DO NOT
execute atomically.)
C.
Give an execution sequence of two threads in the lock() function that would end up with
both threads holding the lock at the same time.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3b25bdcc-1f5e-4716-88d3-d1e15ee2df25%2Fbf93ae45-df16-4042-b5c4-27ced637de3b%2Fif32rf_processed.jpeg&w=3840&q=75)
Transcribed Image Text:Your programming buddy notices that your code has some race conditions, and draws up the following
locking mechanism:
int locked = 0;
void lock() {
while (locked == 1) {
}
continue; // keep looping
locked = 1;
void unlock() {
}
locked = 0;
Because the load-increment-store sequence is the critical section of your program, you place a call to
lock() immediately before the i++ line, and a call to unlock() immediately after. However, running
your supposedly-now-threadsafe program again, you discover that the output is still nondeterministic.
Turns out your buddy's clever locking scheme doesn't do a very good job protecting the critical section
after all. (Note: unlike semaphore locking and unlocking functions, lock() and unlock() DO NOT
execute atomically.)
C.
Give an execution sequence of two threads in the lock() function that would end up with
both threads holding the lock at the same time.
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)