18,772
社区成员
发帖
与我相关
我的任务
分享
futex(0x2afe7c4f2240, FUTEX_WAIT_PRIVATE, 2, NULL
root@primary:~/ci/cmake# cat monitor.c
#include <stdio.h>
#include <signal.h> /* for SIG_ERR */
#include <unistd.h>
#include <time.h> /*定时中断所需*/
#include <sys/time.h>
#include <sys/stat.h> /*for S_IRUSR*/
#include <stdint.h>
#include <fcntl.h>
#include <assert.h>
#include <semaphore.h>
#define MAX_LINE 4096
#define CLOCKID CLOCK_REALTIME /*定时中断的类型,默认为实时中断*/
#define SIG_TIMER SIGRTMIN
static timer_t timerid; /*POSIX定时器标识符*/
static sem_t cycle_mutex;
static int timer_init()
{
struct sigevent sev;
/*创建定时器*/
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG_TIMER;
sev.sigev_value.sival_ptr = &timerid;
if (-1 == timer_create(CLOCKID, &sev, &timerid))
{
perror("创建定时器失败");
return -1;
}
}
static int timer_start(void)
{
struct itimerspec its;
/*设置定时器*/
its.it_value.tv_sec = 60; /*1min一次*/
its.it_value.tv_nsec = 0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
if (-1 == timer_settime(timerid, 0, &its, NULL))
{
perror("设置定时器失败");
return -1;
}
return 0;
}
static void timer_action(int32_t sig, siginfo_t *si, void *uc)
{
static int count = 0;
static char buff[MAX_LINE] = {0};
int ret = 0;
ret = read(2,buff,MAX_LINE);
if(-1 == ret)
{
count ++;
}
else
{
count = 0;
}
if(count > 10)
{
sem_post(&cycle_mutex);
}
}
static int32_t sig_init(void)
{
struct sigaction sa;
/*初始化自定义信号*/
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timer_action;
sigemptyset(&sa.sa_mask);
if (sigaction(SIG_TIMER, &sa, NULL) == -1)
{
perror("初始化SIG_TIMER信号失败");
return -1;
}
return 0;
}
int main()
{
int ret = 0;
/*set stdin to no block*/
ret = fcntl(2,F_GETFL,0);
if (-1 == ret)
{
perror("fcntl get stdout failed");
return -1;
}
ret = fcntl(2,F_SETFL,ret | O_NONBLOCK);
if (-1 == ret)
{
perror("fcntl set stdout failed");
return -1;
}
ret = timer_init();
assert(-1 != ret);
ret = timer_start();
assert(-1 != ret);
ret = sig_init();
assert(-1 != ret);
ret = sem_init(&cycle_mutex,0,0);
if (-1 == ret)
{
perror("初始化cycle_mutex失败");
return -1;
}
do
{
ret = sem_wait(&cycle_mutex);
}
while(-1 == ret);
printf("exit\n");
return 0;
}
$gcc -lrt monitor.c -o monitor
$ps -ef | grep demo # find demo pid 1111
$strace -fp 1111 | ./monitor | kill -9 1111 &