0

Two tasks with different priority are waiting on same semaphore, once semaphore gets released task with high priority gets scheduled ? or its random ?, am using SCHED_RR scheduler policy.

digitizedx
  • 386
  • 5
  • 16

2 Answers2

1

Generally speaking, I know of no rule which waiting task gets woken up first when a semaphore is released, so it is up to the scheduler's choice. The "priority" of the tasks probably only is relevant for the scheduler in case of the normal scheduling mechanism, not the synchronizing due to semaphores.

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • So I cannot assume that my high priority task will get to run first when the semaphore is released. What will be behavior of Linux kernel scheduler? It randomly picks up a task which was waiting or is it like it will pick up the task that went into the queue first? – digitizedx Jul 03 '13 at 14:29
  • Undefined. You may try out, but expect to experience changes in later versions. If you need to rely on the behavior, use a different technique, not just semaphores and priorities. – Alfe Jul 03 '13 at 14:33
1

If you are using SCHED_RR then scheduler runs tasks with highest priority, and runs such tasks in the first place. If there is task with SCHED_RR and it in state TASK_RUNNING, it will run.

On uniprocessor system, if exist task with SCHED_RR and TASK_RUNNING then only this task will be executing. But on multi-core system, task with lower priority could be scheduled on another processor.

In my opinion, task with higher priority and SCHED_RR scheduled first, but there is no waranty that this task gets semaphore first, because this processor might do more important work, such as handle interrupts.

Again, this is my only opinion, and I'm fairly new to linux kernel. It would be great to have somebody more experienced to approve it.


Edit:

Scheduler is not important for semaphore. It just wakes up one task regardless of it priority.

So, you can obtain lock first if your task first tries to obtain lock (it's hard and not safe). Or you could manage semaphore queue by yourself.

Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35