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
Post a Comment