Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. (40 points] Consider a shared variable x, initialized to zero, is operated on

ID: 3733108 • Letter: 4

Question

4. (40 points] Consider a shared variable x, initialized to zero, is operated on by four processes A,B, C, and D. Processes A and B increment x by one while processes C and D decrement x by two. Each process, before reading, performs wait on a semaphore S and signal on S after store. S is initialized with 2 (i.e., S=2). Find what the maximum and minimum values of x are after processes complete execution. Also, show the two sequences (i.e., trace the sequence of interleavings of statements – context switches) for getting these maximum and minimum values. Find the visual representation of these four processes in memory below. D A. wait(s) read(x) x++ write(x) signal(x). wait(s) read(x) x++ write(x) signal(x) wait(s) read(x) x++ write(x) signal(x) wait(s) read(x) X++ write(x) signal(x)

Explanation / Answer

minimum value of x will be -4 and maximum value will be 2.

Minimum:

C:

wait(s) // s=1

read(x) // C reads x as 0

A:

wait(s) // s= 0

read(x) // A reads x as 0

x++ // x becomes 1

write(x) // x is written back in memory as 1

signal(x) // s=1

B:

wait(s) // s= 0

read(x) // B reads x as 1

x++ // x becomes 2

write(x) // x is written back in memory as 2

signal(x) // s=1

C:

x=x-2 // decrements the first read x=0 by 2 i.e x becomes -2

write(x) // x is overwritten as -2.

signal(x) // s=2

D:

wait(s) // s=1

read(x) // x is read as -2 by D

x=x-2 // x becomes -4

write(x) // x is written back as -4 in memory

signal(s) // s=2

So, the minimum value will be -4.

Maximum:

A:

wait(s) // s=1

read(x) // A reads x as 0

C:

wait(s) // s= 0

read(x) // C reads x as 0

x=x-2 // x becomes -2

write(x) // x is written back in memory as -2

signal(x) // s=1

D:

wait(s) // s= 0

read(x) // D reads x as -2

x=x-2 // x becomes -4

write(x) // x is written back in memory as -4

signal(x) // s=1

A:

x++ // increments the first read x=0 by 1 i.e x becomes 1

write(x) // x is overwritten as 1

signal(x) // s=2

B:

wait(s) // s=1

read(x) // x is read as 1 by B

x++ // x becomes 2

write(x) // x is written back as 2 in memory

signal(s) // s=2

So, maximum value is 2.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote