|
Home > Archive > Unix Programming > April 2006 > Can't get alarm to work...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Can't get alarm to work...
|
|
| KraftDiner 2006-04-27, 7:55 am |
| I'm trying to do some background processing while my program is blocked
waiting for a connection...
Every 30 seconds I want to print out a message... blip... whatever...
I'm on MAC OS X 10.4.6 using g++
my code: More or less...
void catch_alarm(int sig_num)
{
printf("blip...\n\n");
signal(SIGALRM, catch_alarm); //reset the signal handler.
}
main()
{
/* set a signal handler for ALRM signals */
signal(SIGALRM, catch_alarm);
alarm(30);
/* call a function that could block forever */
myBlockingFunction();
}
However the catch_alarm function never seems to be called....
| |
| skylazart@gmail.com 2006-04-27, 7:55 am |
| Your blockingFunction didnt ignore the sigalrm?
try to overwrite myBlockingFunction for while (1) { getchar (); } and
try again.
| |
| KraftDiner 2006-04-27, 7:55 am |
| my blocking function is actually a socket listen... Waiting for a
connection....
| |
| Fletcher Glenn 2006-04-27, 7:55 am |
|
"KraftDiner" <bobrien18@yahoo.com> wrote in message
news:1144891773.066138.75160@e56g2000cwe.googlegroups.com...
> my blocking function is actually a socket listen... Waiting for a
> connection....
>
Last time I used it listen() did not block. So the question is: What is
really blocking? accept()?
--
Fletcher Glenn
| |
| Nils O. Selåsdal 2006-04-27, 7:55 am |
| KraftDiner wrote:
> I'm trying to do some background processing while my program is blocked
> waiting for a connection...
> Every 30 seconds I want to print out a message... blip... whatever...
> I'm on MAC OS X 10.4.6 using g++
>
> my code: More or less...
>
> void catch_alarm(int sig_num)
> {
> printf("blip...\n\n");
> signal(SIGALRM, catch_alarm); //reset the signal handler.
> }
>
> main()
> {
> /* set a signal handler for ALRM signals */
> signal(SIGALRM, catch_alarm);
> alarm(30);
> /* call a function that could block forever */
> myBlockingFunction();
> }
>
> However the catch_alarm function never seems to be called....
If you try this standalone example, any difference ?
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
void catch_alarm(int sig_num)
{
char *str = "blip...\n\n";
write(1,str,strlen(str));
signal(SIGALRM, catch_alarm); //reset the signal handler.
}
int main(void)
{
int c;
/* set a signal handler for ALRM signals */
signal(SIGALRM, catch_alarm);
alarm(30);
/* call a function that could block forever */
while((c = getchar()) != EOF && c != 'q')
;
return 0;
}
Should give you one 'blip...' , note - don't use printf in a signal
handler. printf is not async signal safe. write(2) is, afaik.
| |
| KraftDiner 2006-04-27, 7:55 am |
| Yes your are right the accept is blocking...
| |
| KraftDiner 2006-04-27, 7:55 am |
| Yes it is accept that is blocking and will not unblock...
Maybe there is a sock option I can set so that accept will time out...
#include <iostream>
#include <stdio.h> /* standard I/O functions
*/
#include <unistd.h> /* standard unix functions, like alarm()
*/
#include <signal.h> /* signal name macros, and the signal()
prototype */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <strings.h>
#include <netdb.h>
int m_nSocketFD;
int m_nPortNo;
struct sockaddr_in serv_addr;
void Open(int nPort)
{
m_nPortNo = nPort;
m_nSocketFD = (int)socket(AF_INET, SOCK_STREAM, 0);
if (m_nSocketFD < 0)
{
throw("ERROR opening socket");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(m_nPortNo);
int x = 1;
if (setsockopt(m_nSocketFD, SOL_SOCKET, SO_REUSEADDR, (char *)&x,
sizeof x) == -1)
throw("setsockopt SO_REUSEADDR failed");
if (bind(m_nSocketFD, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
{
throw("ERROR on binding");
}
}
int Listen(void)
{
listen(m_nSocketFD, 5);
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
int newsockfd = (int)accept(m_nSocketFD, (struct sockaddr *)
&cli_addr, &clilen);
if (newsockfd < 0)
{
throw("ERROR on accept");
}
return(newsockfd);
}
/* define an alarm signal handler. */
void catch_alarm(int sig_num)
{
printf("Operation timed out. Exiting...\n\n");
exit(0);
}
int main(int argc, char* argv[])
{
try
{
/* set a signal handler for ALRM signals */
signal(SIGALRM, catch_alarm);
/* prompt the user for input */
printf("Listening!");
fflush(stdout);
/* start a 30 seconds alarm */
alarm(30);
/* wait for user input */
Open(4096);
int resp=Listen();
/* remove the timer, now that we've got the user's input */
alarm(0);
}
catch(const char *msg)
{
printf("%s\n", msg);
}
return 0;
}
|
|
|
|
|