multithreading - Thread scheduling in unix -


in rr scheduling policy happen if low priority thread locks mutex , removed scheduler because high priority thread waiting?

will releases lock held low priority thread?

for example consider 3 threads running in process priorities 10,20 , 30 in rr scheduling policy.

now @ given point of time low priority thread 1 locks mutex , still doing execution mean while high priority thread pops in , waits on mutex held thread 1. thread 2 comes in picture needs same mutex locked thread 1.

as far know per scheduling algorithm threads sleeping or waiting mutex,semaphore etc removed , other ones, having low priority allowed execute. correct? if so, in above example high priority threads wait completion of low priority thread doesn't make sense. how system works if @ threads designed said above?

or thread priority should set in such way high priority one's not depend on low priority one's mutexe's ?

also can please explain me how scheduling works @ process level? how set priority process?

typically, scheduling , locks unrelated in other aspect "waiting thread not scheduled until it's finished waiting". rather daft have mutex "stops other thread accessing data" works if other thread has same or lower priority current thread.

the phenomena of "a low priority holds lock high priority thread 'needs'" called priority inversion, , it's known scenario in computer theory.

there schemes "temporarily increase priority of lock-holding thread until releases lock highest priority of waiting threads" (or priority of first waiting thread if it's higher current thread, or other variation on theme). done combat priority inversion - has other drawbacks too, it's not implemented in os's/schedulers (after all, affects other threads 1 waiting too).

edit:

the point of mutex (or other similar locks) prevents 2 threads accessing same resources @ once. example, want update 5 different variables pretty lengthy processing (complicated math, fetching data serial port or network drive, or such), if 2 of variables, other process using these invalid result, can't "let go" of lock.

the high priority thread has wait 5 variables updated , low priority lock.

there no simple workaround application can "fix" problem - don't hold locks more necessary of course [and may can fix problem described above, performing lengthy processing outside of lock, , final "store in 5 variables" lock on. reduce potential time high priority thread has wait, if system busy, won't fix problem.

there whole phd thesis written on subject, , i'm not specialist on "how write scheduler" - have fair idea how 1 works, know how engine in car works - if gave me bunch of suitable basic shapes of steel , aluminium, , required tools/workspace , told me build engine, doubt work great...same scheduler - know parts called, not how build one.


Comments