Saturday, June 4, 2011

How can I set real time priority for a process

You can use chrt command to set or retrieve the real-time scheduling attributes / scheduling priority of an existing PID. From its man page:
The scheduler is the kernel part that decides which runnable process will be executed by the CPU next. The Linux scheduler offers three different scheduling policies, one for normal processes and two for real-time applications.
1. SCHED_OTHER – the default universal time-sharing scheduler policy used by most processes.
2. SCHED_FIFO or SCHED_RR – intended for special time-critical applications that need precise control over the way in which runnable processes are selected for execution
3. SCHED_BATCH – intended for “batch” style execution of processes
The scheduler is the Linux kernel part that decides which runnable process will be executed by the CPU next. As I said earlier Linux offers three scheduling policies. One is for normal process and can be changes using nice or chrt command (or use system calls under C).
Let us get back to chrt command. You can retrieve the real-time attributes of an existing task/pid with following command:
$ chrt -p 1
Output:
pid 1′s current scheduling policy: SCHED_OTHER
pid 1′s current scheduling priority: 0

Set process (PID 5124) to 5 priority: remember priority range from -20 (most favorable scheduling) to 19 (least favorable):
$ chrt -p 5 5124
You can retrieve minimum and maximum valid priorities with following command:
$ chrt -m
Output:
SCHED_FIFO min/max priority     : 1/99
SCHED_RR min/max priority       : 1/99
SCHED_OTHER min/max priority    : 0/0

Set scheduling policy to SCHED_RR:
$ chrt -p -r 5124
Where:
* -f: Set scheduling policy to SCHED_FIFO
* -o: Set scheduling policy to SCHED_FIFO
* -r: Set scheduling policy to SCHED_RR
* -p: Operate on an existing PID and do not launch a new tas

http://planet.admon.org/how-can-i-set-real-time-priority-for-a-process/