我在使用SIGALRM时遇到了麻烦。我每隔3秒用它来写一些活动进程的pid。 但是一旦它杀死主要过程就会发射。我做错了什么? 我也在使用信号杀死每个子进程一段时间后,我在那里使用SIGTERM。 在我将这部分添加到列出活动进程之前,它会很好。即使在杀死了主要人之后,其他人仍在继续。
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <signal.h>
#include <time.h>
#include <map>
using namespace std;
////DARK SORROW PLACE////////////////////////////
#define CHLD_DELAY 3
std::map<pid_t, bool> pidy;
/////////////////////////////////////////////////
void sweetDreams(int sec, int nanosec)
{
timespec ttw;
ttw.tv_sec = sec;
ttw.tv_nsec = nanosec;
while( nanosleep( &ttw, &ttw) && ( errno != EINTR ) ) continue;
}
//////////////////////////////////////////////////
void ns(long int ns, timespec *ts)
{
ts->tv_sec = (time_t)ns/1000000000;
ts->tv_nsec = (long)(ns - ts->tv_sec*1000000000);
}
//////////////////////////////////////////////////
class Order
{
public:
char* table;
int start;
int step;
int shift;
long int dt;
long int dieAfter;
};
////////////////////////////////////////////////////
void killer(int sig, siginfo_t *siginfo, void *context)
{
// kill(siginfo->si_pid, NULL);
_exit(0);
}
////////////////////////////////////////////////////
void carefullDad(int sig)
{
cout << "lista zywych dzieci:\n-----------------------" << endl;
for(auto i : pidy)
{
if( i.second ) cout << i.first << endl;
}
cout << "-----------------------" << endl;
}
////////////////////////////////////////////////////
int main(int argc, char** argv)
{
char test[] = { 't', 'e', 's', 't' };
Order orderArr[2] = {
{test, 0, 2, 0, 1000000000L, 10000000000L},
{test, 1, 3, -32 , 2000000000L, 6000000000L}
};
//pid_t pidArr[sizeof(orderArr) / sizeof(Order)];
pid_t wpid;
int status = 0;
struct sigevent st;
// memset(&st, 0, sizeof(st));
st.sigev_notify = SIGEV_SIGNAL;
st.sigev_signo = SIGALRM;
struct itimerspec it;
//memset(&it, 0, sizeof(it));
it.it_value = { CHLD_DELAY,0L};
it.it_interval = {CHLD_DELAY,0L};
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = carefullDad;
sigaction(SIGALRM, &act, NULL);
timer_t timer;
timer_create( CLOCK_REALTIME, &st, &timer);
timer_settime(timer, 0, &it, NULL);
for(Order ord : orderArr)
{
// static int i = 0;
pid_t pid = fork();
if(pid == -1)
{
cerr << "Blad!!!" << endl;
exit(1);
}
if(!pid)
{
//some code here
//end rest is here
答案 0 :(得分:2)
您忘了设置act.sa_flags
。
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = carefullDad;
act.sa_flags = 0;
sigaction(SIGALRM, &act, NULL);
设置信号处理程序时,它可能设置了SA_RESETHAND
标志。
答案 1 :(得分:1)
使用sigprocmask()
忽略SIGALRM
或使用sigaction()
安装有效的处理程序。
答案 2 :(得分:1)
对于将来遇到此问题的每个人。 你需要设置:
struct sigaction act;
act.sa_handler = carefullDad;
act.sa_flags = SA_RESTART;
sigaction(SIGALRM, &act, NULL);