gpt4 book ai didi

c - 如何一次又一次地生成相同的信号(SIGALRM)?

转载 作者:太空宇宙 更新时间:2023-11-04 11:07:04 28 4
gpt4 key购买 nike

我们正在尝试使用 sigaction 和 setitimer 从我们的三个函数中的 while(1) 循环中获取控制权,我们现在面临的问题是仅生成一次 SIGALRM,而下一次它不会生成,即我们能够从 fun1() 的 while(1) 控制,但不能从 fun2() 的 while(1) 控制。您能否提出解决此问题的方法。请在下面找到代码。

#define INTERVAL 500
void fun1();
void fun2();
void fun3();
void timer_handler(int );
struct itimerval it_val;
int count=0;
void timer_handler (int signum)
{

L1:
printf(“\nScheduler Called .. Calling Fun1″);

if(count==0){
count++;
fun1();
}
printf(“\nScheduler Called .. Calling Fun2″);
if(count==1){
count++;
fun2();
}
printf(“\nScheduler Called .. Calling Fun3″);
if(count==2){
count++;
fun3();
}
count=0;
goto L1;
}

void fun1()
{

struct sigaction sa;

/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;

/* Configure the timer to expire */
it_val.it_value.tv_sec = INTERVAL/1000;
it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000;

it_val.it_interval.tv_sec = INTERVAL/1000;;
it_val.it_interval.tv_usec=(INTERVAL*1000) % 1000000;
/* Start a timer. It counts down whenever this process is*/

sigaction (SIGALRM, &sa, NULL);
setitimer (ITIMER_REAL, &it_val, NULL);
while (1){
printf(“\nfun1″);
}

}

void fun2()
{

struct sigaction sa;

/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;

/* Configure the timer to expire */
it_val.it_value.tv_sec = INTERVAL/1000;
it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000;

it_val.it_interval.tv_sec = INTERVAL/1000;;
it_val.it_interval.tv_usec=(INTERVAL*1000) % 1000000;
/* Start a timer. It counts down whenever this process is*/

sigaction (SIGALRM, &sa, NULL);
setitimer (ITIMER_REAL, &it_val, NULL);
while (1){
printf(“\nfun2″);
}

}


void fun3()
{

struct sigaction sa;

/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;

/* Configure the timer to expire */
it_val.it_value.tv_sec = INTERVAL/1000;
it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000;

it_val.it_interval.tv_sec = INTERVAL/1000;;
it_val.it_interval.tv_usec=(INTERVAL*1000) % 1000000;
/* Start a timer. It counts down whenever this process is*/

sigaction (SIGALRM, &sa, NULL);
setitimer (ITIMER_REAL, &it_val, NULL);
while (1){
printf(“\nfun3″);
}

}
int main(){
timer_handler(1);
return 0;
}

最佳答案

当传递信号时,它会自动屏蔽,直到操作函数返回。

一般来说,你希望你的信号 Action 函数做到绝对最小。你所写的实际上是一个递归 Action 函数,其中包含你的大部分程序。即使它确实有效,你也会一直吃到吃完为止!


章节和诗句:来自 sigaction() for POSIX:

When a signal is caught by a signal-catching function installed by sigaction(), a new signal mask is calculated and installed for the duration of the signal-catching function (or until a call to either sigprocmask() or sigsuspend() is made). This mask is formed by taking the union of the current signal mask and the value of the sa_mask for the signal being delivered, and unless SA_NODEFER or SA_RESETHAND is set, then including the signal being delivered. If and when the user's signal handler returns normally, the original signal mask is restored.

关于c - 如何一次又一次地生成相同的信号(SIGALRM)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24848707/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com