By liran bh | 5/16/2016 | Linux User Space

Priority Ceiling Example

#include <pthread.h>

pid_t child_pid;
struct sched_param old_param;
struct pthread_mutex_t mutex;

void raise_child_priority()
{
  struct sched_param current;

  sched_getparam(0, &current);

  sched_getparam(child_pid, &old_param);

  if(current.sched_priority > old_param.sched_priority) {
    sched_setparam(child_pid, &current);
  }
}

void lower_child_priority()
{
  sched_setparam(child_pid, &old_param);
}

int access_shared_resource()
{
  if(pthread_mutex_trylock(&mutex) < 0) {
    if(errno == EBUSY) {
      raise_child_priority();
      if(pthread_mutex_lock(&mutex) < 0) return -1;
    }
    else {
      return -1;
    }
  }

  lower_child_priority();

  /* Do stuff with the locked resource */

  if(pthread_mutex_unlock(&lock) < 0) return -1;
  return 0;
}

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now