[英]how fork works with logical operators
main()
{
if (fork() || (fork() && fork()))
printf("AA\n");
else if (!fork())
printf("BB\n");
else
printf("CC\n");
}
I have run the following code and get the results AA AA CC BB CC BB. 我已经运行了以下代码,并获得了结果AA AA CC CC BB CC BB。 While I understand how fork works, I don't understand what it does with logical operators.
虽然我了解fork的工作原理,但我不了解它对逻辑运算符的作用。 The teacher in our class wants us to give the answers for this homework.
我们班上的老师要我们给这个作业做答案。 While I can easily run this program, I would like to know what happens exactly.
虽然我可以轻松运行该程序,但我想确切地了解会发生什么。 Can anyone explain or direct me to a website to what happens when using fork with logical operators.
任何人都可以向我解释或指导我进入网站,以了解将fork与逻辑运算符一起使用时会发生什么。
I am pretty new to c/c++ so go easy on me. 我是C / C ++的新手,所以对我轻松一点。 Thanks
谢谢
fork()
returns 0
(false) to the child process, and non-zero (true) to the parent process. fork()
向子进程返回0
(false),向父进程返回非零(true)。
You can apply logical operators to these booleans. 您可以将逻辑运算符应用于这些布尔值。
Remember that logical operators will short-circuit, so 0 || fork()
请记住,逻辑运算符会短路,因此
0 || fork()
0 || fork()
will not call fork
at all. 0 || fork()
根本不会调用fork
。
If you read carefully through the code and think about what each fork()
call will return, you should be able to figure it out. 如果您仔细阅读代码并考虑每个
fork()
调用将返回什么,则应该可以弄清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.