linux应用程序设计基础--进程控制编程

时间:2022-05-20 10:22:07

theme:进程控制编程

author:Jeff.Xue

contact information:contactmexkj@163.com


1.获取ID

#include <sys/type.h>
#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);

2.创建进程

#include <unistd.h>

pid _t fork(void);//代码共享,数据拷贝,父进程与子进程运行顺序不确定
pid_t vfork(void);//代码数据共享,子进程先执行,父进程后执行

3.exec函数族:exec启动一个新进程代替原有进程(替换代码)包括数据,因此PID不变

#include <unistd.h>

int execl(const char *path,const char *arg1,...)//path:被执行程序名(包含路径),eg:execl了("/bin/ls","ls","-al","/etc/passwd",(char*)0);

int execlp(const char *path,const char *arg1,...)//path不包含路径,从path环境变量里面查找

int execv(const char *path,char *const argv[])

int system(const char *string)//产生子进程执行string命令

4.进程等待

#include <sys/type.h>
#include <sys/wait.h>

pid_t wait(int *status);//阻塞进程,知道某个子进程退出,返回等待进程号