[PLUG] Linux process scheduling

Terry Griffin griffint at pobox.com
Wed Feb 15 18:15:30 UTC 2006


Larry Brigman inquired:
> I have a problem that I have spent at least two days searching on google
> and
> not found a solution.
>
> Problem background.
>
> I want to have a process wake up every x time period and send a network
> packet.
> Doing this is not hard but what seems hard to me is that I need this x
> period to be
> close to the same every time.  Minor variations are allowed and can be
> compensated
> in the program.  Basically keep a constant packet rate, whatever rate
> is required
>
> When using select or nanosleep (stock kernel 2.6.15-1.1830_FC4smp), I get
> wild
> variations of output.  Example: request sleep for 2.4 msec. get
> 7.9msec.;  then have
> to send three packets to make up for the time the process did not send
> because it
> was sleeping.
>
> The original program that I was using is iperf but when I want to use
> two different rates
> using two copies of the program running at the same time they conflict
> as the program
> is using a spin loop to get the timing correct.  I replace this spin
> loop with nanosleep
> and the program output is no longer constant.
>
> The really strange thing is when I increase the packet rate (decrease
> the sleep period)
> the programs using nanosleep or select don't get as much error ( 1
> msec. request get
> 980 usec. to 1020 usec.).
>
> The programs use posix threads (which is NPTL) and don't do any
> special calls for
> scheduling or priority settings.
>

Try:
  pthread_attr_init()
  pthread_attr_setschedpolicy() with a policy of SCHED_RR.
  pthread_create() using the attribute initialized above.

The process will have to be started as root for this to work,
but after the new thread is running you may setuid() to a
not-root user and retain the SCHED_RR policy.

Man pages: pthread_attr_init(3), pthread_create(3), and setuid(2).

Terry






More information about the PLUG mailing list