donglun1020 2016-07-18 22:14
浏览 110

所有Linux发行版中的/ proc / [pid] / stat是否总是可用?

I want to find the best universal way to check if a process exist and is running on any linux.

In Unix / BSD I am available to do this via kqueue thanks to the syscall.Kqueue() using EVFILT_PROC / NOTE_EXIT it doesn't' matter if is a mac os X, netbsd, freebsd, etc the code will just work and help to monitor the status of a PID.

Trying to achieve the same on linux, I came with the idea to check periodically for the existence of the /proc/[pid]/stat file, instead of sending a signal 0, kill -s 0 like suggested here: https://stackoverflow.com/a/15210305/1135424 mainly to simplify the logic due that non-nil error could be returned for existing processes.

Probably using something like:

initialStat, err := os.Stat(fmt.Sprintf("/proc/%d/stat", self.pid)                                                                                                                                                           
if err != nil {                                                                                                                                                                                                              
    return                                                                                                                                                                                                                   
}                                                                                                                                                                                                                            

for {                                                                                                                                                                                                                        
    stat, err := os.Stat(fmt.Sprintf("/proc/%d/stat", self.pid)                                                                                                                                                              
    if err != nil {                                                                                                                                                                                                          
        return err                                                                                                                                                                                                           
    }                                                                                                                                                                                                                        

    if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {                                                                                                                                        
        return nil                                                                                                                                                                                                           
    }       
    // wondering how to avoid sleeping here
    time.Sleep(time.Second)                                                                                                                                                                                                                 
}

But wondering if in all linux /proc/[pid]/stat is always available or if by sending signal 0 kill -0 $PID is basically doing exactly the same thing.

At the end I can always fallback to kill -O $PID but just wondering what posible solutions could be used (probably inotify) with the intention to avoid having to sleep on the loop mainly for not consuming to much CPU resources.

  • 写回答

1条回答 默认 最新

  • dousha1394 2016-07-19 18:29
    关注

    For child processes, you should use waitpid.

    There's a netlink API for process events (http://netsplit.com/the-proc-connector-and-socket-filters) which might be usable for your case:

    http://godoc.org/github.com/cloudfoundry/gosigar/psnotify#PROC_EVENT_EXIT

    import "github.com/cloudfoundry/gosigar/psnotify"
    
    func waitpid(pid int) error {
        watcher, err := psnotify.NewWatcher()
        if err != nil {
            return err
        }
    
        if err := watcher.Watch(pid, psnotify.PROC_EVENT_EXIT); err != nil {
            return err
        }
    
        defer watcher.Close()
    
        // At this point, you should probably syscall.Kill(pid, 0) to check that the process is still running.
    
        for {
            select {
            case ev := <-watcher.Error:
                // TODO..
                return ev
            case <-watcher.Exit:
                return nil
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c