{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

20 OS Assisted Synchronization

20 OS Assisted Synchronization - CS-350 Fundamentals of...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS-350: Fundamentals of Computing Systems Page 1 of 9 Lecture Notes © Azer Bestavros. All rights reserved. Reproduction or copying (electronic or otherwise) is expressly forbidden except for students enrolled in CS-350. Concurrency and Synchronization: Basic Primitives and Constructs So far, we have established that hardware approaches relying on disabling interrupts do not solve the mutual exclusion problem in a satisfactory manner: they do not work with multi- processor systems, and even for uni-processor systems, they are not practical, since disabling interrupts is a privilege that we may not want to extend to application programmers. Other Hardware-Assisted Solutions to Mutual Exclusion The issue with our very early attempts at mutual exclusion, using a simple “flag” to signal availability/unavailability of critical section was due to the fact that a process needs to check such a flag and, if it is available (i.e., it is false), it needed to set it (i.e., it sets it to true). Because there were at least two instructions involved, namely (1) reading/checking the flag, and (2) possibly setting the flag, race conditions were possible, whereby a process would be interrupted after finishing the first step, but before completing the second. Given that disabling interrupts (to prevent that from occurring) is not an acceptable solution (not to mention that it won’t work for multi-processor systems), what else could we do? If we can perform both steps in an atomic (i.e., indivisible) fashion—i.e., as a single memory access instruction—we would be all set. Indeed, this is the motivation for atomic machine language instructions such as the test-and-set .” bool testset(int& i) { if (i==0) { i=1; return true; } else { return false; } } Figure 1 Functionality of a test-and-set() atomic instruction Figure 1 shows the functionality of the hardware “ test-and-set ” instruction. It allows a process to atomically (i.e., indivisibly) check if a flag (namely integer i ) is 0. If it is, the call to the “ test-and-set ” instruction succeeds returning “ true ” and the flag’s value is set to 1. If
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS-350: Fundamentals of Computing Systems Page 2 of 9 Lecture Notes © Azer Bestavros. All rights reserved. Reproduction or copying (electronic or otherwise) is expressly forbidden except for students enrolled in CS-350. it is not, the “ test-and-set ” instruction fails returning “ false ” and the flag’s value remains set to 1. Let us now review how this hardware primitive could be used for mutual exclusion. Figure 2 shows such an implementation in which the variable “ b ” is used as the flag signifying whether the critical section is available or not. Clearly, since the “ test-and-set ” instruction is executed atomically, there is no way a race condition could occur and hence mutual exclusion is guaranteed.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern