This question already has answers here:
How does sleep(), wait() and pause() work?
(4 answers)
Closed 2 years ago.
In C, how does the sleep function work? At the background is a while loop created? Or for loop? I would like to know exactly what lib does, how could I recreate a sleep in a simple way without having to use lib?
It's not implemented with a loop of any kind (that would waste energy occupying a core when you're doing nothing); it's a system call in which you tell the OS to suspend the current thread and wake it after the interval has elapsed (the exact mechanism used varies by OS). Reimplementing it yourself is ultimately going to depend on a system call and/or signals in some way; don't bother, just use sleep (or nanosleep, or Sleep, depending on OS).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to do some operation at regular intervals in my thread. What is the best method to do this?
Like if, i have a local socket communication between linux deamon and a android application. In this case, if i want to send data periodically to the android app from deamon, how can i proceed?
If you use Linux why not try sleep() or usleep() functions of unistd.h?
sleep(5);
Will pause the thread for five seconds and then resume execution.
sleep(sec)
The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process.
usleep(usec)
usleep() function suspends execution of the calling thread for (at least) usec microseconds.
Emphasis mine.
Taken from here and here.
You should use Simple Signals - C programming and alarm function if you want truely accurate timing. If you don't do this, using sleep() etc will eventually result in your timer shifting due to the overhead of your code, the OS, etc.
This question already has answers here:
How to detect the launching of programs on Linux?
(8 answers)
Closed 7 years ago.
I am writing a C program that runs in the background of the Linux shell and if the total memory consumption is over 85% of the total memory, then it will print out a warning. What would be the best way to check if a new process was created (I want to check the values for the memory every time a process is created)?
The most effective way of determining when processes are created (and exit) will be to use the proc connector. It's somewhat complex to use, but will notify your process immediately when events occur.
However, keep in mind that the memory usage of processes can change dramatically while they are running. Monitoring for processes being created is almost certainly not going to be sufficient for your needs; you will need to poll memory usage periodically. (There is no general way to get notifications for system memory usage, short of running your processes in a cgroup with a memory controller and registering an OOM handler. You don't want to do this.)
This question already has answers here:
Conditional Variable vs Semaphore
(8 answers)
Closed 8 years ago.
Why should we use wait() and signal() operation in multithreading applications?
I'm relatively new to multithreading and somewhat understand mutual exclusion but I need a better understanding of how wait() and signal() come into the equation.
It seems I'm achieving thread safety by only using lock() and unlock(). Am I wrong?
Can someone give me an example of wait/signal being used and wait and signal not being used with lock/unlock? What are the benefits to using wait/signal over just lock/unlock?
Thanks.
I work with computational maths/science so my examples come from there.
If you were doing a reduce operation such as a dot product (need to sum many calculations) then lock and unlock are useful as the order of the sum does not matter and if it's free the thread should go for it.
If you were solving a PDE over time before you can take the next time step the previous time step needs to be completed, a lock/unlock wouldn't work as even if the data is free for modification the prerequisite calculations may not have been done, this is where you would use a wait/signal.
Cramer your answer gave me good hints but the answer on this page was exactly the explanation I needed.
Conditional Variable vs Semaphore
I need to pause the execution of the main thread with out using sleep statement.
is there any function or status values that shows the alive status of other threads like isalive() in java?
pause() often works well; it suspends execution until a signal is received.
Standard C provides no way to pause the main thread, because standard C has no concept of threads. (That's changing in C201X, but that new version of the standard isn't quite finished, and there are no implementations of it.)
Even sleep() (which is a function, not a language-defined statement) is implementation-specific.
So it's not really possible to answer your question without knowing what environment you're using. Do you have multiple threads? If so, what threading library are you using? Pthreads? Win32 threads?
Why does sleep() not satisfy your requirements? (Probably because it pauses all threads, not just the current one.)
(Hint: Whenever you ask "How do I do X without using Y?", tell us why you can't use Y.)
Consult the documentation for whatever thread library you're using. It should provide a function that does what you need.
A extremely simple approach would be using something as simple as getchar().
Other approach could be waiting for a signal with pthread_cond_wait (or any other similar function in a different threading API).
Other approach could be sitting on a tight loop and using a semaphore (or something simpler like a global variable value) to wait for the other threads to finish.
Anyway, there are several options. You don't say enough about your problem to tell what's your best choice here.
select() is often a good choice.
On Linux, epoll() is often a good alternative to select().
And every program, "threaded" or not, always has "main thread". If you're actually using threads, however, look at pthread_cond_wait().
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Bind threads to processors
In Linux, is it possible to fix threads to execute on certain cores. If that is possible, I also want to know if it is possible to execute one thread exclusively on a certain core, that is disallowing any other thread to execute on that core while that thread is executing.
That's what pthread_setaffinity_np(3) does.
The pthread_setaffinity_np() function sets the CPU affinity mask of
the thread thread to the CPU set pointed to by cpuset. If the call is
successful, and the thread is not currently running on one of the CPUs
in cpuset, then it is migrated to one of those CPUs.
As an example:
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(3, &set); /* Run only on the third CPU. */
pthread_setaffinity_np(thr, CPU_SETSIZE, &set);
You can also do it with sched_setaffinity(2) and gettid, but that manual page says:
If you are using the POSIX threads API, then use
pthread_setaffinity_np(3) instead of sched_setaffinity().