Sleep command issue
Re: Sleep command issue
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)
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)
Re: Sleep command issue
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
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
Re: Sleep command issue; correction of ERRor in my other post
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
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
Re: Sleep command issue
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
Re: Sleep command issue
How about this?
Code: Select all
#Macro pause ' wait and clear keyboard buffer
Sleep
While InKey() <> "" : Wend
#EndMacro
?"one"
pause
?"two"
pause
?"three"
pause
Re: Sleep command issue
Rather:
Code: Select all
#Macro pause ' clear keyboard then wait
While InKey() <> "" : Wend
Sleep
#EndMacro
?"one"
pause
?"two"
pause
?"three"
pause
Re: Sleep command issue
That was my first version, but i changed it after MrSwiss.
Re: Sleep command issue
So we both agree that this last definition of the 'pause' macro:
is the good workaround of the defect that I explained as follows:
Code: Select all
#Macro pause ' clear keyboard then wait
While InKey() <> "" : Wend
Sleep
#EndMacro
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.
Re: Sleep command issue
While you can do, what you want ... (I personally don't agree, with your *description* either!)fxm wrote:So we both agree that this last definition of the 'pause' macro
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
Re: Sleep command issue
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.
.....
Re: Sleep command issue
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.
Since, half the problem is solved already with: ScreenRes() (some Distributions).
On Windows, sleep works as expected.
Re: Sleep command issue
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).
The last discussion (above) focuses only on the best solution to work around this specific problem on linux text console (when exists).
Re: Sleep command issue
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.fxm wrote:So we both agree
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
Re: Sleep command issue
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
Re: Sleep command issue
Right.