创建进程fork()函数使用
2011-08-08 16:00
441 查看
由f o r k函数创建的新进程被称为子进程.该函数被调用一次,但返回两次。
两次返回的区别是子进程的返回值是0,而父进程的返回值则是新子进程的进程I D。
一般的来说,在f o r k之后是父进程先执行还是子进程先执行是不确定的。这取决于内核所使用的调度算法。
1 #include <unistd.h>
2 #include <sys/types.h>
3 void main(void)
4 {
5 pid_t pid;
6
7 pid=fork();
8
9 if(pid < 0)
10 {
11 printf("error in fork!\n");
12 }
13 else if(pid == 0)
14 {
15 printf("in child return pid value is = %d\n",(int)pid);
16 printf("i am the child process,my process id is %d\n",getpid());
17 printf("i am the child process,my parent id is %d\n",getppid());
18 }
19 else
20 {
21 printf("in father return pid value is = %d\n",(int)pid);
22 printf("i am the parent process,my process id is %d\n",getpid());
23 printf("i am the parent process,my parent id is %d\n",getppid());
24 }
25 }
结果:
1,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1293
i am the parent process,my process id is 1292
i am the parent process,my parent id is 1229
root@darkstar:/home/zhangl/unixtest/chapter8# in child return pid value is = 0
i am the child process,my process id is 1293
i am the child process,my parent id is 1
2,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1328
i am the parent process,my process id is 1327
i am the parent process,my parent id is 1301
in child return pid value is = 0
i am the child process,my process id is 1328
i am the child process,my parent id is 1
3,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1373
in child return pid value is = 0
i am the child process,my process id is 1373
i am the child process,my parent id is 1372
i am the parent process,my process id is 1372
i am the parent process,my parent id is 1354
两次返回的区别是子进程的返回值是0,而父进程的返回值则是新子进程的进程I D。
一般的来说,在f o r k之后是父进程先执行还是子进程先执行是不确定的。这取决于内核所使用的调度算法。
1 #include <unistd.h>
2 #include <sys/types.h>
3 void main(void)
4 {
5 pid_t pid;
6
7 pid=fork();
8
9 if(pid < 0)
10 {
11 printf("error in fork!\n");
12 }
13 else if(pid == 0)
14 {
15 printf("in child return pid value is = %d\n",(int)pid);
16 printf("i am the child process,my process id is %d\n",getpid());
17 printf("i am the child process,my parent id is %d\n",getppid());
18 }
19 else
20 {
21 printf("in father return pid value is = %d\n",(int)pid);
22 printf("i am the parent process,my process id is %d\n",getpid());
23 printf("i am the parent process,my parent id is %d\n",getppid());
24 }
25 }
结果:
1,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1293
i am the parent process,my process id is 1292
i am the parent process,my parent id is 1229
root@darkstar:/home/zhangl/unixtest/chapter8# in child return pid value is = 0
i am the child process,my process id is 1293
i am the child process,my parent id is 1
2,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1328
i am the parent process,my process id is 1327
i am the parent process,my parent id is 1301
in child return pid value is = 0
i am the child process,my process id is 1328
i am the child process,my parent id is 1
3,root@darkstar:/home/zhangl/unixtest/chapter8# ./testfork
in father return pid value is = 1373
in child return pid value is = 0
i am the child process,my process id is 1373
i am the child process,my parent id is 1372
i am the parent process,my process id is 1372
i am the parent process,my parent id is 1354
相关文章推荐
- 创建进程 fork()函数的基本使用 父子进程之间的关系
- [Linux进程]使用fork函数创建进程
- [Linux进程]使用fork函数创建多个子进程
- Linux中使用C语言的fork()函数创建子进程的实例教程
- 使用C语言的fork()函数在Linux中创建进程的实例讲解
- Linux进程的创建函数fork()及其fork内核实现解析
- wait函数在fork创建子进程后的使用
- Linux 内核--fork()函数创建进程 (续)之copy_mem(int nr, struct task_struct *p)
- Linux0.11内核--fork()函数创建进程
- Linux 内核--fork()函数创建进程 (续)之copy_mem(int nr, struct task_struct *p)
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识
- 简单掌握Linux系统中fork()函数创建子进程的用法
- Linux下的多进程编程——fork(),exec()等函数的使用
- 用fork创建进程时,在子进程中 使用getppid 得到的返回值为啥总是1
- 进程的创建 —— do_fork()函数详解
- wait函数在fork创建子进程后的使用
- 通过fork函数创建进程的跟踪,分析linux内核进程的创建
- 进程创建函数fork和vfork的区别
- linux c之创建进程fork和vfork函数之间的区别
- 进程创建函数fork和vfork的区别