我想制作一个计时器,它在一定间隔后调用我的函数,然后继续。每当我的特定过程完成时,我就想停止这个计时器。在这里,我有一个函数,我想在特定时间后调用它,但我想在这个函数中传递参数。
我的问题如下。
- 如何将参数传递给该函数?
- 有什么方法可以实现这个目标。
目前我正在使用此代码。请阅读我在代码之间写的代码注释,以便您了解我的目标。
代码:
#include <stdio.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>
int main()
{
int result = 10;
int i =0;
// here I make timer which calls getP() function and I want to pass some
// parameter to my getP function from here.
// I don't want to make these variables global.
initSigaction();
initTime();
// other process thread. If this process will be completed then I have to stop timer.
pauseTime();
}
void initSigaction(void)
{
struct sigaction act;
act.sa_handler = getP;//;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGPROF, &act, NULL);
}
void initTime(void)
{
struct itimerval val;
//printDebugLog(WIFI,"Timer value : %d",TIMER);
val.it_value.tv_sec = 1;
val.it_value.tv_usec = 0;
val.it_interval = val.it_value;
printf("\nProgress Timer started\n");
setitimer(ITIMER_PROF, &val, NULL);
}
void getP()
{
// I want to get some parameter from main function and want to do some
// calculation on this. How can I get that parameter?
printf("HI Hello");
}
int pauseTime(void)
{
struct itimerval val;
val.it_value.tv_sec = 0;
val.it_value.tv_usec = 0;
val.it_interval = val.it_value;
setitimer(ITIMER_PROF, &val, NULL);
printf("\n Stop Progress Timer\n");
return EXIT_SUCCESS;
}