LINUX系统编程学习笔记
exec 子程序跳转,wait、waitpid 回收子程序
1. exec 函数族
1.1 exec 作用
fork创建子进程后执行的是和父进程相同的程序(可能靠if判断pid执行不同的分支结构),exec函数则能让子进程执行另一个程序。当进程调用exec函数时,该进程的用户空间代码和数据完全被新程序替换,从新程序的启动例程开始执行。exec不会创建新进程,所以调用exec前后进程id并没有发生变化
将当前进程的.text、.data替换为所要加载的程序的 .text、.data,然后让进程从新的.text第一条指令开始执行,但进程 id 不变, 换核不换壳
说人话就是:exec跳转另一个.out程序且不返回!!!
1.2 exec 函数族头文件包含及声明
man exec
#include<unistd.h>
int execl(const char* path, const char* arg, ... , NULL); // 自己定义文件路径,(绝对路径/相对路径)
int execlp(const char* file, const char* arg, ... , NULL); // 在环境变量 PATH 中寻找 file 进行跳转
int execv(const char* path, char* const argv[] , ... , NULL);
int execvp(const char* file, char* const argv[] , ... , NULL);
exec函数一旦调用成功即执行新的程序,不返回。只有失败才返回,错误值-1,所以通常直接在exec函数后面进行 perror 和 exit 。
1.3 exec 函数跳转 demo
fork 创建子进程,子进程通过 exec 跳出实现 ls -l 打印的功能 或者 跳转到自己编写的程序(execl_test.out)当中
/* fork_execl.c */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
int main(int argc, char *argv[])
{
pid_t pid = fork();
if(pid == -1)
{
perror("fork error");
exit(0);
}else if(pid == 0)
{
//execlp("ls","ls", "-l", "-h", NULL); //给agrv[0] 传参
execl("./execl_test.out","./execl_test.out", NULL); // 跳转到自己编译的程序中去
execl("/bin/ls","ls", "-l", NULL); // 打印 ll 命令
perror("exec error");
exit(1);
}else if(pid > 0)
{
sleep(1);
printf("I'm parent:%d\n", getpid());
}
return 0;
}
实现 ls -l 功能:
自己编写的 execl_test.c 程序,编译。
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int fd;
fd = open("ps.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
if(fd<0)
{