Welcome 微信登录

首页 / 操作系统 / Linux / Linux fork函数创建一个新进程

在Linux中创建一个新进程的唯一方法是使用fork函数,fork()执行一次但有两个返回值。在父进程中,返回值是子进程的进程号;在子进程中,返回值为0。因此可通过返回值来判断当前进程是父进程还是子进程。使用fork函数得到的子进程是父进程的一个复制品,它从父进程处复制了整个进程的地址空间,包括进程上下文,进程堆栈,内存信息,打开的文件描述符,信号控制设定,进程优先级,进程组号,当前工作目录,根目录,资源限制,控制终端等。而子进程所独有的只是它的进程号,资源使用和计时器等。可以看出,使用fork函数的代价是很大的,它复制了父进程中的代码段,数据段和堆栈段里的大部分内容,使得fork函数的执行速度并不快。头文件:#include <unistd.h>函数定义:int fork( void );返回值:子进程中返回0,父进程中返回子进程ID,出错返回-1一个简单的fork程序:#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(){pid_t id; //定义一个进程号变量int i=0;
printf("start fork ");id = fork(); //调用fork函数新建一个进程i ++;printf("end fork ");
//判断当前进程if(id < 0){ //出错 perror("fork failed ");exit(1);}else if(id == 0){ //子进程printf("In child ");printf("i = %d ", i++);exit(0);}else{ //父进程printf("In father ");printf("i = %d ", i++);exit(0);}return 0;}运行结果:
[root@localhost Process]# ./fork
start fork
end fork
In child
i = 1
end fork
In father
i = 1
可知:
1.子进程是从调用fork函数处的下一条语句开始执行的。
2.子进程中的局部变量i不同于父进程中的i,是父进程的复制。
下面写一个小实验(包括了fork, exec, waitpid等函数的使用):
实验要求:
该实验有3个进程,其中一个为父进程,其余两个是该父进程创建的子进程,其中一个子进程运行“ls -l”指令,另一个子进程在暂停5s之后异常退出,父进程并不阻塞自己,并等待子进程的退出信息,带收集到该信息,父进程就返回。
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(){
pid_t child1, child2, child;
child1 = fork(); //创建子进程1
child2 = fork(); //创建子进程2
if(child1 < 0){
perror("fork child1 failed ");
exit(1);
}
else if(child1 == 0){
printf("In child1: execute "ls -l" "); //子进程1执行"ls -l"
if(execlp("ls", "ls", "-l", NULL) < 0){
perror("execlp failed ");
}
}
if(child2 < 0){
perror("fork child2 failed ");
exit(1);
}
else if(child2 == 0){
printf("In child2: sleep for 5 sec and exit ");
sleep(5); //子进程2睡眠5s
exit(1);
}
  else{
printf("In father process: ");
do{ //父进程每隔1s接受一次子进程2的信号
child = waitpid(child2, NULL, WNOHANG);
if(child == 0){
printf("The child2 process has not exited! ");
sleep(1);
}
}while( child == 0);
if(child == child2){ //接受到信号
printf("Get child2 ");
}
else{
perror("Error ");
}
}
return 0;
}
运行结果:
[root@localhost Process]# ./exp1
In child1: execute "ls -l"
In child1: execute "ls -l"
In child2: sleep for 5 sec and exit
In father process:
The child2 process has not exited!
总计 15
-rwxrwxrwx 1 root root 5903 02-12 19:44 dameon
-rwxrwxrwx 1 root root  812 02-12 19:43 dameon.c
-rwxrwxrwx 1 root root 5712 2011-02-12 exp1
-rwxrwxrwx 1 root root 1024 2011-02-12 exp1.c
-rwxrwxrwx 1 root root 5300 02-12 22:19 fork
-rwxrwxrwx 1 root root  508 02-12 22:19 fork.c
-rwxrwxrwx 1 root root  510 02-12 22:18 fork.c.bak
-rwxrwxrwx 1 root root 6026 02-12 19:57 sys_dameon
-rwxrwxrwx 1 root root  919 02-12 19:57 sys_dameon.c
总计 15
-rwxrwxrwx 1 root root 5903 02-12 19:44 dameon
-rwxrwxrwx 1 root root  812 02-12 19:43 dameon.c
-rwxrwxrwx 1 root root 5712 2011-02-12 exp1
-rwxrwxrwx 1 root root 1024 2011-02-12 exp1.c
-rwxrwxrwx 1 root root 5300 02-12 22:19 fork
-rwxrwxrwx 1 root root  508 02-12 22:19 fork.c
-rwxrwxrwx 1 root root  510 02-12 22:18 fork.c.bak
-rwxrwxrwx 1 root root 6026 02-12 19:57 sys_dameon
-rwxrwxrwx 1 root root  919 02-12 19:57 sys_dameon.c
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
Get child2