久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Zombie Processes and their Prevention

 studydoer 2020-08-26

僵尸進程和如何防止其產(chǎn)生,。

Zombie state : When a process is created in UNIX using fork() system call, the address space of the Parent process is replicated. If the parent process calls wait() system call, then the execution of parent is suspended until the child is terminated. At the termination of the child, a ‘SIGCHLD’ signal is generated which is delivered to the parent by the kernel. Parent, on receipt of ‘SIGCHLD’ reaps the status of the child from the process table. Even though, the child is terminated, there is an entry in the process table corresponding to the child where the status is stored. When parent collects the status, this entry is deleted. Thus, all the traces of the child process are removed from the system. If the parent decides not to wait for the child’s termination and it executes its subsequent task, then at the termination of the child, the exit status is not read. Hence, there remains an entry in the process table even after the termination of the child. This state of the child process is known as the Zombie state.

上文的大意是:當子進程結束后,,仍然會在系統(tǒng)的進程表中有一個表項存儲著子進程的退出狀態(tài)信息,,如果父進程不執(zhí)行wait獲取子進程的退出狀態(tài),,那么進程表中的子進程表項就會一直存在下去,,我們把子進程結束了,,資源釋放了,,但是在進程表中依然有一個表項的狀態(tài)稱為僵尸狀態(tài),。這里的進程表項其實就是task_struct結構體.

// A C program to demonstrate working of
// fork() and process table entries.
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
  
int main()
{
    int i;
    int pid = fork();
  
    if (pid == 0)
    {
        for (i=0; i<20; i++)
            printf("I am Child\n");
    }
    else
    {
        printf("I am Parent\n");
        while(1);
    }
}

在終端執(zhí)行ps -aux查看進程表:

這里的[a.out] <defunct>就是僵尸進程,。

為什么要防止僵尸進程的產(chǎn)生呢?

There is one process table per system. The size of the process table is finite. If too many zombie processes are generated, then the process table will be full. That is, the system will not be able to generate any new process, then the system will come to a standstill. Hence, we need to prevent the creation of zombie processes.

大意是:一個系統(tǒng)只有一個進程表,,進程表的大小是有限的,。如果有太多的僵尸進程產(chǎn)生,那么進程表將會變滿,,導致系統(tǒng)不能再產(chǎn)生新的進程,,然后系統(tǒng)就會進入一個僵死狀態(tài),因此我們需要防止僵尸進程的產(chǎn)生,。

下面介紹三種防止僵尸進程產(chǎn)生的方法,。

方法1:用系統(tǒng)調用wait()

Using wait() system call : When the parent process calls wait(), after the creation of a child, it indicates that, it will wait for the child to complete and it will reap the exit status of the child. The parent process is suspended(waits in a waiting queue) until the child is terminated. It must be understood that during this period, the parent process does nothing just waits.

父進程調用wait()等待子進程結束和獲取它的狀態(tài),,父進程在子進程結束前是掛起的,父進程干不了其他事,。顯然這種方法并不可取,。

// A C program to demonstrate working of
// fork()/wait() and Zombie processes
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
  
int main()
{
    int i;
    int pid = fork();
    if (pid==0)
    {
        for (i=0; i<20; i++)
            printf("I am Child\n");
    }
    else
    {
        wait(NULL);
        printf("I am Parent\n");
        while(1);
    }
}

方法2:忽略SIGCHLD信號

By ignoring the SIGCHLD signal : When a child is terminated, a corresponding SIGCHLD signal is delivered to the parent, if we call the ‘signal(SIGCHLD,SIG_IGN)’, then the SIGCHLD signal is ignored by the system, and the child process entry is deleted from the process table. Thus, no zombie is created. However, in this case, the parent cannot know about the exit status of the child.

父進程調用signal(SIGCHLD,SIG_IGN),會導致子進程結束后收不到SIGCHLD信號,,但是子進程的進程表項是可以從進程表中刪除的,,因此不會產(chǎn)生僵尸進程,但是,,這樣一來,,父進程就無法獲取子進程退出的狀態(tài)了。這種方法也有缺點,。

// A C program to demonstrate ignoring 
// SIGCHLD signal to prevent Zombie processes
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
  
int main()
{
    int i;
    int pid = fork();
    if (pid == 0)
        for (i=0; i<20; i++)
            printf("I am Child\n");
    else
    {
        signal(SIGCHLD,SIG_IGN);
        printf("I am Parent\n");
        while(1);
    }

}


方法3:用信號處理函數(shù)

By using a signal handler : The parent process installs a signal handler for the SIGCHLD signal. The signal handler calls wait() system call within it. In this senario, when the child terminated, the SIGCHLD is delivered to the parent. On receipt of SIGCHLD, the corresponding handler is activated, which in turn calls the wait() system call. Hence, the parent collects the exit status almost immediately and the child entry in the process table is cleared. Thus no zombie is created.

父進程安裝SIGCHLD信號函數(shù),,在信號函數(shù)中調用wait()。當子進程結束,,父進程收到SIGCHLD信號后,,信號處理函數(shù)被激活,然后wait()被調用,。因此父進程可以立即獲取子進程退出狀態(tài),,并且子進程的進程表項被刪除,不會產(chǎn)生僵尸進程,。

// A C program to demonstrate handling of

// SIGCHLD signal to prevent Zombie processes.
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
  
void func(int signum)
{
    wait(NULL);
}
  
int main()
{
    int i;
    int pid = fork();
    if (pid == 0)
        for (i=0; i<20; i++)
            printf("I am Child\n");
    else
    {
        signal(SIGCHLD, func);
        printf("I am Parent\n");
        while(1);
    }
}

    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多