获取进程ID
#include <sys/types.h>
#include <unistd.h>
pid_t getpid(void)
//获取本进程ID
pid_t getppid(void)
//获取父进程ID
创建子进程
#include<unistd.h>
pid_t fork(void)
//功能:创建子进程
//fork调用一次,却返回两次,有三种不同的返回值
//1.父进程中,fork返回新创建的子进程的PID
//2.子进程中,fork返回0
//3.如果出现错误,fork返回一个负值
//注意:使用此函数创建的子进程,其数据空间、堆栈空间都会
//从父进程得到一个拷贝,而不是共享
#include <sys/types.h>
#include <unistd.h>
pid_t vfork(void)
//功能:创建子进程
vfork和fork创建子进程的 区别:
- fork: 子进程拷贝父进程的数据段
- vfork: 子进程与父进程共享数据段
- fork: 父、子进程的执行次序不确定
- vfork: 子进程先运行,父进程后运行