How Does Down_Read(Semaphore) Work in Kernel?

If we let a thread hold a semaphore by down_read(&current->mm->mmap_sem) function, it provides the read-only access to mmap (list of vma areas), so other threads are not able to change mmap anymore. I'm reading the source code, but still confused about how down_read achieves that.

3

1 Answer

The basic idea is: free lock = 0

down_read() decrements lock ... -1 for each reader (only if it's <= 0)

up_read() increments lock ... +1 when 1 reader finished reading

down_write() increments lock to 1 ... only if it's 0 -- free

up_write() decrements back to 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest