创建进程fork()函数使用
来源:互联网 发布:小说阅读软件哪个好 编辑:程序博客网 时间:2024/06/04 17:46
两次返回的区别是子进程的返回值是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()函数使用
- fork函数创建进程
- 创建进程-fork函数
- 进程创建函数fork()、vfork()
- 进程创建,fork()函数讲解
- Linux进程创建fork()函数
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- linux系统如何使用fork函数创建子进程
- 有意思的进程创建函数fork() && fork() || fork() .
- fork函数创建一个新进程
- Linux0.11进程创建函数fork()
- Linux0.11内核--fork()函数创建进程
- fork()函数-创建子进程
- git 学习 - 单机操作
- snprintf函数使用说明
- VS2010 语法错误: 标识符“__RPC__out_xcount_part” 解决方法
- Windows的消息定义大全
- Object转byte[];byte[]转Object
- 创建进程fork()函数使用
- 我的一天我做主(Enjoy)- 1
- Oracle中Decode()函数使用技巧
- 插入排序
- java中byte数组与int类型的转换(两种方式)
- VC编译开关选项分析(debug和release版本的对比)
- C++字符串长度
- 欧拉道路(回路)的判断
- Matlab的二值化问题