weixin_44655378 2019-07-22 01:14 采纳率: 0%
浏览 421
已采纳

关于子进程与父进程的问题,有些搞不明白,希望有人能帮小弟解答下!谢谢了!

小弟刚学python,有个问题想请教,父进程与子进程的问题,求大神解答,代码在下面给出:
源代码:
import os
from time import sleep

pid = os.fork()
if pid <0 :
print("create process failed")
elif pid == 0:
sleep(3)
print("this is child process")
else :
sleep(1)
print("this is parent process")

print("******the end*******")

结果:
this is parent process
******the end*******

Process finished with exit code 0
无论我跑多少次,一直都没有子进程的结果,而我把时间对调之后,就会既显示父进程也显示子进程了,为什么子进程sleep时间比父进程长,就不会显示子进程了呢?另外我Debug了一下,结果又不一样了。
debug结果:
Connected to pydev debugger (build 191.7479.30)
this is child process
******the end*******

程序未结束,可是不显示父进程的东西了,到底为什么,搞不懂了,大神解释下啊!谢谢各位了!!!

  • 写回答

1条回答 默认 最新

  • qq_41054229 2019-07-22 09:40
    关注

    按照你的代码去执行是可以显示出 parent & child process message

    gti@ubuntu-vm:~/Test$ python f.py
    this is parent process
    ******the end*******
    gti@ubuntu-vm:~/Test$ this is child process
    ******the end*******

    gti@ubuntu-vm:~/Test$
    gti@ubuntu-vm:~/Test$ cat f.py
    #!/usr/bin/env python
    #coding:utf-8

    import os
    from time import sleep

    pid = os.fork()
    if pid <0 :
    print("create process failed")
    elif pid == 0:
    sleep(3)
    print("this is child process")
    else :
    sleep(1)
    print("this is parent process")

    print("******the end*******")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘