[alsa-devel] usleep() and nanosleep() timings seem inaccurate using ALSA

Takashi Iwai tiwai at suse.de
Thu Mar 29 12:42:14 CEST 2007

At Thu, 29 Mar 2007 17:46:38 +0800,
Carlo Florendo wrote:
> Good Day!
> After studying the intricacies of MIDI, I ended up writing an 
> implementation of the MIDI protocol and file format.
> I then studied the ALSA sequencer API to be able to control a synthesizer 
> keyboard and play MIDI files.  I've used aplaymidi, aconnect, arecordmidi, 
> and all those great ALSA utilities.  They're very good!
> However, I wanted to have a simple ncurses based, command line MIDI 
> sequencer meant for small Linux distributions such as DSL or Trustix so I 
> began to write a command line sequencer using ALSA.  I've encountered one 
> problem about using usleep() and nanosleep() especially in 2.4 kernels.

You cannot get a small sleep usually on user-space processes.
Usually, usleep() is implemented with select/poll and its timeslice is
defined by HZ in kernel config.  In most cases, it's HZ=100, 250 or
1000 while 2.4-i386 kernel supports only HZ=100.  That is, the least
sleep time is 10ms no matter what value you pass to usleep().

This can be overcome by using a realtime schedule class and priority
like JACK does.


More information about the Alsa-devel mailing list