我想写一个基本的过程控制块的程序,下面的代码显示了我在测试阶段做了什么,我想知道如果是的话,我们是否可以恢复一次孩子而不是再次分岔一个新孩子我们如何做到这一点。 在此先感谢!
代码:
`是否可以恢复被杀死的子进程?
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<time.h>
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
int hell()
{
int j;
for(j=1;j<6;j++)
printf("%d hello\n",j);`
}
int hello(k)
{
int i
for(i=1;i<15;i++
printf("%5d hello \n", i);
}
void sigint()
{
signal(SIGCONT,sigint); /* reset signal */
printf("CHILD: I have received a SIGINT\n");
}
int main()
{
int i, status;
pid_t childID, endID,end1id,parentid;
time_t when;
label: if ((childID = fork()) == -1) {
perror("fork error");
exit(EXIT_FAILURE);
}
else if (childID == 0) {
time(&when);hell();
printf("k value %d\n",hello(5));
printf("Child process started at %s\n", ctime(&when));
printf("child PID : %d \n",getpid());
hello();
sleep(10);
//kill(childID, SIGKILL);
exit(EXIT_SUCCESS);
}
}
else {
time(&when);
printf("Parent process started at %s", ctime(&when));
printf("parent PID : %d\n",getpid());
hell();
parentid = getpid();
for(i = 0; i < 15; i++) {
endID =waitpid(childID,&status,WNOHANG|WUNTRACED);
printf("endid: %d\n",endID);
end1id = waitpid(parentid, &status, WNOHANG|WUNTRACED);
if (endID == -1) {
perror("waitpid error");
exit(EXIT_FAILURE);
}
else if (endID == 0) {
time(&when);
printf("Parent waiting for child at %s", ctime(&when));
sleep(1);
}
else if (endID == childID) {
if (WIFEXITED(status))
printf("Child ended normally\n\n");
else if (WIFSIGNALED(status)){
printf("Child ended because of an uncaught signal\n");goto label;}
else if (WIFSTOPPED(status)){
printf("Child process has stopped\n");goto label;}
exit(EXIT_SUCCESS);
}
}
}
什么?如果它能活下去的话,那么称它为“被杀”? –
育儿的第一条规则:不要杀死你的孩子,除非你确定你不希望他们继续生活。 –