multithreading - How is semaphore queue protected with multiple threads -


in 1 of interviews asked...

wait(semaphore sem) {                               disable_ints    sem.val--    if (sem.val < 0){       add thread sem.l       block(thread)     } enable_ints 

the above semaphore wait implementation (copied other thread). how queue sem.l protected when multiple threads try enqueue (when fail lock) ? take lock before updating queue ?

this implementation system single core. concurrency managed disabling interrupts during critical sections. that's "disable_ints" , "enable_ints" in code.

unless interviewing position developing multi-tasking operating system code single-core systems, it's strange question ask. perhaps interviewer expecting ask questions code , see if understand under circumstances make sense.

conflict can occur code running on core or core. since there's 1 core, possible conflict code running on care. require context switch.

context switches come in 2 forms, voluntary , involuntary. voluntary context switch occurs when code explicitly asks one. code doesn't ask context switch, that's not issue. involuntary context switch can triggered interrupt, , code disables interrupts in critical section. typical way "locks" implemented in kernel code single core systems.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -