在Linux中fork函数时非常重要的函数,它从已存在进程中创建一个新进程。新进程为子进程,而原进程为父进程。
进程调用fork,当控制转移到内核中的fork代码后,内核做:
当一个进程调用 fork 之后,就有两个二进制代码相同的进程。而且它们都运行到相同的地方。但每个进程都将可以开始它们自己的旅程,看如下程序:
int main( void )
{
pid_t pid;
printf("Before: pid is %d\n", getpid());
if ( (pid=fork()) == -1 )perror("fork()"),exit(1);
printf("After:pid is %d, fork return %d\n", getpid(), pid); sleep(1);
return 0;
}
运行结果:
[root@localhost linux]# ./a.out
Before: pid is 43676
After:pid is 43676, fork return 43677 After:pid is 43677, fork return 0
这里看到了三行输出,一行 before ,两行 after 。进程 43676 先打印 before 消息,然后它有打印 after 。另一个 after消息有43677 打印的。注意到进程 43677 没有打印 before ,为什么呢?如下图所示
所以, fork之前父进程独立执行,fork之后,父子两个执行流分别执行。注意,fork之后,谁先执行完全由调度器决定。
fork()的返回值
通常,父子代码共享,父子再不写入时,数据也是共享的,当任意一方试图写入,便以写时拷贝的方式各自一份副本。具体见下图:
正常终止(可以通过 echo $? 查看进程退出码)
异常退出
#include <unistd.h> void _exit(int status); 参数: status 定义了进程的终止状态,父进程通过 wait 来获取该值
#include <unistd.h> void exit(int status);
exit() 最后也会调用_ exit(), 但在调用_ exit() 之前,还做了其他工作:
实例:
int main()
{
printf("hello");
exit(0);
}
运行结果:
[root@localhost linux]# ./a.out
hello[root@localhost linux]#
int main()
{
printf("hello");
_exit(0);
}
运行结果:
[root@localhost linux]# ./a.out
[root@localhost linux]#
return 是一种更常见的退出进程方法。执行 return n等同于执行exit(n), 因为调用 main 的运行时函数会将 main 的返回值当做exit() 的参数。非main函数的return,表示函数的结束而不是进程的结束。
#include<sys/types.h> #include<sys/wait.h> pid_t wait(int*status); 返回值: 成功返回被等待进程pid ,失败返回 -1 。 参数: 输出型参数,获取子进程退出状态, 不关心则可以设置成为 NULL
pid_ t waitpid(pid_t pid, int *status, int options); 返回值:
参数: pid:
status:
options:
注:
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main( void )
{
pid_t pid;
if ( (pid=fork()) == -1 )
perror("fork"),exit(1);
if ( pid == 0 )
{
sleep(20);
exit(10);
}
else
{
int st;
int ret = wait(&st);
if ( ret > 0 && ( st & 0X7F ) == 0 )
{ // 正常退出
printf("child exit code:%d\n", (st>>8)&0XFF);
}
else if( ret > 0 )
{ // 异常退出
printf("sig code : %d\n", st&0X7F );
}
}
}
进程的阻塞等待方式
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
printf("%s fork error\n",__FUNCTION__);
return 1;
}
else if( pid == 0 )
{
//child
printf("child is run, pid is : %d\n",getpid());
sleep(5);
exit(1);
}
else
{
//father
int status = 0;
pid_t ret = waitpid(-1, &status, 0);//阻塞式等待,等待5S
printf("this is test for wait\n");
if( WIFEXITED(status) && ret == pid )
{
printf("wait child 5s success, child return code is :%d.\n",WEXITSTATUS(status));
}
else
{
printf("wait child failed, return.\n");
return 1;
}
}
return 0;
}
进程的非阻塞等待方式
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
int main()
{
pid_t pid;
pid = fork();
if(pid < 0){
printf("%s fork error\n",__FUNCTION__);
return 1;
}
else if( pid == 0 )
{
//child
printf("child is run, pid is : %d\n",getpid());
sleep(5);
exit(1);
}
else
{
int status = 0;
pid_t ret = 0;
do
{
ret = waitpid(-1, &status, WNOHANG);//非阻塞式等待
if( ret == 0 )
{
printf("child is running\n");
}
sleep(1);
}while(ret == 0);
if( WIFEXITED(status) && ret == pid )
{
printf("wait child 5s success, child return code is :%d.\n",WEXITSTATUS(status));
}
else
{
printf("wait child failed, return.\n");
return 1;
}
}
return 0;
}