linux创建新进程的函数,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 child20b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值