gillesvds gillesvds - 1 year ago 125
C Question

why sched_setscheduler() does not work if not wrapped within an if block?

This following code works and requires "root" authentication:

struct sched_param param;
param.sched_priority = 99;
if (sched_setscheduler(0, SCHED_FIFO, & param) != 0) {

However, this one seems working (no error) but has no effect and does not requires "root" authentication:

struct sched_param param;
param.sched_priority = 99;
sched_setscheduler(0, SCHED_FIFO, & param);

Why ? I compile with gcc / Ubuntu 13.

Answer Source

Most likely sched_setscheduler didn't work in your second example. You just ignored the return value which probably wasn't 0.

Since you ignored the return value you do not actually know if it worked.

Looking at the man page for sched_setscheduler you will find this under RETURN VALUE

       On success, sched_setscheduler() returns zero.  
       On success, sched_getscheduler() returns the policy for the process (a nonnegative integer).
       On error, -1 is returned, and errno is set appropriately.

If -1 is returned errno is set and perror prints a human readable string for the error.

Since you said -1 was returned from the second example sched_setscheduler did not actually work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download