fork函数创建一个新进程
2011-02-12 22:31
429 查看
在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/n");
id = fork(); //调用fork函数新建一个进程
i ++;
printf("end fork/n");
//判断当前进程
if(id < 0){ //出错
perror("fork failed/n");
exit(1);
}
else if(id == 0){ //子进程
printf("In child/n");
printf("i = %d/n", i++);
exit(0);
}
else{ //父进程
printf("In father/n");
printf("i = %d/n", 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/n");
exit(1);
}
else if(child1 == 0){
printf("In child1: execute 'ls -l'/n"); //子进程1执行"ls -l"
if(execlp("ls", "ls", "-l", NULL) < 0){
perror("execlp failed/n");
}
}
if(child2 < 0){
perror("fork child2 failed/n");
exit(1);
}
else if(child2 == 0){
printf("In child2: sleep for 5 sec and exit/n");
sleep(5); //子进程2睡眠5s
exit(1);
}
else{
printf("In father process:/n");
do{ //父进程每隔1s接受一次子进程2的信号
child = waitpid(child2, NULL, WNOHANG);
if(child == 0){
printf("The child2 process has not exited!/n");
sleep(1);
}
}while( child == 0);
if(child == child2){ //接受到信号
printf("Get child2/n");
}
else{
perror("Error/n");
}
}
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
在父进程中,返回值是子进程的进程号;在子进程中,返回值为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/n");
id = fork(); //调用fork函数新建一个进程
i ++;
printf("end fork/n");
//判断当前进程
if(id < 0){ //出错
perror("fork failed/n");
exit(1);
}
else if(id == 0){ //子进程
printf("In child/n");
printf("i = %d/n", i++);
exit(0);
}
else{ //父进程
printf("In father/n");
printf("i = %d/n", 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/n");
exit(1);
}
else if(child1 == 0){
printf("In child1: execute 'ls -l'/n"); //子进程1执行"ls -l"
if(execlp("ls", "ls", "-l", NULL) < 0){
perror("execlp failed/n");
}
}
if(child2 < 0){
perror("fork child2 failed/n");
exit(1);
}
else if(child2 == 0){
printf("In child2: sleep for 5 sec and exit/n");
sleep(5); //子进程2睡眠5s
exit(1);
}
else{
printf("In father process:/n");
do{ //父进程每隔1s接受一次子进程2的信号
child = waitpid(child2, NULL, WNOHANG);
if(child == 0){
printf("The child2 process has not exited!/n");
sleep(1);
}
}while( child == 0);
if(child == child2){ //接受到信号
printf("Get child2/n");
}
else{
perror("Error/n");
}
}
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
相关文章推荐
- Linux中使用C语言的fork()函数创建子进程的实例教程
- LInux 子进程创建函数fork简介
- Linux 内核--fork()函数创建进程 (续)之copy_mem(int nr, struct task_struct *p)
- Linux下进程创建Fork()函数分析
- fork函数创建进程
- linux c之创建进程fork和vfork函数之间的区别
- Linux 内核--fork()函数创建进程 (续)之copy_mem(int nr, struct task_struct *p)
- [Chapter 3 Process]Practice 3.1 相关知识:进程创建、fork函数
- Linux进程的创建函数fork()及其fork内核实现解析
- Linux 内核--fork()函数创建进程 (续)之copy_mem(int nr, struct task_struct *p)
- fork()创建子进程步骤、函数用法及常见考点(内附fork()过程图)
- 进程创建函数fork和vfork的区别
- 浅谈进程的创建函数fork及vfork
- 创建进程 fork()函数的基本使用 父子进程之间的关系
- fork函数的返回值实际只有一个值,看似两个值是因为在不同的进程中返回
- 进程创建函数fork()、vfork()
- 有意思的进程创建函数fork() && fork() || fork() .
- Linux0.11内核--fork()函数创建进程
- 进程的创建过程(fork函数)
- 进程的创建 —— do_fork()函数详解