获取进程ID
[cpp] view
plaincopy
print?
#include
#include
pid_t getpid(void)
//获取本进程ID
pid_t getppid(void)
//获取父进程ID
创建子进程
[cpp] view
plaincopy
print?
#include
pid_t fork(void)
//功能:创建子进程
//fork调用一次,却返回两次,有三种不同的返回值
//1.父进程中,fork返回新创建的子进程的PID
//2.子进程中,fork返回0
//3.如果出现错误,fork返回一个负值
//注意:使用此函数创建的子进程,其数据空间、堆栈空间都会
//从父进程得到一个拷贝,而不是共享
[cpp] view
plaincopy
print?
#include
#include
pid_t vfork(void)
//功能:创建子进程
vfork和fork创建子进程的区别:
fork: 子进程拷贝父进程的数据段vfork: 子进程与父进程共享数据段(PS: COW介于fork和vfork之间,即有各自的虚拟数据段,但物理数据共享)fork: 父、子进程的执行次序不确定vfork: 子进程先运行,父进程后运行
exec函数族
exec用被执行的程序替换调用它的程序。
与fork区别:fork创建一个新的进程,产生一个新的PID,exec启动一个新程序,替换原有的进程,因此进程的PID不会改变。
execl
[cpp] view
plaincopy
print?
#include
intexecl(constchar*path,constchar*arg1,...)
//参数说明:
//path:被执行的程序名(含完整路径)
//arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束
execlp
[cpp] view
plaincopy
print?
#include
intexeclp(constchar*path,constchar*arg1,...)
//参数说明:
//path:被执行程序名(不含路径,将从path环境变量中查找该程序)
//arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束
execv
[cpp] view
plaincopy
print?
#include
intexecv(constchar*path,char*constargv[])
//参数说明:
//path:被执行程序名(含完整路径)
//argv[]:被执行程序所需的命令行参数数组
system
[cpp] view
plaincopy
print?
#include
intsystem(constchar*string)
//功能:
//调用fork产生子进程,由子进程调用/bin/sh -c string来执行参数string所代表的命令
进程等待
[cpp] view
plaincopy
print?
#include
#include
pid_t wait(int*status)
//功能:阻塞该进程,直到某个子进程退出
转载自:http://blog..net/abclixu123/article/details/8288292