fork()函数详解

描述

1.fork()简介

函数原型:

pid_t fork(void);//pid_t为int类型,进行了重载

pid_t getpid();// 获取当前进程的 pid 值。

pid_t getppid(); //获取当前进程的父进程 pid 值。

用于创建一个进程,所创建的进程复制父进程的代码段/数据段/BSS段/堆/栈等所有用户空间信息;在内核中操作系统重新为其申请了一个PCB,并使用父进程的PCB进行初始化;

Fork

如图所示 :我们将A 进程, 也就是调用 fork 的进程称之为父进程, 而新的进程(B 进程)称之为子进程。

关于fork 可以命令,查看详细说明及用法:

man 3 fork

NAME

       fork, wait, waitpid - basic process management

SYNOPSIS
       @load "fork"

       pid = fork()

       ret = waitpid(pid)

       ret = wait();

DESCRIPTION
       The fork extension adds three functions, as follows.

       fork() This  function  creates  a new process. The return value is the zero in the child and the process-id number of the child in the parent, or -1 upon error. In the latter case, ERRNO indicates
              the problem.  In the child, PROCINFO["pid"] and PROCINFO["ppid"] are updated to reflect the correct values.

       waitpid()
              This function takes a numeric argument, which is the process-id to wait for. The return value is that of the waitpid(2) system call.

       wait() This function waits for the first child to die.  The return value is that of the wait(2) system call.

BUGS
       There is no corresponding exec() function.

       The interfaces could be enhanced to provide more facilities, including pulling out the various bits of the return status.

EXAMPLE
       @load "fork"
       ...
       if ((pid = fork()) == 0)
           print "hello from the child"
       else
           print "hello from the parent"

SEE ALSO
       GAWK: Effective AWK Programming, filefuncs(3am), fnmatch(3am), inplace(3am), ordchr(3am), readdir(3am), readfile(3am), revoutput(3am), rwarray(3am), time(3am).

       fork(2), wait(2), waitpid(2).

AUTHOR
       Arnold Robbins, arnold@skeeve.com.

COPYING PERMISSIONS
       Copyright 2012, 2013, Free Software Foundation, Inc.

       Permission is granted to make and distribute verbatim copies of this manual page provided the copyright notice and this permission notice are preserved on all copies.


2.fork()特性

fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,它可能有三种不同的返回值:

在父进程中,fork返回新创建子进程的进程ID;

在子进程中,fork返回0;

如果出现错误,fork返回一个负值;

因此我们可以通过fork返回的值来判断当前进程是子进程还是父进程。(注:fork 调用生成的新进程与其父进程谁先执行不一定,哪个进程先执行要看系统的进程调度策略)

举个例子来解释fpid的值为什么在父子进程中不同:“相当于链表,进程形成了链表,父进程的fpid(p 意味point)指向子进程的进程id, 因为子进程没有子进程,所以其fpid为0.

3, fork()例程

看到这里大家对fork()有个大致了解了,让我们来看个例题:
 

#include

#include

#include

int main(int argc, const char *argv[])

{

    int num = 10;

    pid_t pid = fork();

    if(pid==0)

    {

        while (1)

        {

            num = 100;

           printf("The father pid=%d The child pid=%d num=%d ", getppid(),getpid(), num);

           sleep(3);

        }

    }

    else

    {

        while (1)

        {

            printf("The father pid=%d num=%d ", getpid(), num);

            sleep(5);

        }

    }

    return 0;

}

保存为fork_test.c

gcc  -o fork_test.a fork_test.c


运行:

./fork_test.a
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
The father pid=15131 The child pid=15132 num=100
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100
The father pid=15131 The child pid=15132 num=100
The father pid=15131 num=10
The father pid=15131 The child pid=15132 num=100

可以看到产生两个pid(进程)





审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分