Thread: a timed terminal...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    23

    a timed terminal...

    hi, i wanted to emulate a terminal that asked for a user input, like login name, but doesn't wait for it indefinitely. a timer runs in the background and if no hit is received from the keyboard before the before the timer expires the program exits with default values. kinda like the grub timer.
    so i implemented this using both fork() and threads separately. is there any function that can get this done without any of these methods, i.e. is there any way for a single process with single thread to work this out??

    thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Have a look a non-blocking input. ncurses does it quite nicely, but if you're on a non-Unix system or don't want to use ncurses, there are other ways (albeit a bit more difficult).

    The idea behind non-blocking input is that you can check if the user has entered something. If they have, you can act on that input (say, one character at a time until you have a complete username). If they haven't, you can move on to something else (say, checking the timer to see if you should keep waiting). You can request input without losing control until the user presses enter.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use select(), with a timeout, to monitor the stdin file descriptor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    thanks a lot for your time.

    Have a look a non-blocking input. ncurses does it quite nicely, but if you're on a non-Unix system or don't want to use ncurses, there are other ways (albeit a bit more difficult).
    how to do this with curses? the functions that support non-blocking input are also available for everything else. and i don't recall any other function specific to curses.

    Use select(), with a timeout, to monitor the stdin file descriptor.
    did not think of that. thanks

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    how to do this with curses?
    What I was thinking of was setting cbreak(), nodelay()'ing the window, and then using getch(), which just returns an error if there's no input just yet.

    I actually like Salem's solution better (and I'm surprised I haven't read / thought about that one, as it's a lot more elegant than other solutions) - especially if you're not already using ncurses in your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can C work outside the terminal or CMD window?
    By ryantcb in forum C Programming
    Replies: 2
    Last Post: 07-22-2010, 01:11 PM
  2. My own terminal > Running a background process
    By tiestodj in forum C Programming
    Replies: 3
    Last Post: 03-06-2010, 03:30 AM
  3. Problem Clearing Terminal
    By mrginger in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 11:58 AM
  4. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  5. How do terminal emulator work?
    By Roaring_Tiger in forum C Programming
    Replies: 1
    Last Post: 04-15-2003, 08:20 PM