设为首页 加入收藏

TOP

Linux fork函数创建一个新进程
2014-11-24 07:48:46 来源: 作者: 【 】 浏览:0
Tags:Linux fork 函数 创建 一个 进程

在Linux中创建一个新进程的唯一方法是使用fork函数,fork()执行一次但有两个返回值。


在父进程中,返回值是子进程的进程号;在子进程中,返回值为0。因此可通过返回值来判断当前进程是父进程还是子进程。


使用fork函数得到的子进程是父进程的一个复制品,它从父进程处复制了整个进程的地址空间,包括进程上下文,进程堆栈,内存信息,打开的文件描述符,信号控制设定,进程优先级,进程组号,当前工作目录,根目录,资源限制,控制终端等。而子进程所独有的只是它的进程号,资源使用和计时器等。可以看出,使用fork函数的代价是很大的,它复制了父进程中的代码段,数据段和堆栈段里的大部分内容,使得fork函数的执行速度并不快。


头文件:


#include


函数定义:


int fork( void );


返回值:


子进程中返回0,父进程中返回子进程ID,出错返回-1


一个简单的fork程序:


#include


#include


#include


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
#include
#include
#include
#include
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++ IDE开发工具 下一篇GCC 4.5.1交叉编译器构建过程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·JAVA现在的就业环境 (2025-12-26 01:19:24)
·最好的java反编译工 (2025-12-26 01:19:21)
·预测一下2025年Java (2025-12-26 01:19:19)
·Libevent C++ 高并发 (2025-12-26 00:49:30)
·C++ dll 设计接口时 (2025-12-26 00:49:28)