1. 创建进程fork

fork创建一个子进程,给新进程分配资源(数据和代码空间)

#include <unistd.h>

pid_t fork(void);

函数返回值,给子进程返回0,给父进程返回子进程的ID,

  • 0 to the child process

  • the process ID of the child process to the parent process.

  • -1 to the parent process, no child process is created

fork运行原理如下:

image-20230524091439334

2. vfork

vfork,创建一个子进程,并阻塞父进程(block parent),

#include <unistd.h>

pid_t vfork(void);

fork与vfock区别,

  • vfork creates a child process and block parent (the calling thread is suspended until the child terminates)
  • vfork() is a special case of clone(2). It is used to create new processes without copying the page tables of the parent process. It may be useful in performance-sensitive applications where a child is created which then immediately issues an execve(2).
本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2023-05-24 11:45

results matching ""

    No results matching ""