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

Posted on 2003-01-26
Medium Priority
Last Modified: 2012-05-04
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.

Question by:sanjay_thakur
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7827164
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.

Expert Comment

ID: 7837469
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. :)

Author Comment

ID: 7927109
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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7939146
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.

Author Comment

ID: 8021718
I would like to split the answer points .

Thanks a lot for all the help.

Accepted Solution

Computer101 earned 75 total points
ID: 8022564
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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question