簡體   English   中英

在嘗試終止父進程時,子進程仍在運行

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

給出以下代碼:

分叉:

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

試圖殺死進程:

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

當我嘗試殺死“父親”進程時,子進程繼續運行嗎?

這是為什么 ?

問候

問題是您僅殺死了一個進程。 SIGKILL僅發送到您指定的一個進程。 您可以使用流程組一次將其發送到多個應用程序,但是全部或全部都沒有,在這里無濟於事。

因此,首先,不要使用SIGKILL,而要使用SIGTERM。

然后,在子進程中安裝SIGTERM處理程序。 處理程序應依次發出自己的孩子的信號,然后退出。

您需要在閱讀了signalsigaction 有手冊頁和許多Web資源。

如果子進程的父進程被殺死,則子進程將繼續運行,直到其正常退出或中止為止。 由於子進程的父進程不存在,因此子進程變成了zombie並且其返回值和核心數據結構被保留以供父進程收集。

在Linux上, init進程將重新創建僵屍進程的父級 ,並free關聯的內存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM