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.