linux使用fork()来创建一个新进程,fork函数将运行着的进程分裂出另一个子进程,它通过拷贝父进程的方式创建子进程。子进程与父进程有相同的代码空间、文件描述符等资源。
进程创建后,子进程与父进程开始并发执行,执行顺序由内核调度算法来决定。
fork()函数如果成功创建了进程,就会对父子进程各返回一次,其中对父进程返回子进程的 PID,对子进程返回 0;失败则返回小于 0 的错误码。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
pid_t pid;
pid = fork(); //创建进程
if (pid == 0) { //对子进程返回0
printf("Here is child, my pid = %d, parent's pid = %d\n", getpid(), getppid());
exit(0);
} else if(pid > 0) { //对父进程返回子进程的PID
printf("Here is parent, my pid = %d, child's pid = %d\n", getpid(), pid);
} else {
perror("fork error\n");
}
return 0;
}
exec 族函数
exec 族函数用来修改当前进程的代码空间。
在子进程中调用 exec 族函数将它的代码段完全替换为新的程序,并从这个新进程的 main 函数开始执行。exec执行示意图如下:
调用 exec 族函数并不创建进程,因此前后进程的 ID 并无改变,exec 只是用一个全新的程序替换掉当前进程代码空间里的内容。
exec 族函数有 6 个不同的 exec 函数,函数原型分别如下:
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);
exec 函数簇成功执行后,原有的程序代码都将被指定的文件或脚本覆盖,因此这些函数一旦成功后面的代码是无法执行的,他们也是无法返回的。
下面展示子进程被创建出来之后执行的代码,以及如何加载这个指定的程序。
被子进程加载的示例代码child_elf.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("[%d]: yep, I am the child\n", (int)getpid());
exit(0);
}
下面是使用 exec 函数簇中的 execl 来让子进程加载上述代码的示例exec.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
pid_t x;
x = fork();
if(x > 0)
{
printf("[%d]: I am the parent\n", (int)getpid());
exit(0);
}
if(x == 0)
{
printf("[%d]: I am the child\n", (int)getpid());
execl("./child_elf", "child_elf", NULL);
printf("NEVER be printed\n");
}
return 0;
}
wait()函数
wait()函数用来帮助父进程获取其子进程的退出状态。当进程退出时,内核为每一个进程保存了一定量的退出状态信息,父进程可根据此退出信息来判断子进程的运行状况。如果父进程未调用 wait()函数,则子进程的退出信息将一直保存在内存中。
由于进程终止的异步性,可能会出现子进程先终止或者父进程先终止的情况,从而出现两种特殊的进程:
1.僵尸进程:如果子进程先终止,但其父进程没有为它调用 wait()函数,那么该子进程就会变为僵尸进程。僵尸进程在它的父进程为它调用 wait()函数之前将一直占有系统的内存资源。
2.孤儿进程:如果父进程先终止,尚未终止的子进程将会变成孤儿进程。孤儿进程将直接被 init 进程收管,由 init 进程负责收集它们的退出状态。
vmuser@vmuser-virtual-machine:~/workspace/test$ cat child_elf.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("[%d]: yep, I am the child\n", (int)getpid());
#ifdef ABORT
abort();//自己给自己发送一个致命信号 SIGABRT,自杀
#else
exit(7);//正常退出,且退出值为7
#endif
}
wait函数调用代码
vmuser@vmuser-virtual-machine:~/workspace/test$ cat wait.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
pid_t x = fork();
if(x == 0) //子进程,执行指定程序 child_elf
{
execl("./child_elf", "child_elf", NULL);
}
if(x > 0) //父进程,使用 wait( )阻塞等待子进程的退出
{
int status;
wait(&status);
if(WIFEXITED(status)) //判断子进程是否正常退出
{
printf("child exit normally, "
"exit value: %hhu\n", WEXITSTATUS(status));
}
if(WIFSIGNALED(status)) //判断子进程是否被信号杀死
{
printf("child killed by signal: %u\n",
WTERMSIG(status));
}
}
return 0;
}
测试结果
PS
gcc的-D,-w,-W,-Wall,-O3这些参数的意义
gcc child_elf.c -o child_elf -DABORT -w
-D添加宏定义
-w关闭所有警告信息
-W和-Wall开启所有警告,一般一起使用
-O3意思是开启编译优化,等级为三