Thread: Listening to stdin while performing certain operations

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    Listening to stdin while performing certain operations

    Hi!

    I'm trying to write a program, based on following ideas:
    - while not interrupted, program is performing some operations, then goes in sleep/wait state for a minute, then again - operations, and sleep/wait state...
    - if interrupted (user inserts a command), program handles the command and then goes back to his work/sleep loop.

    My question is if there is a way to make my program sleep, but at the same time monitor standard input, to handle the command immediately if given. I would rather not create two thread application.

    Big thanks in advance!

    [edit]
    Ah, of course: I'm looking rather for a C++ solution.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Windows or Linux?
    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Linux.

    Thanks for moving thread to the correct board.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    select(2): synchronous I/O multiplexing - Linux man page
    - create an fd set for stdin
    - create a timeout for 1 minute

    Look at the return result to decide whether it was a timeout of some input.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Big thanks, Salem.

    However, I must admit, I lied a bit... I said I need linux command, because I'm compiling under MinGW, and I hoped the answer would be proper also for this. Unfortunately it seems that select() is one of POSIX functions, which MinGW doesn't support (damn, damn, damn...).

    So I need to apologize and re-ask: could you help me find select() equivalent which would work under MinGW?

    <edit>
    I found something... I think.

    It is a Winsock2.h select() function, which is similar to the Unix one.
    (need to add -lws2_32 switch to make it link under MinGW)

    The code I've written has one serious bug: the program doesn't go to sleep. I don't know if it is because I passed the descriptor/time in the wrong way, but it keeps looping just as if the select function didn't work.

    Could you help me with that?
    Code:
    #include <iostream>
    #include <Winsock2.h>
    using namespace std;
    int main(){		
    			/*
    			this, I believe is creating a fd_set structure with one element pointing at Standard Input 
    			std::cin should have 0 as his descriptor
    			*/
    			fd_set set = {1, {0}};//
    		
    		/*
    		I'm not sure if both values should be equal or not, but it is a positive number
    		*/
    			timeval time = {5000, 5000000};
    
    			while (1){
    			
    			cout<<"I start looping!!"<<endl;
    			
    				int i = select (0, &set, NULL, NULL, &time); //Now the process *should* go to sleep for 5 seconds or more.
    				
    				if (i == 0) cout <<"It's timeout!"<<endl; //if i!=, it means that the input woke the process, not timeout
    			
    			}
    }
    Last edited by Horsey; 09-17-2010 at 11:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DWARF-2 expression error
    By programhelp in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2010, 06:18 PM
  2. Performing File operations using File Inode number
    By rak1986 in forum C Programming
    Replies: 4
    Last Post: 09-22-2008, 02:43 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  5. Replies: 3
    Last Post: 03-23-2002, 04:20 PM