The SIGINT processing scheme for Linux scripts and programs. The script passes signals to the program through kill.

Source: Internet
Author: User
Tags sigint signal
In the performance test, the tester program is started by the start. Sh script. Now, the user needs to press Ctrl-C to exit the program graceful without restarting the board. The problem is: Ctrl-C generates a signal SIGINT, but this signal is passed to the script process, not to the tester process. Therefore, there is a problem of signal transmission. The solution is as follows:

1. Add code to the script:

Code: select all
forward_sigint()
{
    # check out the tester's pid
    testerpid=$(cat /tmp/tester.pid)
    kill -2 $testerpid

    # call analyser and quit
    echo "Calling analyser..."
    ./Analyser/analyser

    echo "Test finished. Thanks."
    rm -f /tmp/tester.pid

    exit 0
}
   
......

# trap control-c signal and forwards it to tester
trap forward_sigint SIGINT

After pressing Ctrl-C, the forward_sigint function is executed. Then, this function extracts the PID of the tester program and uses the kill command to send the SIGINT signal to the tester so that the graceful program exits. Note that the kill command here is a built-in shell command, not a/bin/kill program. You can also use the/bin/kill program to send signals, with the same effect.

2. Add code to the tester program:

First, add the following in the main function:

Code: select all
// Catch SIGINT interrupt
Signal (SIGINT, stop_playing );

// Write PID into/tmp/tester to make test script knows our PID
Write_pid_to_temp_file ();

Then the implementation of these two functions:

/*
* Write our PID to file/tmp/tester. PID
* This makes start script knows our PID and send SIGINT to us
* When the user pressed Ctrl-C
*/
Void write_pid_to_temp_file ()
{
File * pidfile = fopen ("/tmp/tester. PID", "WB ");
Char content [10];
Sprintf (content, "% d", getpid ());
Fputs (content, pidfile );
Fclose (pidfile );
}

/*
* Stop playing and quit
*/
Void stop_playing (INT sig)
{
Debug_output ("Ctrl-C caught, stop playing and quit ...");
// Restore the SIGINT Handler
Signal (SIGINT, sig_dfl );

// Make sure the glib mainloop is running
While (! G_main_loop_is_running (loop ))
Wait_seconds (1 );

    g_main_loop_quit(loop);
}

OK. The key point is that when a program is started through a script, the signal is sent to the script process, not to the program process being executed. Therefore, the signal must be transmitted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.