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.
Related
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).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
As we know that exec family function calls eventually calls execve() which is a system call and remaining are library functions. So, typically, whatever implications applies on execve() are true for other exec*() functions as well.
I would like to know if, during execution of execve(), all signals are blocked until it succeed, or if there is a way to pass signal to that pid which corresponds to exec? (I know it does not return on success and further execution of calling function does not happen)
I am not sure I got your question right, so feel free to correct me if I am wrong.
But, basically, yes, system calls can be considered as 'atomic' from the process point of view. So, once the execve() system call is started, only the kernel has the hand on it and it won't release the process until running the new command or failing with an error code or raise the SIGKILL signal (as SIGKILL is the only unblockable signal).
But, once the execve() spawned a new process (and returned from the kernel), it is perfectly interruptible with any signal.
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 8 years ago.
Improve this question
What is a practical relevance of signals in C programs ?
Where do we need signals. I am getting confused and feeling traped in this topic, Who generates signals ?
Signals are a lightweight way for processes to communicate with each other asynchronously; as such, it shouldn't be a surprise that processes generate signals.
Signals are in C because signals were how you communicated with a process in the original version of UNIX.
They are a fairly simple way of allowing your process to respond to external requests such as "re-read my config file", for example.
The K&R chapter is a good intro, but read the C standard to see the minimum for what is defined. In particular, the only portable thing you can do in a signal handler is either abort the process, or set an atomic flag which your main thread of execution will check in due course. In practice, people do more complicated things in signal handlers such as making system calls.
Signals provide a way of supporting asynchronous interrupts in a C program. That said, the C spec is almost completely void of specified behavior for signals, leaving them essentially entirely up to the implementation, so writing portable code that uses signals is almost impossible. You need to carefully read the documentation for your implementation to see what exactly signals can do.
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 8 years ago.
Improve this question
I don't really understand re-entrant code.
Can somebody show me an algorithm for re-entrant code so I can look at it? An explanation of how it works would be nice too. Thanks.
If you, as a developer of some function, can be sure that your function is not called during its execution time, then you may not to pay attention to make it reentrant.
However if your function can be called from different task, threads, or just by interruption signal, there can be a situation, when the function reenters itself during its execution, if it is not reentrant-compliant , not protected from multiple entrances it may lead to unpredictable behaviour, such as data corruption\overwriting or corruption of chronology of the actions in the function.
Here may be a number of approaches. Sometimes it is enough to write a function, for example, working only with copies of the external variables. However sometimes using of mutexes is inevitable. When mutexes are used, the caller of the function will be waiting for mutex unlocking. You just place mutex locking before the critical section and mutex unlocking after the critical section. Whole function body can be wrapped between lock and unlock couple.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for an open source timer library written in C. The library should have Timer callback functions, etc.
On searching I see POSIX timers or setitimer(), which use a signal based approach that may lead to problems in multithreaded code.
Say if I use POSIX Timers inside threaded code, the signals won't be reaching to the correct place. If I use several timers in a process then each one should use different signals. Are there any other alternatives?
Since you are running Linux, I would recommend using the built in POSIX timer API's.
int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid);
Here is a link to some documentation showing how to use POSIX timers which provide support for callback functions.
Regarding multiple timers in a process, the documentation says this:
A program may create multiple interval timers using timer_create().
Timers are not inherited by the child of a fork(2), and are disarmed and
deleted during an execve(2).
The kernel preallocates a "queued real-time signal" for each timer created
using timer_create(). Consequently, the number of timers is limited by the
RLIMIT_SIGPENDING resource limit (see setrlimit(2)).
Note that POSIX timers can be used in a threaded application by setting up notification using SIGEV_THREAD_ID as shown below:
The sevp.sigev_notify field can have the following values:
SIGEV_NONE
Don't asynchronously notify when the timer expires. Progress of the
timer can be monitored using timer_gettime(2).
SIGEV_SIGNAL
Upon timer expiration, generate the signal sigev_signo for the process.
See sigevent(7) for general details. The si_code field of the
siginfo_t structure will be set to SI_TIMER. At any point in time, at
most one signal is queued to the process for a given timer; see
timer_getoverrun(2) for more details.
SIGEV_THREAD
Upon timer expiration, invoke sigev_notify_function as if it were the
start function of a new thread. See sigevent(7) for details.
SIGEV_THREAD_ID (Linux-specific)
As for SIGEV_SIGNAL, but the signal is targeted at the thread whose ID
is given in sigev_notify_thread_id, which must be a thread in the same
process as the caller. The sigev_notify_thread_id field specifies a
kernel thread ID, that is, the value returned by clone(2) or gettid(2).
This flag is only intended for use by threading libraries.
The Linux way of doing it would be via timerfd_create which integrates nicely with epoll-based event loops (and thereby avoiding the restrictions of signal handlers)
This is far to simple to create a library for.
example:
#include <time.h>
int main()
{
time_t start,end;
double dif;
double duration=40f; //duration of timer
bool loop=true;
while(loop==true)
{
time(&start);
if(dif==duration)
{
/*callback*/
dif=0;
}
//do stuff
time(&end);
dif+=difftime(end,start);
}
{