Thread: what if i want to run kbhit in background process like this, why not work proprely

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    what if i want to run kbhit in background process like this, why not work proprely

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int kbhit(void)
    {
    	struct termios oldt, newt;
    	int ch;
    	int oldf;
    
    	tcgetattr(STDIN_FILENO, &oldt);
    	newt = oldt;
    	newt.c_lflag &= ~(ICANON | ECHO);
    	tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    	oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    	fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
    
    	ch = getchar();
    
    	tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    	fcntl(STDIN_FILENO, F_SETFL, oldf);
    
    	if(ch != EOF)
    	{
    		ungetc(ch, stdin);
    		return 1;
    	}
    
    	return 0;
    }
    
    
    main( int argc, char** argv )
    {
    	char *input = argv[0];
    	int nomor = argc;
    	pid_t pid = 0;
    	/* set stuff up */
    	/* accept command line args */
    	
    	pid = fork();
    	if( pid == 0 ) {
    		/* this is the "background" process.  Execute process loop here */
    		while(1)
    		{
    			if(kbhit())
    			{
    			}	
    		}
    	}
    	else {
    		/* "foreground" process exits */
    		exit(0);
    	}
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Can you describe "doesn't work properly" a bit more in detail (particularly the difference between what you see happening and what actually happens).

    2. This is an infinite loop:
    Code:
    		while(1)
    		{
    			if(kbhit())
    			{
    			}	
    		}
    as there is nothing that actually makes the loop exit. Is this perhaps the cause of "not working".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    i want to make a daemon process that could stop if user hit the keyboard, but the kbhit routine doesnt work when it is in the daemon process, would you help please?

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Code:
     while(1)
    		{
    			if(kbhit())
    			{
                                  exit(0);
    			}	
    		}
    and it is could not stop the child process, how do i make the child process exit ??

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Please do not bump old threads. I have split the new posts to a new thread.

    In addition do not abuse the report to moderator function to ask for someone's instant messenger contact. For one thing it isn't even going to reach a non moderator and for another it annoys the heck out of the moderators.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by wiraredi View Post
    Code:
     while(1)
    		{
    			if(kbhit())
    			{
                                  exit(0);
    			}	
    		}
    and it is could not stop the child process, how do i make the child process exit ??
    That should stop the child process. However, if you already exited out of the parent process, the parent process has given the stdin handles back to it's parent process (your shell, most likely), and the shell process may "get there first" with regards to "eating the input". Maybe you should put a sleep(10) in your first process instead of the exit(0)?

    And if it was my personal details you were asking for, please forget it, as I don't do "personal" support.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So is it a background process or a daemon process you're trying to do this with?
    Because they're different things.
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    So is it a background process or a daemon process you're trying to do this with?
    Because they're different things.
    But neither will have direct access to stdin if another process is in the foreground and also using stdin.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Code:
    while(1)
    		{
    			sleep(10);
    			if(kbhit())
    			  printf("you press keyboard");
    		}
    i try to know wheter the kbhit() working or not by do that, but after i compile it then i type something in command line the program doesnt print "you press keyboard" what happen actually?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by wiraredi View Post
    Code:
    while(1)
    		{
    			sleep(10);
    			if(kbhit())
    			  printf("you press keyboard");
    		}
    i try to know wheter the kbhit() working or not by do that, but after i compile it then i type something in command line the program doesnt print "you press keyboard" what happen actually?
    I actually meant that you should put the sleep in the OHTER side of the fork.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. background process switches to stopped
    By bonkey in forum Linux Programming
    Replies: 3
    Last Post: 11-20-2002, 08:27 AM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  3. Replies: 2
    Last Post: 10-29-2002, 04:56 PM
  4. Run in background
    By smog890 in forum C Programming
    Replies: 4
    Last Post: 06-09-2002, 02:22 PM
  5. run a program in background
    By Dream in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 05:57 AM