获取进程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) //功能:建立子进程
exec函数族函数
exec用被执行的程序替换调用它的程序。命令行
与fork区别:fork建立一个新的进程,产生一个新的PID,exec启动一个新程序,替换原有的进程,所以进程的PID不会改变。指针
execlcode
#include <unistd.h> int execl(const char *path, const char *arg1,...) //参数说明: //path:被执行的程序名(含完整路径) //arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束
#include <unistd.h> int execlp(const char *path, const char *arg1,...) //参数说明: //path:被执行程序名(不含路径,将从path环境变量中查找该程序) //arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束
#include <unistd.h> int execv(const char *path, char *const argv[]) //参数说明: //path:被执行程序名(含完整路径) //argv[]:被执行程序所需的命令行参数数组
#include <stdlib.h> int system(const char *string) //功能: //调用fork产生子进程,由子进程调用/bin/sh -c string来执行参数string所表明的命令
#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status) //功能:阻塞该进程,直到某个子进程退出