linux 之 系统编程-进程fork

知识点 :

1.进程的话就是创建多任务

2.Ubuntu 中提供了fork()函数用来执行多任务 (windows中不能使用)

3.fork的返回值 ret= fork() 返回值有两个 ret =0 和 ret>0 ret == 0 为子进程 ret > 0 为 父进程   父进程的返回值 为子进程的pid

4.通过调用os.getpid()  os.getppid() 查看当前pid 和父进程

5.多次调用fork() 进程指数增长

6.当主进程执行完成后 不会等待子进程

import os
import time
ret = os.fork()
while True:
    time.sleep(1)
    if ret ==0:
        print("1")
    else :
        print("2")
import os
ret1 = os.fork()
if ret1 > 0:
    print("parents-fork")
    print("current:%d"%(os.getpid()))
else :
    print("son-fork")
    print("current:%d  parent:%d"%(os.getpid(),os.getppid()))

fork()

fork()

fork()

多次进程调用关系:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值