자식 (r.out
)을 시작하는 프로그램이 있으며 그 자식에는 시간 제한이 있습니다.제한 시간 후에 완료되지 않은 경우 자식 프로세스를 중지하는 방법
제한 시간이 지난 후 r.out
실행을 중지하는 방법을 알고 싶습니다.
나는 리눅스에서 코드를 실행 중이다. 아이를 죽일하기 위해
#include <unistd.h>
#include <signal.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#define TIME_LIMIT 1 //determine time limit
void my_function(); //supposed to include the submitted code
void alarm_handler(int);
int main()
{
if (sigaction(SIGALRM, NULL, NULL) == -1)
err(1, NULL);
signal(SIGALRM, alarm_handler); // assigning an alarm handler for SIGALRM
alarm(TIME_LIMIT); // install an alarm to be fired after TIME_LIMIT
system("./r.out"); //Running the file
alarm(0);
return 0;
}
void alarm_handler(int sig)
{
printf("%s" , "Time limit exceeded");
//Here i want a code to stop the r.out file
}
코드 검토 서비스가 없습니다. – Olaf
스레딩을 사용할 수 있습니까? 그렇다면'r.out'을 쓰레드에서 실행시키고'n' 초 후에 쓰레드를 죽일 수 있습니다. –
'SIGKILL'을 보내십시오. 그러나 더 잘 제어하려면'fork/exec' 명령으로 더 잘 스폰하십시오. –