zoukankan      html  css  js  c++  java
  • Python 孤儿进程与僵尸进程

    孤儿进程

    from multiprocessing import Process,current_process
    import time,sys
    
    def func():
        # 孤儿进程,父进程早于子进程退出
        print('子进程PID',current_process().pid)
        time.sleep(10)
    
        print('子进程退出')
    
    def main():
        print('子进程PID',current_process().pid)
        p = Process(target=func)
        p.start()
        print('父进程退出')
        sys.exit()   # 退出程序
        # p.join(timeout=1)  # 如果回收失败,则不回收
        # timeout 父进程只会阻塞等待1秒常识回收子进程。
    if __name__ == '__main__':
        main()
    
    import os,time
    # 父进程早于子进程退出,此时子进程一直会被1号进程回收,监控
    def func():
        pid = os.fork()      #fork()会执行分割成两部分,调用一次,返回两次,且在fork()在Linux运行。
    
        if pid == 0:     # 子进程pid为0
            time.sleep(10)
            print('我是子进程')
    
        if pid > 0:      # 父进程pid为子进程的PID
            print('我是父进程,我开启的子进程PID是',pid)
    if __name__ == '__main__':
        func()
    

    僵尸进程

    import time,sys
    from multiprocessing import Process,current_process
    def func():
        print('子进程PID',current_process().pid)
        print('子进程退出')
    
    def main():
        print('子进程PID',current_process().pid)
        p = Process(target=func)
        p.start()
        time.sleep(10)
        print('父进程退出')
        sys.exit()   # 退出程序
        # p.join(timeout=1)  # 如果回收失败,则不回收
        # timeout 父进程只会阻塞等待1秒常识回收子进程。
    if __name__ == '__main__':
        main()
    
    # 注:在Linux中查看进程,Z+,代表僵尸进程    ps -aux|grep python
    
    
  • 相关阅读:
    spring boot整合使用mybatis
    spring boot整合使用jdbcTemplate
    spring boot全局捕获异常
    springboot 静态资源访问
    spring boot项目的启动方式
    第一个spring boot项目 springboot-helloworld
    ASP.Net MVC 登录授权验证
    C# mysql 事务处理
    无法删除数据库,因为该数据库当前正在使用"问题解决
    mysql 按照小时去除一段时间内的不同状态数据统计
  • 原文地址:https://www.cnblogs.com/xinzaiyuan/p/12507572.html
Copyright © 2011-2022 走看看