關於LINUX殭屍程式的出現和原理
殭屍程式出現在父程式沒有回收子程式的PCB的時候,這個時候子程式已經結束,但是父程式沒有回收他,儲存了一份子程式的PCB在父程式的核心空間中。
殭屍程式佔用的是一個PCB結構體的記憶體空間,所以佔用量不會太大,但是過多的殭屍程式就會出現記憶體洩露,解決的辦法就是給父程式傳送一個終止的訊號
如 9) SIGKILL,2) SIGINT , 15) SIGTERM訊號,只要父程式終止了,那麼殭屍程式的PPID就變成了init程式那麼自然init程式回收了子程式的PCB
來看一段程式碼:
#include<stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
void serror( const char *ichar,int i)
{
perror(ichar);
exit(i);
}
int main(void)
{
pid_t pid;
int i,c,p;
for(i = 0;i < 10;i++)
{
if((pid = fork()) == 0)
{
break;
}
else if(pid < 0)
{
serror("forkerror",1);
}
}
if(pid == 0)
{
while(c < 5)
{
c += 1;
printf("child process is %d\n",getpid());
sleep(1);
}
}
if(pid > 0)
{
while(p < 20)
{
p += 1;
printf("parent process is %d\n",getpid());
sleep(1);
}
}
}
這個程式建立了10個子程式但是沒有使用wait/waitpid會後子程式PCB。執行後我們發現如下:
gaopeng 3902 3630 0 06:17 pts/25 00:00:00 ./a.out
gaopeng 3903 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3904 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3905 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3906 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3907 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3908 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3909 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3910 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3911 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3912 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
10個殭屍子程式。但是等到20秒後父程式結束,也就沒有了。那麼改進如下:
#include<stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
void serror( const char *ichar,int i)
{
perror(ichar);
exit(i);
}
void do_sig(int signo)
{
int status;
pid_t pid;
while((pid = waitpid(0,&status,WNOHANG))>0)
{
if(WIFEXITED(status))
{
printf("child %d exit normal %d\n",pid,WEXITSTATUS(status));
}
else if(WIFSIGNALED(status))
{
printf("child %d cancel by signal %d\n",pid,WTERMSIG(status));
}
}
}
int main(void)
{
pid_t pid;
int i,c,p;
for(i = 0;i < 10;i++)
{
if((pid = fork()) == 0)
{
break;
}
else if(pid < 0)
{
serror("forkerror",1);
}
}
if(pid == 0)
{
while(c < 10)
{
c += 1;
printf("child process is %d\n",getpid());
sleep(1);
}
return i;
}
if(pid > 0)
{
struct sigaction act;
act.sa_handler=do_sig;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGCHLD,&act,NULL);
while(p < 20)
{
p += 1;
printf("parent process is %d\n",getpid());
sleep(1);
}
}
}
主要為加入了WAITPID來防止殭屍程式
殭屍程式佔用的是一個PCB結構體的記憶體空間,所以佔用量不會太大,但是過多的殭屍程式就會出現記憶體洩露,解決的辦法就是給父程式傳送一個終止的訊號
如 9) SIGKILL,2) SIGINT , 15) SIGTERM訊號,只要父程式終止了,那麼殭屍程式的PPID就變成了init程式那麼自然init程式回收了子程式的PCB
來看一段程式碼:
#include<stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
void serror( const char *ichar,int i)
{
perror(ichar);
exit(i);
}
int main(void)
{
pid_t pid;
int i,c,p;
for(i = 0;i < 10;i++)
{
if((pid = fork()) == 0)
{
break;
}
else if(pid < 0)
{
serror("forkerror",1);
}
}
if(pid == 0)
{
while(c < 5)
{
c += 1;
printf("child process is %d\n",getpid());
sleep(1);
}
}
if(pid > 0)
{
while(p < 20)
{
p += 1;
printf("parent process is %d\n",getpid());
sleep(1);
}
}
}
這個程式建立了10個子程式但是沒有使用wait/waitpid會後子程式PCB。執行後我們發現如下:
gaopeng 3902 3630 0 06:17 pts/25 00:00:00 ./a.out
gaopeng 3903 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3904 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3905 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3906 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3907 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3908 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3909 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3910 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3911 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
gaopeng 3912 3902 0 06:17 pts/25 00:00:00 [a.out] <defunct>
10個殭屍子程式。但是等到20秒後父程式結束,也就沒有了。那麼改進如下:
#include<stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
void serror( const char *ichar,int i)
{
perror(ichar);
exit(i);
}
void do_sig(int signo)
{
int status;
pid_t pid;
while((pid = waitpid(0,&status,WNOHANG))>0)
{
if(WIFEXITED(status))
{
printf("child %d exit normal %d\n",pid,WEXITSTATUS(status));
}
else if(WIFSIGNALED(status))
{
printf("child %d cancel by signal %d\n",pid,WTERMSIG(status));
}
}
}
int main(void)
{
pid_t pid;
int i,c,p;
for(i = 0;i < 10;i++)
{
if((pid = fork()) == 0)
{
break;
}
else if(pid < 0)
{
serror("forkerror",1);
}
}
if(pid == 0)
{
while(c < 10)
{
c += 1;
printf("child process is %d\n",getpid());
sleep(1);
}
return i;
}
if(pid > 0)
{
struct sigaction act;
act.sa_handler=do_sig;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGCHLD,&act,NULL);
while(p < 20)
{
p += 1;
printf("parent process is %d\n",getpid());
sleep(1);
}
}
}
主要為加入了WAITPID來防止殭屍程式
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2057203/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 檢視 Linux 殭屍程式Linux
- Linux殭屍程式處置Linux
- fork和殭屍程式
- Linux 中殭屍程式詳解Linux
- Linux中殭屍程式是什麼意思?怎麼檢視殭屍程式?Linux
- 殭屍程式和孤兒程式
- Linux系統殭屍程式詳解Linux
- Perl程式:殭屍程式和孤兒程式
- Linux 效能優化之 CPU 篇 ----- 殭屍程式Linux優化
- 殭屍程式,孤兒程式
- Mirai 殭屍網路出現了新的變種AI
- Linux上的殭屍跑得比Windows快LinuxWindows
- 殭屍網路XorDDoS的原理分析與清除
- UNIXC002 程式資源的回收、孤兒程式和殭屍程式
- 針對執行 Webmin 的 Linux 伺服器出現了新的 Roboto 殭屍網路WebLinux伺服器
- 孤兒程序和殭屍程序
- 子程式、孤兒程式,殭屍程式, init程式
- 物聯網教程Linux系統程式設計——特殊程式之殭屍程式Linux程式設計
- 系統中出現大量不可中斷程式(D)和殭屍程式(Z)怎麼辦?
- Linux 幹掉狀態為Z的殭屍程序Linux
- Go Exec 殭屍與孤兒程式Go
- 案例:系統中出現大量不可中斷程式(D)和殭屍程式(Z)怎麼辦?
- PHP 多程式之孤兒和殭屍簡單講解PHP
- Python 植物大戰殭屍程式碼實現(2):植物卡片選擇和種植Python
- JaCoCo助您毀滅線上殭屍程式碼
- 什麼是殭屍程式以及如何處理
- 濫用ThinkPHP漏洞的殭屍網路Hakai和YowaiPHPAI
- 怎樣有效的治理殭屍網路?
- Unity 植物大戰殭屍(一)Unity
- 什麼是殭屍網路
- 從一道面試題來學習前臺程式和後臺程式、孤兒程式和殭屍程式面試題
- 聊聊 PHP 多程序模式下的孤兒程序和殭屍程序PHP模式
- 殭屍蜜網:首款具備誘捕及反探測能力的物聯網殭屍網路
- 3.19實戰殭屍工廠1
- 3.20實戰殭屍工廠2
- Mirai殭屍網路重出江湖AI
- 《植物大戰殭屍》10歲了
- 植物大戰殭屍 雜交版
- 植物大戰殭屍-雜交版