Can't get alarm to work...
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Can't get alarm to work...




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Can't get alarm to work...  
KraftDiner


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-06 12:55 PM

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....






[ Post a follow-up to this message ]



    Re: Can't get alarm to work...  
skylazart@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-06 12:55 PM

Your blockingFunction didnt ignore the sigalrm?

try to overwrite myBlockingFunction for while (1) { getchar (); } and
try again.






[ Post a follow-up to this message ]



    Re: Can't get alarm to work...  
KraftDiner


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-06 12:55 PM

my blocking function is actually a socket listen... Waiting for a
connection....






[ Post a follow-up to this message ]



    Re: Can't get alarm to work...  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-06 12:55 PM


"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







[ Post a follow-up to this message ]



    Re: Can't get alarm to work...  
Nils O. Selåsdal


Report This Message To A Moderator Edit/Delete Message


 
04-27-06 12:55 PM

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.





[ Post a follow-up to this message ]



    Re: Can't get alarm to work...  
KraftDiner


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-06 12:55 PM

Yes your are right the accept is blocking...






[ Post a follow-up to this message ]



    Re: Can't get alarm to work...  
KraftDiner


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-06 12:55 PM

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;
}






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:02 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register