child 进程在 fork 中 3 秒后自行终止
child process kill itself after 3 seconds in fork
我有一个创建 child 进程和 parent 进程的 fork 函数
我希望 child 进程在 3 秒后立即终止自身,使用 kill(child, SIGKILL) 函数,即意味着不使用 exit() 函数。但是我不确定执行情况。我尝试在 while 循环结束后立即使用 kill 函数信号,但它不会杀死 child。
我试着把它放在主函数的末尾,但它在 3 秒之前就杀死了 child
我试着把它放在一个 while 循环结束后立即调用的函数中,但它并没有杀死 child
我怎样才能使用实际有效的更好实施来做到这一点?
这是我的代码,带有可用评论:
int main()
{
pid_t pid;
int seconds = 3;
int child;
pid = fork();
int i = 0;
if(pid == 0) //this is the child
{
child = getpid();
while( i <= seconds)
{
cout << " second " << i << endl;
sleep(1);
i++;
}
killChild(child); //function called after 3 seconds passes
}
else if(pid < 0) //error statement
{
cout << " we have an error " << endl;
}
else //parent if pid > 0
{
cout << " I am the parent " << endl;
sleep(1);
}
//kill(child, SIGKILL) placing it here will kill child before seconds passes
}
void killChild(int child) //function to kill child (NOT KILLING IT)
{
kill(child, SIGKILL);
}
void killChild(int sigNum)//Argument here is signal number
{
kill(getpid(), SIGKILL);
}
int main()
{
pid_t pid;
int child;
pid = fork();
if(pid == 0)//this is the child
{
signal(SIGALRM,killchild)
child = getpid();
alarm(3); //set alarm for 3 seconds
while(1); //continue to loop until 3 seconds passed
}
else if(pid < 0)//error statement
{
cout << " we have an error " << endl;
}
else//parent if pid > 0
{
cout << " I am the parent " << endl;
}
}
After 3 seconds, alarm() function will generate SIGALRM and on this signal egneration, your killchild() function will be called and kill child.
希望对您有所帮助!
我有一个创建 child 进程和 parent 进程的 fork 函数
我希望 child 进程在 3 秒后立即终止自身,使用 kill(child, SIGKILL) 函数,即意味着不使用 exit() 函数。但是我不确定执行情况。我尝试在 while 循环结束后立即使用 kill 函数信号,但它不会杀死 child。
我试着把它放在主函数的末尾,但它在 3 秒之前就杀死了 child
我试着把它放在一个 while 循环结束后立即调用的函数中,但它并没有杀死 child
我怎样才能使用实际有效的更好实施来做到这一点?
这是我的代码,带有可用评论:
int main()
{
pid_t pid;
int seconds = 3;
int child;
pid = fork();
int i = 0;
if(pid == 0) //this is the child
{
child = getpid();
while( i <= seconds)
{
cout << " second " << i << endl;
sleep(1);
i++;
}
killChild(child); //function called after 3 seconds passes
}
else if(pid < 0) //error statement
{
cout << " we have an error " << endl;
}
else //parent if pid > 0
{
cout << " I am the parent " << endl;
sleep(1);
}
//kill(child, SIGKILL) placing it here will kill child before seconds passes
}
void killChild(int child) //function to kill child (NOT KILLING IT)
{
kill(child, SIGKILL);
}
void killChild(int sigNum)//Argument here is signal number
{
kill(getpid(), SIGKILL);
}
int main()
{
pid_t pid;
int child;
pid = fork();
if(pid == 0)//this is the child
{
signal(SIGALRM,killchild)
child = getpid();
alarm(3); //set alarm for 3 seconds
while(1); //continue to loop until 3 seconds passed
}
else if(pid < 0)//error statement
{
cout << " we have an error " << endl;
}
else//parent if pid > 0
{
cout << " I am the parent " << endl;
}
}
After 3 seconds, alarm() function will generate SIGALRM and on this signal egneration, your killchild() function will be called and kill child.
希望对您有所帮助!