FORK()函数的理解
对于刚刚接触Unix/Linux操作系统,在Linux下编写多进程的人来说,fork是最难理解的概念之一:它执行一次却返回两个值。
本文引用地址:http://www.eepw.com.cn/article/148649.htm首先我们来看下fork函数的原型:
#i nclude
#i nclude
pid_t fork(void);
返回值:
负数:如果出错,则fork()返回-1,此时没有创建新的进程。最初的进程仍然运行。
零:在子进程中,fork()返回0
正数:在负进程中,fork()返回正的子进程的PID
其次我们来看下如何利用fork创建子进程。
创建子进程的样板代码如下所示:
pid_t child;
if((child = fork())0)
/*错误处理*/
else if(child == 0)
/*这是新进程*/
else
/*这是最初的父进程*/
fock函数调用一次却返回两次;向父进程返回子进程的ID,向子进程中返回0,
这是因为父进程可能存在很多过子进程,所以必须通过这个返回的子进程ID来跟踪子进程,
而子进程只有一个父进程,他的ID可以通过getppid取得。
下面我们来对比一下两个例子:
第一个:
#include
#include
int main()
{
pid_t pid;
int count=0;
pid = fork();
printf( This is first time, pid = %dn, pid );
printf( This is secONd time, pid = %dn, pid );
count++;
printf( count = %dn, count );
if ( pid>0 )
{
printf( This is the parent process,the child has the pid:%dn, pid );
}
else if ( !pid )
{
printf( This is the child Process.n)
}
else
{
printf( fork failed.n );
}
printf( This is third time, pid = %dn, pid );
printf( This is fouth time, pid = %dn, pid );
return 0;
}
运行结果如下:
问题:
这个结果很奇怪了,为什么printf的语句执行两次,而那句“count++;”的语句却只执行了一次
接着看:
#include
#include
int main(void)
{
pid_t pid;
int count=0;
pid = fork();
printf( Now, the pid returned by calling fork() is %dn, pid );
if ( pid>0 )
{
printf( This is the parent procESS,the child has the pid:%dn, pid );
printf( In the parent process,count = %dn, count );
}
else if ( !pid )
{
printf( This is the child process.n);
printf( Do your own things here.n );
count ++;
printf( In the child process, count = %dn, count );
}
else
{
printf( fork failed.n );
}
return 0;
}
运行结果如下:
现在来解释上面提出的问题。
看这个程序的时候,头脑中必须首先了解一个概念:在语句pid=fork()之前,只有一个进程在执行这段代码,但在这条语句之后,就变成两个进程在执行了,这两个进程的代码部分完全相同,将要执行的下一条语句都是if ( pid>0 )……。
两个进程中,原先就存在的那个被称作“父进程”,新出现的那个被称作“子进程”。父子进程的区别除了进程标志符(process ID)不同外,变量pid的值也不相同,pid存放的是fork的返回值。fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,它可能有三种不同的返回值:
1. 在父进程中,fork返回新创建子进程的进程ID;
2.在子进程中,fork返回0;
3.如果出现错误,fork返回一个负值;
fork出错可能有两种原因:(1)当前的进程数已经达到了系统规定的上限,这时errno的值被设置为EAGAIN。(2)系统内存不足,这时errno的值被设置为ENOMEM。
接下来我们来看看APUE2中对fork的说明:
The new process created by fork is called the child process. This function is called once but returns twice. The only difference in the returns is that the return value in the child is 0, whereas the return value in the parent is the process ID of the new child. The reason the child"s process ID is returned to the parent is that a process can have more than one child, and there is no function that allows a process to o^ain the process IDs of its children. The reason fork returns 0 to the child is that a process can have only a single parent, and the child can always call getppid to o^ain the process ID of its parent. (Process ID 0 is reserved for use by the kernel, so it"s not possible for 0 to be the process ID of a child.)
被fork创建的新进程叫做自进程。fork函数被调用一次,却两次返回。返回值唯一的区别是在子进程中返回0,而在父进程中返回子进程的pid。在父进程中要返回子进程的pid的原因是父进程可能有不止一个子进程,而一个进程又没有任何函数可以得到他的子进程的pid。
Both the child and the parent continue executing with the instruction that follows the call to fork. The child is a copy of the parent. For example, the child gets a copy of the parent"s data space, heap, and stack. Note that this is a copy for the child; the parent and the child do not share these portions of memory. The parent and the child share the text segment (Section 7.6).
您可能也感兴趣:
为您推荐
8家险企股权被挂牌转让,为何险企股权不再被追捧?
深圳最低工资标准调整为2360元/月 失业保险金为2124元/月
辽宁实施失业保险省级统筹 对缺口核定等作出详细规定
排行
最近更新
- 芜湖释放创新“N次方”效应 数字赋能驱动产业升级
- 去年快递服务全程时限水平有较大提升 干线运输时限缩短
- 住建部:推动长江经济带高质量发展,打造区域协调发展新样板
- 江苏发布重大项目清单 增资扩产项目明显增多
- 山东抓投资抓项目 新兴领域投资规模持续扩大
- Gucci在2月将投放 10 个“SuperGucci”NFT
- 美国银行:美国CBDC将保持美元作为世界储备货币的地位
- 美股三大指数尾盘集体转涨
- 光伏发电概念股有哪些?光伏发电概念龙头股一览
- The new iPad屏幕对比评测
- 电源接通延时器
- 汽车电子稳定系统(ESP)详解
- 联想B520一体机拆解
- 焦炭相关股票有哪些?焦炭概念股票龙头一览
- 增强信号 3G无线上网卡改装拆解全攻略
- 盘点全球五大智能手机生产商 华为联想入围
- 三星新平板 Galaxy Note 10.1全拆解
- 智能监控防盗报警系统
- 格力电器未来三年股东回报规划:每年累计分红不低于当年净利润50%
- 消息称京东科技计划2022年在港IPO,募资10亿至20亿美元
- 恒大集团:呼吁境外债权人不采取任何激进的法律行动
- 海航董事长刘璐因个人原因辞职,在海航已近28年
- 青青稞酒预计2021年营收增长30%-40%,四季度净亏损超1500万
- 2022年在港上市募资10亿美元?京东科技:不予置评
- 财政部修订出台《财政行政处罚听证实施办法》
- 卖房子需要交什么税?卖房子必须携带哪些证件?
- 医保卡的使用范围有哪些?补牙可以使用医保报销吗?
- 小产权房能过户吗?购买小产权房有什么风险?
- 奕东电子的实际控制人是谁?奕东电子股票上市了吗?
- 百合股份是一家什么公司?百合股份的股票何时上市?