谷动谷力

 找回密码
 立即注册
谷动谷力 首页 嵌入式 查看内容

【Linux应用开发】fork()函数详解

2023-8-14 21:20| 发布者: sunsili| 查看: 16| 评论: 1|原作者: sunsili

摘要: 【Linux应用开发】fork()函数详解 1.fork()简介 函数原型: pid_t fork(void);//pid_t为int类型,进行了重载 pid_t getpid();// 获取当前进程的 pid 值。 pid_t getppid(); //获取当前进程的父进程 pid 值。 用 .. ...
【Linux应用开发】fork()函数详解


1.fork()简介

函数原型:
pid_t fork(void);//pid_t为int类型,进行了重载
pid_t getpid();// 获取当前进程的 pid 值。
pid_t getppid(); //获取当前进程的父进程 pid 值。
用于创建一个进程,所创建的进程复制父进程的代码段/数据段/BSS段/堆/栈等所有用户空间信息;在内核中操作系统重新为其申请了一个PCB,并使用父进程的PCB进行初始化;
fork.png
如图所示 :我们将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()有个大致了解了,让我们来看个例题:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>

  4. int main(int argc, const char *argv[])
  5. {
  6.     int num = 10;
  7.     pid_t pid = fork();
  8.     if(pid==0)
  9.     {
  10.         while (1)
  11.         {
  12.             num = 100;
  13.            printf("The father pid=%d The child pid=%d num=%d\n", getppid(),getpid(), num);
  14.            sleep(3);
  15.         }
  16.     }
  17.     else
  18.     {
  19.         while (1)
  20.         {
  21.             printf("The father pid=%d num=%d\n", getpid(), num);
  22.             sleep(5);
  23.         }
  24.     }

  25.     return 0;
  26. }
复制代码
保存为fork_test.c
  1. 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(进程)


鲜花

握手

雷人

路过

鸡蛋

相关阅读

发表评论

最新评论

sunsili 2023-8-14 21:36
本帖最后由 sunsili 于 2023-8-14 21:37 编辑

编译时遇到了错误:
gcc -o fork_test.a fork_test.c
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
分析要链接动态库
用命令:
gcc -shared -g -o fork_test.a fork_test.c
/usr/bin/ld: /tmp/cc1wgtVD.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/cc1wgtVD.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
但又出现问题, 按提示要加参数 fPIC的, 但我没用
重新用原始命令编译:
gcc -o fork_test.a fork_test.c
又OK了

查看全部评论(1)

最新热点

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-4-12 09:13 , Processed in 0.100336 second(s), 37 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

返回顶部