POSIX Timer : Signal Handler for Timer freezes?

The POSIX spec tells you exactly what API functions you may safely call from a signal handler (there is a list near the end of section 2.4.3).

The POSIX spec tells you exactly what API functions you may safely call from a signal handler (there is a list near the end of section 2.4.3). Socket, connect, recv, and close are all on the list, so they should be safe. Printf and fprintf are not, which is not surprising since they manage a user-space buffer, which is hard to keep consistent in the presence of async signals.So other than the printf type calls, this code actually looks OK to me.

When your "code freezes", have you tried attaching with a debugger to see where you are stuck? That said, the usual way to handle this is one of two ways. First way: If your application has an "event loop", arrange for the signal simply to send an event.

Then your main thread handles that event just like any other, and everything stays nice and synchronous. Second way: Devote a thread to the periodic task. The thread can just loop calling sleep(1) and performing your periodic task.

But I still think your approach here should be working.

Thanks Nemo :) The first thing I tried now was to comment out printf and tested about 12-13 times and till now this freezing didn't pop up. (As a second measure, I also tried to put two signal handler routines to separate SIG and SIGTIMER and also commented printf here). I like the thread idea for the periodic task.

But don't want to have sleep called in it. Can I implement it any other way using a spearate thread? As in call a timer for that thread or something?

– user489152 Jun 22 at 14:02 Why do you not want to call sleep() in the thread? If you are worried that sleep() itself might set a timer, you can just use select() (on zero descriptors) with a timeout. This should be very efficient; select() will block, the kernel will put the thread to sleep and it will consume zero resources until the kernel decides to wake it up... – Nemo Jun 22 at 14:21 oh ok.

I will try that. Actually now my code again freezes(after removing the printfs :(...) what should I do? – user489152 Jun 22 at 14:28 Attach with gdb and look at the backtrace.

– Nemo Jun 22 at 14:31 1 select takes three sets of file descriptors and a timeout. If the sets are empty, you can use the timeout alone to perform a high-resolution "sleep". Or if you only care about modern POSIX systems, just use nanosleep, which is guaranteed to have "no effect on the action or blockage of any signal." – Nemo Jun 22 at 15:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions