Fork and exit process

Int fork():

One process can create another process that is child process through fork(). fork() is called once but return twice. The different of twice return is returned 0 by child process but child ID by parent process.

  The child process is the copy of the parent process, it gets the copy of the parent such as data space,stack,heap resource. So, the child process has the copy of the space, it can't share these resource with parent, only share the code segment.

 

The usage of the fork(): 

int pid = fork():

if(pid == 0)

    printf("This is child process.")

else if(pid > 0)

    printf("This is parent process");

    //you can get the pid from the parent process.

 

If you want to launch a process in another process, you can create a child process to do it through fork(). 

 

 

Exit(int status);

 

If you exit you child process, the parent process can get the status and child process ID(This is a good way to get the child process ID when the child process exit.)

 

The following code is the example of exit() and fork()

 

The important Point: exit(status)--the status reserve the last 8bit int( status & 0377)

(If you status is greater than 255, you don't get the correct status code in parent process.)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值