Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution to Exercise 3.47 #838

Open
clean99 opened this issue Nov 12, 2022 · 1 comment
Open

Solution to Exercise 3.47 #838

clean99 opened this issue Nov 12, 2022 · 1 comment
Labels
Exercise solution Solutions to textbook exercises _work in progress

Comments

@clean99
Copy link
Contributor

clean99 commented Nov 12, 2022

function make_semaphore(n) {
    const cell = list(0);
    function the_semaphore(m) {
        return m === "acquire"
               ? test_and_set(cell, n)
                   ? the_semaphore("acquire")
                   : true
               : m === "release"
               ? release(cell)
               : error(m, "unknown request -- semaphore");
    }
    return the_semaphore;
}
function release(cell) {
    if(head(cell) > 0) {
        set_head(cell, head(cell) - 1);
    }
}
function test_and_set(cell, n) {
    if(head(cell) < n) {
        set_head(cell, head(cell) + 1);
        return false;
    } else {
        return true;
    }
}
@martin-henz martin-henz added the Exercise solution Solutions to textbook exercises label Dec 29, 2022
@martin-henz
Copy link
Member

I think you misunderstood the question. You are supposed to implement semaphores in terms of the two synchronization primitives given in the text: mutexes, and test_and_set. You are not supposed to modify them, as you did with test_and_set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Exercise solution Solutions to textbook exercises _work in progress
Projects
None yet
Development

No branches or pull requests

2 participants