What exactly does alarm() and sleep() do? - c++

If my timeslice is 3 seconds, I am guessing the alarm stops the execution of a process every three seconds. What does sleep do? Does it put the process to sleep for 3 seconds? This does not make sense to me - what if there are a lot of processes? Wouldn't it have to sleep for longer?
I am doing this with the round robin stimulation:
while (head!=NULL)
{
alarm(TIMESLICE);
sleep(TIMESLICE);
}
cout<<"no processes left"<<endl;
The code works, but I just want to understand what exactly is going on as I am new to this concept.

I am guessing the alarm stops the execution of a process every three seconds.
Sort of. It arranges for a signal to be sent to the process in three seconds. The process can then continue normally and can even ignore the signal if it wants to.
What does sleep do? Does it put the process to sleep for 3 seconds?
Correct.
This does not make sense to me - what if there are a lot of processes? Wouldn't it have to sleep for longer?
No. Even a process that never sleeps isn't guaranteed to get the CPU all the time. A process that isn't sleeping may or may not be scheduled to run on a core at any particular time. Once it's no longer sleeping, it's ready-to-run, and the scheduler will make the decision of when and for how to long to let it use what core.

Related

Worker and interrupter thread coordination in Boost

Kind of C++ and even more Boost noobie here.
I have successfully managed to create two threads, based on this example, one Worker and one Interrupter. The latter sleeps, via this_thread::sleep_for for 50 seconds and interrupts the Worker if he is not done within that time, with proper use of while !this_thread::interruption_requested() and thread::interrupt(). So it's a very nice and easy timeout mechanism. My problem is how to stop Interrupter sleep sooner? I mean if Worker finishes before that 50 seconds, I do a this_thread::yield or this_thread::interruption_requested (both seem to work) but Interrupter is still asleep and have to wait for 50 seconds for him to wake up:(
Is there any way to do this timeout mechanism, but if Worker is done with his work before sleep is over, to notify/wake up Interrupter?
PS: Do I need some sort of synchronization when all are done?
Your interrupter thread should not sleep unconditionally for 50 seconds but wait for conditional variable for 50 seconds, if worker thread finishes earlier it will signal conditional var and interrupter would wake up. You can use either std::conditional_variable or one from boost with the same name

c++ alert timer with little cpu load

I want to write a small alert timer on windows using c++ and msvc2010. The timer needs to trigger a status message after a couple of minutes. I know how to check the system time using c++ and I know there is sleep function in windows api. How can I implement a timer with very little cpu load? For example, I don't want to check the system time every couple of milliseconds to trigger the status message when the trigger time is reached. Do I create cpu load, when using things like sleep(600000) in an extra thread or are there more efficient ways to wait a couple of minutes and execute some code afterwards?
You can indeed busy-wait and poll the time. Even a Sleep(1) will be enough that your program will be barely measurable.
I used to do it "back in the day" and even on my PII 233 Mhz running multiple threads doing this it barely made a dent in the CPU usage.
You could create a thread, write a continuous loop inside which you just sleep for the time interval that your trigger needs to run at then print your message. If you need to run it at 2 minutes, why choose multiple small sleep values and check the time? That would be a waste of CPU time.

thread didnt awake from sleep

I have a multi-threaded application but sometimes one thread that is suppose to wake up after 10 seconds and do some work is not getting woken up from sleep or is starving.
It only happens sporadically.
//ACE task svc method
int svc (void)
{
while(true)
{
ACE_DEBUG((MY_INFO "sleep\n"));
sleep(10);
ACE_DEBUG((MY_INFO "awake for HB\n"));
_csender.sendHeartBeat();
}
return 0;
}
The last line in the log is:
2012-06-12 11:34:20.807272|INFO|sleep
Thread either didnt awake for 15 seconds or didn't do any work after its awaken until the 15th second, so the application closed.
There are total 6 threads in the application, all started with same priority.
One of the thread is very busy, it receives a lot of market data and processes it, but does not send anything out on socket. The thread above is the only thread sending data out and both the receiver and sender threads are sharing the same socket object.
This is on red hat linux 5.3.
any ideas what could be the issue?
'Busy thread is prints 2 ACE_DEBUGs every 2 microseconds' - so probably clogging up the output queue of the debugger and preventing this thread from getting in to queue up its 'sleep\n'.
That, and/or you have prioritized down this thread and it cannot get a core, as other posters have indicated.
The sleep(10) is almost an irellevance.
Sleep does not give any guarantees about maximum time spent sleeping, i.e. you sleep for at least that amount of time, or any time longer. I don't know if it's plausible to expect a 5 second delay though, sounds far too long.
I think its in ACE_DEBUG. Did you try a printf with a flush right after the sleep?

C++: More precise timing intervals

I need to periodically do a particular task and am currently using nanosleep.
The task needs to be run every second or every 10 seconds.
Is there a better way to do this than:
while(true)
{
doTask();
sleep();
}
Walter
One of the options could be to create a thread that will do the task with specified timeout.
You can use a thread library to create a thread which handle run the doTask(). Your main thread just keeps sleeping and runs every 1 second or 10 seconds.
This can be done with a QTimer and a QRunnable.
http://doc.qt.nokia.com/latest/qtimer.html
According to the dock, the resolution is around 1 ms in most cases. For your need, this should be sufficient.

Concurrency question about program running in OS

Here is what I know about concurrency in OS.
In order to run multi-task in an OS, the CPU will allocate a time slot to each task. When doing task A, other task will "sleep" and so on.
Here is my question:
I have a timer program that count for inactivity of keyboard / mouse. If inactivity continues within 15min, a screen saver program will popup.
If the concurrency theory is as I stated above, then the timer will be inaccurate? Because each program running in OS will have some time "sleep", then the timer program also have chance "sleeping", but in the real world the time is not stop.
You would use services from the OS to provide a timer you would not try to implement yourself. If code had to run simple to count time we would still be in the dark ages as far as computing is concerned.
In most operating systems, your task will not only be put to sleep when its time slice has been used but also while it is waiting for I/O (which is much more common for most programs).
Like AnthonyWJones said, use the operating system's concept of the current time.
The OS kernel's time slices are much too short to introduce any noticeable inaccuracy for a screen saver.
I think your waiting process can be very simple:
activityTime = time of last last keypress or mouse movement [from OS]
now = current time [from OS]
If now >= 15 mins after activityTime, start screensaver
sleep for a few seconds and return to step 1
Because steps 1 and 2 use the OS and not some kind of running counter, you don't care if you get interrupted anytime during this activity.
This could be language-dependent. In Java, it's not a problem. I suspect that all languages will "do the right thing" here. That's with the caveat that such timers are not extremely accurate anyway, and that usually you can only expect that your timer will sleep at least as long as you specify, but might sleep longer. That is, it might not be the active thread when the time runs out, and would therefore resume processing a little later.
See for example http://www.opengroup.org/onlinepubs/000095399/functions/sleep.html
The suspension time may be longer than requested due to the scheduling of other activity by the system.
The time you specify in sleep() is in realtime, not the cpu time your process uses. (As the CPU time is approximately 0 while your program sleeps.)