hw/study guide
DESCRIPTION
HW/Study Guide. Synchronization. Make sure you understand the HW pro blems!. global shared int counter = 0, BUFFER_SIZE = 10 ; Producer: while (1) { while (counter == BUFFER_SIZE) ; // do nothing buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/1.jpg)
HW/Study Guide
![Page 2: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/2.jpg)
Synchronization
• Make sure you understand the HW problems!
![Page 3: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/3.jpg)
global shared int counter = 0, BUFFER_SIZE = 10 ;
Producer: while (1)
{ while (counter == BUFFER_SIZE); // do nothing
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++; }
![Page 4: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/4.jpg)
Consumer:
while (1) {
while (counter == 0); // do nothingnextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;counter--;// consume the item
}
![Page 5: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/5.jpg)
• Identify the race condition in this version of the consumer/producer problem.– The race condition is the incrementing and
decrementing of the shared variable counter.
![Page 6: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/6.jpg)
• Fix this race condition using the TestAndSet hardware instruction.
global shared int counter = 0, BUFFER_SIZE = 10 ; shared int lock = 0 ;
Producer: while (1)
{ while (counter == BUFFER_SIZE); // do nothing
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
while (TestAndSet(lock) == 1) ;
counter++;lock = 0 ;
}
![Page 7: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/7.jpg)
Consumer:
while (1) {
while (counter == 0); // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
while (TestAndSet(lock) == 1) ; //busy wait
counter--;lock = 0 ;
}
global shared int counter = 0, BUFFER_SIZE = 10 ; shared int lock = 0 ;
![Page 8: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/8.jpg)
• Now assume there is still one producer but there are now two consumers. – Does this introduce any additional race
conditions (the correct answer is yes!)
![Page 9: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/9.jpg)
• If so, where does it occur?– The race condition occurs when the variable out is accessed
since this is now shared by the two consumers.
• Now fix this additional race condition using a semaphore.
![Page 10: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/10.jpg)
Consumer:
while (1) {
while (counter == 0); // do nothingwait(mutex) ;
nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;
signal(mutex) ;
while (TestAndSet(lock) == 1) ; //busy waitcounter--;
lock = 0 ; }
Note that the producer code does NOT have to be modified since it does not use out.
global shared int counter = 0, BUFFER_SIZE = 10 ; shared int lock = 0 ;
global shared int out = 0 ; //Now the two consumers must share out.
struct semaphore mutex = 1 ; //Must supply the semaphore
![Page 11: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/11.jpg)
• Assume I have just learned about using semaphores to synchronize the order in which certain statements are executed.
• I think this is really cool, and want to give it a try. So I want to use semaphores to enforce the following execution order:– Statement S1 of process P1 executes before
statement S2 of process P2. – Statement S2 of process P2 executes before
statement S3 of Process P3.
![Page 12: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/12.jpg)
• Statement S3 of process P3 executes before Statement S1 of process P1.
• Use semaphores to enforce this ordering, or, show how this may not be such a great idea (i.e., what is the problem here?).
![Page 13: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/13.jpg)
• This ordering cannot be enforced since it creates a cyclic waiting condition that would result in deadlock. This can be seen clearly when you look at the requested ordering of the statements:
S1 S2 S3
![Page 14: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/14.jpg)
• Now assume we have four processes and want Statement S1 in P1 to execute before statement S2 in P2 before S3 in P3. Also, we want Statement S4 in P4 to execute after S2 in P2.
• Use semaphores to enforce this ordering. You must explicitly initialize any semaphores you use.
struct semaphore S1 = 0 ; struct semaphore S2 = 0 ;
P1: P2: P3: P4:
S1 ; wait(S1) ; wait(S2) ; wait(S2) ;signal(S1) ; S2 ; S3 ; S4 ;
signal(S2) ; signal(S2) ;
![Page 15: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/15.jpg)
• Assume there is one producer process and one consumer process, and that they share a buffer with 10 slots.
• Implement the producer/consumer problem using semaphores. You must explicitly initialize the semaphores that you use.
![Page 16: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/16.jpg)
BUFFER_SIZE = 10 ; struct semaphore full = 0 ; struct semaphore empty = 10 ;
Producer: while (1)
{wait(empty) ;
buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;
signal(full) ; }
![Page 17: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/17.jpg)
Consumer:
while (1) { wait(full) ;
nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;
signal(empty) ;
}
![Page 18: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/18.jpg)
Paging
• Assume a 16-bit virtual address space with pages that are 2048 bytes. How many pages are in the logical address space?
– The number of bits required to access any byte in the page (i.e., the offset) is 11. This leaves 5 bits for the logical page. Thus there are 2^5 or 32 pages.
![Page 19: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/19.jpg)
Paging
• Consider the page table (shown on the next slide) for some process in this same system, and assume the logical address is 2052. To what physical address will this logical address be mapped? Show the steps you took to determine this address.
![Page 20: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/20.jpg)
• Step 1: Convert to binary: 2052 = 00010 00000000100.• Step 2: Take the top most 5 bits and use them as an index into the
process page table. 00010 00000000100.
0
2
1
3
21
![Page 21: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/21.jpg)
• Step 3: Take the physical page frame number from the page table
00010
0
2
1
3
21
00001
![Page 22: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/22.jpg)
• Step 4: Concatenate the offset to the physical page frame to get final address. 00010
0
2
1
3
21
0000100000000100
= 2052.
![Page 23: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/23.jpg)
• What is the Translation Lookaside buffer?– A very fast associative memory that caches page table entries.
• What purpose does it serve?– It avoids having to go to the page table in memory if the entry is
found in the Translation Lookaside buffer. Thus it avoids the latency of accessing main memory.
![Page 24: HW/Study Guide](https://reader036.vdocuments.net/reader036/viewer/2022062309/56815b01550346895dc8b453/html5/thumbnails/24.jpg)
• Consider a 64-bit address space with 4K pages. How many pages are there in this virtual address space?
– Since the page offset requires 12 bits this leaves 54 bits for the logical page number. Thus there are 2^54 logical pages in the address space.
• Is this a big number?– Yes, it is a big number.
• You will most likely be asked to work through a two-level page table example.