Linux中fork的使用(02)---fork的返回值
2015-09-20 15:31
351 查看
环境:Vmware Workstation;CentOS-6.4-x86_64
fork的返回值:
1、fork执行的时候,会有两个返回值,一个是父进程的返回值,一个是子进程的返回值。
2、在父进程中fork的返回值是子进程的PID。
3、在子进程中fork的返回值是0。
4、fork失败,返回值为-1
图解说明:
程序:
1、源文件main.c:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *args[])
{
pid_t id = fork();
if (id > 0)
printf("father id = %d\n", id);
else
printf("son id = %d\n", id);
return 0;
}
2、编译并执行程序:
[negivup@negivup mycode]$ gcc -o main main.c
[negivup@negivup mycode]$ ./main
father id = 12467
[negivup@negivup mycode]$ son id = 0
PS:根据传智播客视频学习整理得出。
fork的返回值:
1、fork执行的时候,会有两个返回值,一个是父进程的返回值,一个是子进程的返回值。
2、在父进程中fork的返回值是子进程的PID。
3、在子进程中fork的返回值是0。
4、fork失败,返回值为-1
图解说明:
程序:
1、源文件main.c:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *args[])
{
pid_t id = fork();
if (id > 0)
printf("father id = %d\n", id);
else
printf("son id = %d\n", id);
return 0;
}
2、编译并执行程序:
[negivup@negivup mycode]$ gcc -o main main.c
[negivup@negivup mycode]$ ./main
father id = 12467
[negivup@negivup mycode]$ son id = 0
PS:根据传智播客视频学习整理得出。
相关文章推荐
- build linux kernel的错误
- Linux Golang安装与环境变量设置
- 1.2Linux下C语言开发基础(学习过程)
- vmware下linux找不到网卡eth0的解决方法
- Linux中的程序和进程,PID和PPID
- Linux各个版本资源下载
- Linux开启mysql远程连接的设置步骤
- V微软S2015下载:开展Win10/Linux/iOS多平台软件
- Linux下执行C语言函数system
- Linux的开机启动过程
- Linux下的目录操作
- php5.4.42 centos5 安装
- 嵌入式Linux系统内核Kernel(裁减移植)启动调试技术(回顾)
- centos 6.4 源更新
- linux查看硬件的命令详解
- shred_linux_unix
- Linux下文件的复制、移动与删除
- 20135328信息安全系统设计基础第一周学习总结(Linux应用)
- Linux终端操作MySQL常用命令
- 【Linux导论】Linux哲学(Linux philosophy)