Consultor Eletrônico



Kbase 18904: Program to Determine What Signals are Received By a Process
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   8/7/2001
SUMMARY:

sigtst - records signals sent to a process.

EXPLANATION:

"sigtst" is a program, independent of Progress, to determine what
signals are received by a process. This program can be useful
in situations where there are reports that closing a window or
turning off a terminal are causing the Progress broker to shut
down, when this kind of activity should be sending a hangup
signal to Progress. With the use of "sigtst" we can find out
what signal is really being sent when the window is closed
or the terminal is shut off.

SOLUTION:

To compile, save this text to a file called "sigtst.c".
Then, do the following:

cc sigtst.c -o sigtst

This will create an executable file called "sigtst".

Type in "sigtst" to run the program. When sigtst is run, it
returns its process id to the screen. Signals received by that
process are recorded in a file called "sigtst.out".

Run sigtst, then shut off the terminal it was being run from.
Look in the sigtst.out file to see what signal was sent. Was
it a hangup signal (signal number 1) or some other signal? To
test other signals you can issue a kill command to the process
id listed and the signal will be recorded in sigtst.out. A
kill -9 will terminate the program.

/***************************************************************/

#include <fcntl.h>
#include <errno.h>

/* program that prints out signals that is receives. This will
loop forever until a kill -9 is sent to it */

void sighdl(); /* signal handler */
int fd = 0; /* global file descriptor */

main()
{

int i;

/* open the output file */
fd = open("sigtst.out",O_RDWR | O_CREAT, 0666);
if (fd < 0)
{
printf("Unable too open sigtst.out, errno = %d\n",
errno);
exit(1);
}

/* give us a process number to signal */
printf("Process id is %d\n",getpid());

/* set up handlers for all signals */
for (i=1; i<32; i++)
{
signal(i,sighdl);
}

/* loop until kill -9 */
while (1)
sleep(1);

}

void
sighdl(sigcode)
int sigcode;
{
char buf[80]; /* temp buffer to write output */

if (fd <= 0)
{
printf("output file not open, using stdout\n");
printf("Received signal %d\n",sigcode);
return;
}

/* clear the buffer */
memset(buf,'\0',80);

/* setup the buffer */
sprintf(buf,"Received signal %d\n",sigcode);

write(fd,buf,sizeof(buf));
return;
}