Sleep command issue

Linux specific questions.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sleep command issue

Post by fxm »

On Linux text console, SLEEP cancelled by character already in keyboard buffer

Description:
When running on a Linux text console, "SLEEP" does not wait for a keypress if a character is already present in the keyboard buffer.

(only one proposal)
tom_dehn
Posts: 4
Joined: Apr 05, 2017 13:50

Re: Sleep command issue

Post by tom_dehn »

Hallo,

some general thoughts:

The issue's cause is likely some incompatibility of
some Linux implementations. The Code itself seems to
be correct (at least for some Linuxes). So we have a problem:

All Linuxes are equal, but some Linuxes are more equal than
the others.

SLEEP is used, I guess, primarily for granting
other processes more shared CPU time.

Waiting for a key is more more less a side effect.

Since this is a severe issue ;-), I tried to find out if there is an
API call for Linux. Didn't find a lot, most information was compiler specific,
and older than 5 years.
At last, Wikipedia told me (Bold/UL done by myself):
" On POSIX systems, the nanosleep and related syscalls are interruptible
by signals, returning the remaining sleep time. The sleep library
function, on the other hand, is implemented via the alarm syscall
on many older systems, thus it only works by delivering a signal. "

I don't know if this is of importance. Maybe KeyPressed() isn't enough SIGNAL
anymore, but CTRL-C still is ? I don't know...

Nevertheless: I never was happy with the "abuse" of sleep; it is OK in smaller
test programs or in demos, but, IMHO, not for "real programming".
Although it might return some result code - e.g., the remaining time -, you can't call
it as a function().
So I wrote some time ago the PAUSE function/statement.
It is an extension to the well known DOS/WIN Pause:

https://www.freebasic-portal.de/porticu ... -1815.html

PAUSE clears the KB buffer, and, in case of keypressed(), can return the KB scancode.
And it does not use the built-in SLEEP time delimiter, but keeps track of the system
timer. Written in 2015, one of my first codings in FB , so clap your hands and stomp
your feet :)

I just compiled and tried it a few minutes ago. For the first time with FBC 105/Linux/64,
and (once) again with FBC 105/Win/64. Tested both. It seems OK.

IMO the developping people might have to separate the "sharing" SLEEP from INKEY()/GETKEY(),
rename the (original all APIs conform, only -1- parameter) sleep() ) to something like

function cpuidle(timeout as single=0.232) as single

( 0 secs, 232 ms (the weird num is just for clarity, see PAUSE () )

and replace the code for the FreeBASIC SLEEP with a combination of IDLE() & INKEY()
So old code would not be broken, and newer code can be more precise.
Or, to avoid just another rarely used keyword (idle, not sleep!), forget IDLE()

Bye

TD
tom_dehn
Posts: 4
Joined: Apr 05, 2017 13:50

Re: Sleep command issue; correction of ERRor in my other post

Post by tom_dehn »

Sorry,

of course, SLEEP _can_ be a called as a function and _can_ return a result.
But: var x=sleep() (without params) causes a syntax error. My fault.

TD
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sleep command issue

Post by MrSwiss »

The simplest *workaround* solution is imho, a Macro:

Code: Select all

#Macro ClKbdBuff  ' clear keyboard buffer
    While InKey() <> "" : Wend
#EndMacro

?"one"
Sleep
ClKbdBuff
?"two"
Sleep
ClKbdBuff
?"three"
Sleep
ClKbdBuff
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Sleep command issue

Post by lizard »

How about this?

Code: Select all

#Macro pause  ' wait and clear keyboard buffer
    Sleep
    While InKey() <> "" : Wend
#EndMacro

?"one"
pause
?"two"
pause
?"three"
pause
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sleep command issue

Post by fxm »

Rather:

Code: Select all

#Macro pause  ' clear keyboard  then wait
    While InKey() <> "" : Wend
    Sleep
#EndMacro

?"one"
pause
?"two"
pause
?"three"
pause
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Sleep command issue

Post by lizard »

That was my first version, but i changed it after MrSwiss.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sleep command issue

Post by fxm »

So we both agree that this last definition of the 'pause' macro:

Code: Select all

#Macro pause  ' clear keyboard  then wait
    While InKey() <> "" : Wend
    Sleep
#EndMacro
is the good workaround of the defect that I explained as follows:
fxm wrote:On Linux text console, SLEEP cancelled by character already in keyboard buffer

Description:
When running on a Linux text console, "SLEEP" does not wait for a keypress if a character is already present in the keyboard buffer.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sleep command issue

Post by MrSwiss »

fxm wrote:So we both agree that this last definition of the 'pause' macro
While you can do, what you want ... (I personally don't agree, with your *description* either!)

Description: In Linux Terminal, 'sleep' doesn't *reliably* clear the keyboard-buffer.

I'd go with lizard's version, since "Sleep() doesn't *reliably* clear the keyboard-buffer".

Code: Select all

' LinuxPause_Mac.bas -- 2018-04-10, MrSwiss
'
' compile: -s console
'
#Macro Pause    ' wait, then clear keyboard buffer (Kbd-Buff)
    Sleep       ' not always, clearing Kbd-Buff (Linux only!)
    While InKey() <> "" : Wend  ' forced clear Kbd-Buff
#EndMacro

Print "one"
Pause
Print "two"
Pause
Print "three"
Pause
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sleep command issue

Post by fxm »

See SLEEP documentation page:
.....
Sleep does not clear the keyboard buffer and any keys pressed during a call to Sleep are retained and can be read using Inkey.
.....
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sleep command issue

Post by MrSwiss »

You don't seem to realize, that this is a "Linux Console/Terminal" issue only! (even different, depending on Distribution!)
Since, half the problem is solved already with: ScreenRes() (some Distributions).

On Windows, sleep works as expected.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sleep command issue

Post by fxm »

I know all that well.
The last discussion (above) focuses only on the best solution to work around this specific problem on linux text console (when exists).
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Sleep command issue

Post by lizard »

fxm wrote:So we both agree
Yes, now it is crystal-clear that the keyboardbuffer must be cleared before sleep. Because the user could press other keys in the time between two pauses.

It would be easy to clear the buffer before and after sleep because it is not much work.

Code: Select all

#Macro pause  ' clear keyboard  then wait
    While InKey() <> "" : Wend
    Sleep
    While InKey() <> "" : Wend
#EndMacro
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Sleep command issue

Post by jj2007 »

What's wrong with GetKey, too simple...? It seems to work on all platforms, right?
jj2007 wrote:Sleep waits on Windows for FB32 and FB64. Same behaviour for Getkey:

Code: Select all

?"one"
getkey
?"two"
getkey
?"three"
getkey
?"four"
getkey
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Sleep command issue

Post by lizard »

Right.
Post Reply