participate


Reflections & Reference Objects - Ctrl C signal
<<   Back to Forum  |   Give us Feedback
This topic has 3 replies on 1 page.
khurramkhan1974
Posts:68
Registered: 4/15/04
Ctrl C signal   
Apr 19, 2004 10:32 PM

 
How can a program recognize a Ctrl C signal? What should it be doing to realize that a Ctrl C sginal has been sent? Should there be a separate thread blocked on reading an input from the keyboard and once the keys are pressed and released the thread quit reading and check if it is Ctrl C signal and if it is not then it should go back to reading again? What is a Ctrl C signal basically? Is it just text string? Or a bit pattern of some kind? Or what?

Any suggestions or advices??


Khurram
 
YATArchivist
Posts:4,906
Registered: 23/07/02
Re: Ctrl C signal   
Apr 20, 2004 7:17 AM (reply 1 of 3)  (In reply to original post )

 
To trap an OS signal you'll need to use JNI. Google around and you should find some suitable C code.
 
asjf
Posts:2,568
Registered: 6/7/02
Re: Ctrl C signal   
Apr 20, 2004 7:35 AM (reply 2 of 3)  (In reply to #1 )

 
a quick dirty way (ie relies on non-public API that might disappear/ won't be available on non-sun VM's) is

http://www.smotricz.com/kabutz/Issue043.html
 
edsonw
Posts:3,055
Registered: 18/06/98
Re: Ctrl C signal   
Apr 22, 2004 12:36 PM (reply 3 of 3)  (In reply to original post )

 
If you have a console program and want to use the Ctrl+C for closing program resources (like the notorious JBoss shutdown sequence), you must use a ShutdownHook:

public class TestCtrlC {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                System.out.println ("Called at shutdown.");
            }
        });
        System.out.println ("Hit Ctrl+C (not Ctrl+Break!)");
        System.out.println ("Trying to sleep 10 seconds...");
        try { Thread.sleep (10000); } catch (InterruptedException ex) {}
        System.out.println ("Sleep end");
    }
}


If you want to trap (handle) the Ctrl+C and other signals, use the undocumented, Sun JVM-specific, sun.misc.Signal and sun.misc.SignalHandler classes. You can get the full source in the SCSL Source Code.


package sun.misc;
/**
 * This class provides ANSI/ISO C signal support. A Java program can register
 * signal handlers for the current process. There are two restrictions:
 * <ul>
 * <li>
 * Java code cannot register a handler for signals that are already used
 * by the Java VM implementation. The <code>Signal.handle</code>
 * function raises an <code>IllegalArgumentException</code> if such an attempt
 * is made.
 * <li>
 * When <code>Signal.handle</code> is called, the VM internally registers a 
 * special C signal handler. There is no way to force the Java signal handler 
 * to run synchronously before the C signal handler returns. Instead, when the 
 * VM receives a signal, the special C signal handler creates a new thread
 * (at priority <code>Thread.MAX_PRIORITY</code>) to 
 * run the registered Java signal handler. The C signal handler immediately
 * returns. Note that because the Java signal handler runs in a newly created
 * thread, it may not actually be executed until some time after the C signal
 * handler returns.
 * </ul>
 * <p>
 * Signal objects are created based on their names. For example:
 * <blockquote><pre>
 * new Signal("INT");
 * </blockquote></pre>
 * constructs a signal object corresponding to <code>SIGINT</code>, which is
 * typically produced when the user presses <code>Ctrl-C</code> at the command line.
 * The <code>Signal</code> constructor throws <code>IllegalArgumentException</code>
 * when it is passed an unknown signal.
 * <p>
 * This is an example of how Java code handles <code>SIGINT</code>:
 * <blockquote><pre>
 * SignalHandler handler = new SignalHandler () {
 *     public void handle(Signal sig) {
 *       ... // handle SIGINT
 *     }
 * };
 * Signal.handle(new Signal("INT"), handler);
 * </blockquote></pre>
 *
 * @author   Sheng Liang
 * @author   Bill Shannon
 * @version  @(#)Signal.java	1.8 03/01/23
 * @see      sun.misc.SignalHandler
 * @since    JDK1.2
 */
public final class Signal {
    /* Returns the signal number */
    public int getNumber() {
    /**
     * Returns the signal name.
     * 
     * @return the name of the signal.
     * @see sun.misc.Signal#Signal(String name)
     */
    public String getName() {
    /**
     * Compares the equality of two <code>Signal</code> objects.
     *
     * @param other the object to compare with.
     * @return whether two <code>Signal</code> objects are equal.
     */
    public boolean equals(Object other) {
    /**
     * Returns a hashcode for this Signal.
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
    /**
     * Returns a string representation of this signal. For example, "SIGINT"
     * for an object constructed using <code>new Signal ("INT")</code>.
     *
     * @return a string representation of the signal
     */
    public String toString() {
    /**
     * Constructs a signal from its name.
     *
     * @param name the name of the signal.
     * @exception IllegalArgumentException unknown signal
     * @see sun.misc.Signal#getName()
     */
    public Signal(String name) {
    /**
     * Registers a signal handler.
     *
     * @param sig a signal
     * @param handler the handler to be registered with the given signal.
     * @result the old handler
     * @exception IllegalArgumentException the signal is in use by the VM
     * @see sun.misc.Signal#raise(Signal sig)
     * @see sun.misc.SignalHandler
     * @see sun.misc.SignalHandler#SIG_DFL
     * @see sun.misc.SignalHandler#SIG_IGN
     */
    public static synchronized SignalHandler handle(Signal sig, 
						    SignalHandler handler) 
    /**
     * Raises a signal in the current process.
     *
     * @param sig a signal
     * @see sun.misc.Signal#handle(Signal sig, SignalHandler handler)
     */
    public static void raise(Signal sig) throws IllegalArgumentException {
}
 
/**
 * This is the signal handler interface expected in <code>Signal.handle</code>.
 *
 * @author   Sheng Liang
 * @author   Bill Shannon
 * @version  @(#)SignalHandler.java	1.7 03/01/23
 * @see      sun.misc.Signal
 * @since    JDK1.2
 */
 
public interface SignalHandler {
 
    /**
     * The default signal handler
     */
    public static final SignalHandler SIG_DFL = new NativeSignalHandler(0);
    /**
     * Ignore the signal
     */
    public static final SignalHandler SIG_IGN = new NativeSignalHandler(1);
 
    /**
     * Handle the given signal
     *
     * @param sig a signal object
     */
    public void handle(Signal sig);
}
 
 
This topic has 3 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 59
  • Guests : 125

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums