Regarding Sleep() function in c
Hi there
I know what is the functionality of the sleep() method. for(i = 0; i < length; i++){ sleep(1000); printf("%c\n", message[i]); } Result :every print of the message will be takes 1000sec of gap but i want to know why wee need to use the Sleep function. where exactly it is useful Thanking you reddy |
One use: (In a layman's language) sleep() can be used to synchronize make threads wait, i.e. when you want one thread to pause till another one completes its task.
You might like to read about the terms: sleep wait pthread |
Quote:
Code:
#define _BSD_SOURCE If you replaced the sleep(5) with a loop that waits until the system timer has progressed five seconds, the program would work the same way from the user's perspective -- but it would waste massive amounts of CPU processing time for nothing. It would be extremely wasteful. (That kind of loop would be called spinning or busy-waiting, and should always be avoided.) The sleep(5), on the other hand, tells the kernel that this process (or thread) does not have any work to do for the next five seconds (unless something interesting happens, like a signal is delivered), so the CPU can be given to other processes. If the load is low enough, the kernel can decide to put the CPU to a lower power state, saving quite a bit of energy. Hope this helps. |
Quote:
|
Quote:
pthread_join can be used instead of sleep by the way. and secondly I never claimed that sleep is the most reliable way. If you think tutorial is wrong in using it, then do bother to enlighten me please. |
The resource you cited does not use sleep() as a method of synchronizing threads. It appears to be used in an attempt to prevent any particular thread from repeatedly acquiring a lock on the mutex thus starving the other threads. And, by the way, pthread_join can't be used instead of sleep() (not in general nor in the code contained in the article you provided as back-up for your claims).
To recap: the tutorial is not wrong in using it because it is not using it in the way you claim it is using it. |
Aaah, yes yes, now I realize the use of word "synchronize" was wrong. Thanks, that's how we learn. and pthread_join can be used as a wait. Is that term correct now?
|
I suppose one could claim some degree of similarity between pthread_join and waitpid given that they do somewhat similar things with regards to threads and processes, respectively.
|
Thanks again, keep on correcting me :)
|
Hello,
when we're by sleep function I should ask... Why sleep function don't work for me in windows xp with codeblocks and gcc? Functions from pthreads works well but I can't get sleep. Compiler says: Quote:
What else can be? And, did sleep stops execution for number of seconds or milliseconds? Is there a simple way to chose between them? |
Quote:
I personally don't use Windows at all, but I understand that if you include windows.h and winbase.h, you can use Sleep(), which takes the number of milliseconds to sleep as a DWORD parameter. For portability, nanosleep() (declared in time.h) is a much better choice. Here is a portable nap() function, which takes the number of seconds to sleep as a double (fractional seconds are ok). It will normally return zero (0.0), but if it is interrupted, it will return the number of seconds left (to sleep). Code:
#include <time.h> (sleep() may be implemented internally with alarm()/SIGALRM signal, which makes sleep() in portable threaded programs problematic.) |
Quote:
Quote:
And, indeed, as I recently discovered, if I want my whole process to sleep, and use 'nanosleep', 'nanosleep' keeps 100% CPU load. I think this "came" to me with SUSE-11.1, i.e. I think previously my single-thread process was sleeping quite nicely with 'nanosleep'. To suspend a process one has to use 'sleep' or 'usleep' - the latter for fractional second if/when needed. |
Hello Nominal!:D, my favourite C guru!
Unfortunately, my computer won't sleep with windows.h and winbase.h too. Same error appears. And with nap function I have this: Quote:
Is here possible to rewrite nap function to work with milliseconds? |
Quote:
|
Yes Sergei,
something is obviously missing here. I am totally new in C and this kind of issues so I try reachable examples from net to learn C. Between others I try about 30 examples with pthreads and mutexes which are also from Posix and all of them works as expected. Except ones which have sleep function. This is how I know that sleep don't work for me. But maybe here is some workaround suitable to apply to beginners. Nominal already know which kind of help and which level of examples to give to me :) |
All times are GMT -5. The time now is 07:53 PM. |