c语言sigaction,C语言中的Sigaction和setitimer

1我试图在1秒后杀死一个子进程。 子进程是一个外部c程序,它运行一些嵌套的for循环,并在通过后显示“ ALL DONE”。 循环大约需要10秒钟,因此,如果我正确设置了计时器和动作处理程序,则永远不要将其设置为“全部完成”。

创建计时器后,将使用execve运行子进程。pid_t pid = -1;

void time_handler(int signal){

printf("in time_handler\n");

kill(pid, SIGKILL);

}

int main(int argc, char *argv[]){

char *const path = "/home/aidan/CSC252/a4/test";

char *const env_args[] = { (char*)0 };

pid = fork();

while(1==1){

if(pid == 0){ //in child

printf("in child\n");

struct sigaction sa;

struct itimerval *timer = malloc(sizeof(struct itimerval));

sa.sa_handler = &time_handler;

printf("sigaction: %d\n", sigaction(SIGVTALRM, &sa, NULL));

timer->it_value.tv_sec = 0;

timer->it_interval.tv_sec = 1;

printf("settimer: %d\n", setitimer(ITIMER_REAL, timer, NULL)); //start timer

//printf("getitimer: %d\n", getitimer(ITIMER_REAL, timer));

int exec = execve(path, NULL, env_args);

printf("Exec is %d\n", exec); //-1 = error

}

else if (pid > 0){//in parent

printf("in parent, pid: %d\n", pid);

printf("wait: %d\n", wait(NULL));

pid = 0;

}

}

}

sigaction和setitimer都返回0,但是我不确定我是否正确处理了定时器到期。 我尝试阅读alarm(5);

但是我的结果不成功。

当前输出为:in parent, pid: 2362

in child

sigaction: 0

settimer: 0

In test

ALL DONE

wait: 2362

in child

sigaction: 0

settimer: 0

In test

ALL DONE

我不确定为什么它只运行两次然后退出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值