• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

alarm () and sleep() functions in the same process

Hi All,

I wanted to use alarm() function call in my process
I already have some calls to sleep() function.

I read the man pages for alarm() saying that  
alarm() and sleep() should not be used in the same process. (since sleep() may internally  use SIGALRM)

I wanted to know sugestions from the experts regd. this

What is the work around/design  if alarm() and sleep()
have to be used in the same process.

Is there any alternative to using alarm() function in the user space.

Thanks for all your help.

1 Solution
On Linux, sleep() does not use SIGALRM; there is no reason not to use it together with alarm().

On other systems, the way around this is to implement your own sleep by just scheduling an alarm and then waiting (e.g. pause()).  You have to coordinate this with your other use of the alarm signal -- essentially, you need to implement a schedule of events, where at any given time the alarm signal is scheduled to happen when the next event on the schedule is due.
Instead of using sleep() you could in a pinch also use select() to delay your program for a bit. But In my years, I found that I hardly have to use the alarm() function at all, by making sure the program executes a service routine function every now and then, that will check up on everything that needs to be done. :)
sanjay_thakurAuthor Commented:
Hi RanHoek,

I am not very clear how to use select()

Is this the same select() as with unix sockets.How is that
going to help.

could you please provide a simple example?considering I am
bit new to all this.

Thanks a lot both of you.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

It's the same select.  While its principal purpose is to wait for activity on file descriptors (sockets or other), it also has a timeout parameter.  If you just specify an empty set of file descriptors and specify a timeout, you have the same thing as usleep(), which is the same thing as sleep() but with microsecond precision.

Some people use select() in this abnormal way because it is available on more systems than usleep() and they need more precision than sleep().

Since I mentioned usleep(), I should also mention nanosleep(), which has even more precision than usleep() and has parameters that are sometimes easier to work with.
sanjay_thakurAuthor Commented:
I would like to split the answer points .

Thanks a lot for all the help.
Points reduced for a split.  You can now accept one experts comment as an answer.  After that, make another question in this topic area for the other expert.  Make the question for the amount of points intended.  Label the question, "Question for (Expert Name) and post this question number in the base of the question, i.e. For your assistance in question # --------

E-E Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now