简体   繁体   中英

While trying to kill the father process , child processes are still running

Given the following code:

Forking :

if(strcmp(str,"mkDir")==0)
            {
                str = strtok(NULL," ");
                switch(pid_child = fork())
                {
                        case -1: 
                        {
                            printf("Problem with producing a new process!\n");
                            exit(1);
                            break;
                        }
                        case 0: 
                        {
                            wait(1);
                            strcat(curRoot,str);
                            strcat(curRoot,"\\");
                            if(num_dir>0)
                            {
                                free(arr);
                                num_dir=0;
                            }
                            if(numFile>0)
                            {
                                free(files);
                                numFile=0;
                            }
                            break;
                        }
                        default:
                        {
                            pid = getpid();
                            *cur_pid = pid;
                            arr = add_dir(arr,str,pid_child,&num_dir);
                            break;
                        }
                }
            }//if MKDIR

Trying to kill the processes :

struct Directory* rmDir(struct Directory* dirs,char* name,int *size)
{
    int i,m=0,j;
    struct Directory* temp=NULL;
    j = find_dir(dirs,name,*size);
    if(j==-1)
    {
        printf("The directory does not exist\n");
        return dirs;
    }
    else
    {
        temp = (struct Directory*)malloc(sizeof(struct Directory)*((*size)-1));
        for (i=0; i<*size;i++)
        {
            if(i!=j)
            {
                temp[m]=dirs[i];
                m++;
            }
        }//for
        kill(dirs[j].dir_pid,SIGKILL);
        (*size)--;
        free(dirs);
        printf("Directory - %s was removed successfully!\n",name);
        return temp;
    }
}//rmDir

When I try to kill the "father" process , the child processes keep on running ?

Why is that ?

Regards

The problem is that you're only killing one process. SIGKILL is only sent to the one process you specified. You can send it to more than one at once using process groups, but then it's all or nothing, which won't help here.

So, first, don't use SIGKILL, use SIGTERM.

Then, install a SIGTERM handler in the child processes. The handler should signal it's own children, in turn, and then exit.

You need to read up on signal or sigaction . There are man pages and many web resources.

If the child's parent process is killed, the child process continues to run until it exits normally or is aborted. Since, the child's parent process doesn't exists, the child process is turned into zombie and it's return value and core data structures are retained to be collected by the parent.

On Linux, the init process re-parents the zombie processes and free 's the associated memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM