if(fork) and if(!fork) - how do they work?
Let's say we have the following code:
How many A's, B's and C's are going to be printed?
I'm a little bit confused with how fork() works inside "if" and whether I'm in parent or child process.
8 Replies
I think you are on the wrong discord my friend
this is the C# discord. Your code is... C? C++? regardless, head to $cpp
We're partnered with Together C & C++, check them out here: https://discord.gg/vnyVmAE
wrong server.. im so sorry lol
no worries
thanks for letting me know c:
assuming c/c++, treating a number as a boolean is false if the number is 0 and true if the number is anything other than 0
so fork() in an if is just checking if the PID is nonzero
dunno about inverting it with ! though, that looks weird and i wouldn't do it to begin with
is that even valid?
yeah it is
when you
fork()
you spawn another process
so in father process fork() will return false
in child process it will return true
(or reverse, i haven't used this stuff in years)