Question
Suppose the CPU on which OS runs supports an atomic instruction fetch_and_increment as
follows:
int fetch_and_increment (int *x) {
last_value = *x;
*x = *x + 1;
return last_value;
}
Show how can use the above instructions to implement mutual exclusion. Your answer should be
in two distinct parts:
(i) Give a solution that achieves mutual exclusion and progress, but has starvation,
(ii) Give a modified solution that also achieves bound waiting.
For each part, just show the entry section and exit section codes clearly, no explanation is
needed.
Instructions:
● Word limit 250 – 300
● Plagiarism free
● Referencing Style APA
Question:
Question
Suppose the CPU on which OS runs supports an atomic instruction fetch_and_increment as
follows:
int fetch_and_increment (int *x) {
last_value = *x;
*x = *x + 1;
return last_value;
}
Show how can use the above instructions to implement mutual exclusion. Your answer should be
in two distinct parts:
(i) Give a solution that achieves mutual exclusion and progress, but has starvation,
(ii) Give a modified solution that also achieves bound waiting.
For each part, just show the entry section and exit section codes clearly, no explanation is
needed.
Instructions:
● Word limit 250 – 300
● Plagiarism free
● Referencing Style APA
This is AI generated Response.
Asked by: Meghana
Created at: 2025-07-24 11:08:47
Final Answer:
RenderedRaw Text
Did you like this answer?
Certainly! Below are the solutions as per your requirements, using the atomic `fetch_and_increment` instruction as described:
---
### (i) Solution Achieving Mutual Exclusion and Progress (But Allows Starvation)
```c
// Shared variable
int lock = 0;
// Entry Section
while (fetch_and_increment(&lock) != 0) {
// Busy-wait (spin) until lock is acquired
}
// Critical Section
// ...
// Exit Section
lock = 0;
```
---
### (ii) Modified Solution Achieving Mutual Exclusion, Progress, and Bounded Waiting
```c
#define N // number of processes (must be defined)
int ticket = 0;
int turn = 0;
// Per-process local variable
int my_ticket;
// Entry Section
my_ticket = fetch_and_increment(&ticket);
while (my_ticket != turn) {
// Busy-wait (spin) until it's this process's turn
}
// Critical Section
// ...
// Exit Section
turn = turn + 1;
```
---
**References:**
- Silberschatz, A., Galvin, P. B., & Gagne, G. (2018). *Operating System Concepts* (10th ed.). John Wiley & Sons.
- Tanenbaum, A. S., & Bos, H. (2015). *Modern Operating Systems* (4th ed.). Pearson.
---
**Note:**
- In (i), the solution is a simple spinlock that can starve processes.
- In (ii), the solution is similar to the ticket lock algorithm, ensuring bounded waiting by serving requests in order of arrival.
- Only entry and exit sections are shown, as requested.
VIPSolutions focuses on clear, step‑by‑step explanations so you can learn quickly. Whether you need an AI‑generated walkthrough or a short hint to get unstuck, each solution is organized for fast reading and easy review later.
Search similar questions, compare approaches, and bookmark the best answers for revision. Our goal is simple: quick, reliable study help that feels natural—not noisy.