Obtain a list of process scheduling policy and priority

Normally, you can read the file /proc/[pid]/sched and get the related information. But since I am using a simplified one, the sched file is not presented. And I need to figure out another way to get it. I finally found there is a C library, <sched.h>, to do that. You can find the following declarations in the library.

In this case, we only need sched_getparam() to obtain scheduling priority and sched_getscheduler() to obtain scheduling policy.

Sample output of getting scheduling policy and priority

Let’s see some details from the man page.

DESCRIPTION
sched_getparam() retrieves the scheduling parameters for the process identified by pid. If pid is zero, then the parameters of the calling process are retrieved.
============
sched_getscheduler() queries the scheduling policy currently applied to the process identified by pid. If pid equals zero, the policy of the calling process will be retrieved.

RETURN VALUE
On success, sched_setparam() and sched_getparam() return 0. On error, -1 is returned, and errno is set appropriately.
============
On success, sched_setscheduler() returns zero. On success, sched_getscheduler() returns the policy for the process (a non-negative integer). On error, -1 is returned, and errno is set appropriately.

That means, for sched_getparam(), the value of parameter is written into the struct sched_param on success and the scheduling priority is a parameter in the struct. So, you can get and print it!

For sched_getscheduler(), the return value is scheduling policy on sucess. But it returns a number only. Some people asked that how we can know the actual policy and the man page doesn’t say about it. In fact, if you check <sched.h>, it usually defines some macros for scheduling policies with the identifier name,

  1. SCHED_OTHER (or SCHED_NORMAL)
  2. SCHED_FIFO
  3. SCHED_RR
  4. SCHED_BATCH
  5. SCHED_IDLE

Yet, it is not necessary that all the policies appear because of variation between different versions of the library. Thus, you can just simply compare the number with the name. Here is a sample code written by me.

For more details, please also check the man pages:
http://linux.die.net/man/2/sched_getparam
http://linux.die.net/man/2/sched_getscheduler

Hope you find this useful.

Comments

comments

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">