2015-06-10 145 views
1

我在我的程序中使用inotify()函数来监视/ proc目录。现在我的工作就是找到一个进程何时成为僵尸。然后我需要输出警告,发现僵尸进程。目前,节目录制了一些变化,但我不知道如何找到僵尸进程..僵尸进程 - 寻找它

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/inotify.h> 
#include <limits.h> 

int main() 
{ 
    int inotify_fd, wd; 
    int num; 

    char buffer[sizeof(struct inotify_event)+NAME_MAX+1]; 
    struct inotify_event *dogodek; 

    inotify_fd = inotify_init(); 
    if (inotify_fd==-1) { 
     perror("inotify_init"); 
     return 1; 
    } 


    wd = inotify_add_watch(inotify_fd, "/proc/",IN_ALL_EVENTS); 
    if (wd==-1) {              
     perror("inotify_add_watch"); 
     return 1; 
    } 

    printf("Map gots descriptor %d\n",wd); 

    int f; 
    for (f=0; f<20; f++) { 
     num=read(inotify_fd, &buffer, sizeof(buffer)); 
     if (num>0) { 
      dogodek=(struct inotify_event*)buffer; 

      printf("Access to map with descriptor %d\n", dogodek->wd); 
      if (dogodek->len>0) 
       printf("\tevent found: %s\n", dogodek->name); 
      else 
       printf("\tevent found in directory!\n"); 


      printf("\tMask of the event %x\n", dogodek->mask); 
      if (dogodek->mask & IN_ACCESS) 
       printf("\t\tReading file!\n"); 

      if (dogodek->mask & IN_CREATE) 
       printf("\t\tCreating file!\n"); 

      if (dogodek->mask & IN_DELETE) 
       printf("\t\tDeleting file!\n"); 

      if (dogodek->mask & IN_OPEN) 
       printf("\t\tOpening file!\n"); 




     } 
    } 
    inotify_rm_watch(inotify_fd, wd); 
    return 0; 
} 

我需要实例代码不只是explation如果有可能。

+0

可能重复[?如何检查是否有PID X的过程就是一个僵尸(http://stackoverflow.com/questions/1619020/how - 检查,如果一个进程与一个pid-x-is-a-zombie) –

+1

也许有一些相似之处,但我提出了一个代码。 – sandivratar

回答

1

以下适用于Linux的(至少):

  • 一个进程的状态可以在/proc/[pid]/status找到。

  • 对于僵尸文件/proc/[pid]/cmdline是空的,即从它读取返回0字节。

详情请看这里:http://man7.org/linux/man-pages/man5/proc.5.html