Need help exiting from a program.

This is a discussion on Need help exiting from a program. within the C Programming forums, part of the General Programming Boards category; Hi. I just finished writing a simple HTTP server for a class assignment (finished in the sense that I could ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    Need help exiting from a program.

    Hi. I just finished writing a simple HTTP server for a class assignment (finished in the sense that I could turn the code in and get an A for it). But there's a problem with it: so far, the only way to exit the server is to hit Control-C (fyi, I'm programming on a remote Sun unixbox). I'm fine with this, so long as I can find a way to run some closing lines of code when Control-C is entered, so the socket that the server is using is freed up when the program is exited.

    Another option I've looked into is listening for a specific keypress. I've done something similar with microprocessor coding using interrupts. I tried to implement this approach, but it didn't work; maybe I'm missing something. Once the key (eg. 'q') is pressed, the program would need to run the socket closing function, then return 0, regardless of what the server is doing.

    Any ideas in either direction would help a lot. Thanks!

  2. #2
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Quote Originally Posted by Grimloc
    Hi. I just finished writing a simple HTTP server for a class assignment (finished in the sense that I could turn the code in and get an A for it). But there's a problem with it: so far, the only way to exit the server is to hit Control-C (fyi, I'm programming on a remote Sun unixbox). I'm fine with this, so long as I can find a way to run some closing lines of code when Control-C is entered, so the socket that the server is using is freed up when the program is exited.
    This works atleast on linux i have no idea about "Sun unixbox" but...

    Control-C sends signal (SIGINT on linux) so we can create
    own signal handler and install it via signal (...)

    Here's sample.
    Code:
    void
    my_sighandler (int sig) {
        if (sig == SIGINT) {
            puts ("Interrupt recieved. Exiting...");
            exit (sig);
        }
    }
    
    int 
    main (void) {
        signal (SIGINT, my_sighandler);
        
        while (1) {
            sleep (1);
        }
        
        return 0;    
    }
    -- Add Your Signature Here --

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Ok I understand what he is saying here , but I have the same question.
    I have code as follows:
    Code:
    void main_function_process();
    
    bool check_for_key_press();
    
    int main()
    {	
    	// process functions to open program ie. connect to database
    	bool run = true;
    	while ( run = true)
    	{
    		main_function_process();
    		run = check_for_key_press();
    	}
    	// process functions to close program ie. disconnect from database
    };
    main_function_process()
    {
    	// Do all my program stuff here
    };
    bool check_for_key_press()
    {
    	if ( //is q held down)
    	{
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    };
    or do I need to run another thread. Ideally I would like to be able to type commands into the program while it is running, so it can do differnt things from terminal, but my program keeps taking over the terminal. even if I am not printing anything out. When in debug mode it does though.
    Been really racking my brain about this and would like some help if possible thanks.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Oh and the program can not wait for user input between loops. I only need it to shut down if it registors a key press and better yet gets the value of a char* using gets(char*). All though I pretty sure I would need a n other thread for the gets. But I would be happy with the key press for now.
    Thank you very much.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    sorry I guess I do not totaly understand the sig event. Looking it up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exiting program
    By ypramesh in forum C Programming
    Replies: 2
    Last Post: 04-01-2006, 03:27 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 08:51 PM
  4. Exiting a program
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-14-2004, 09:51 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21