1833747 Members
2831 Online
110063 Solutions
New Discussion

Re: Alarm Signal Issue

 
SOLVED
Go to solution
Alfonso_15
Advisor

Alarm Signal Issue

Good Morning:
I have a server program in cc. this Program is a socket listener. Under certain conditions I need to set the alarm (alarm(120)) in the loop in wich it is accepting conenctions. And when this signal (SIGALRM) arrives to my program, the handler signal function is executed, but the program cant not accept more connections in the listener socket. The accept function returns with error and the program crashes with errno 4.
Somebody have idea how sove this issue? Thanks a lot for your help.
alfonsoarias
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Alarm Signal Issue

If I understand your question, you are doing an alarm(120) before an accept(). I assume that if accept receives a connection you issue an alarm(0) to cancel the alarm. I strongly suspect that you have no SIGALRM signal handler. Your errno 4 (EINTR) make perfect sense; a system call was terminated when the process received a signal -- in your case the SIGALRM.

Normally, you would do something like a setjmp() and your signal handler would do a longjmp() causing setjmp() to return a non-zero value and thus branch to another section of code. I'd have to see you code to help any more.
If it ain't broke, I can fix that.
Mike Stroyan
Honored Contributor

Re: Alarm Signal Issue

It is clear that you do have a signal handler for SIGALRM since the default action without a handler would kill a process.

It is quite normal for a blocking call such as accept() to return EINTR. In general it is good practice to check for an EINTR errno and just loop back to making a call that you don't want to abandon because of a signal. You definitely should not start messing around with setjmp/longjmp for this simple activity.

You can also consider setting your signal handler to restart system calls. The sigaction() function has an SA_RESTART flag for that purpose. It will cause a call such as accept() to restart instead of returning with EINTR. You need to be careful that restarting calls will not break other assumptions that your code makes about what a signal can break out of. You might have some situations where you expect a SIGALARM to cause you to break out of a call such as select().
Ken_109
Advisor

Re: Alarm Signal Issue

A true server would not need to do this. So maybe rethinking your architecture would be a good idea. If the program also needs to do some background work, Just create a separate thread of execution and run that work in a seperate thread.Also creating a thread to perform work, that is read from the accept is general a common practice. Check out the stevens network programming books...