Discussion:
fork() process counting
(too old to reply)
G G
2020-08-04 07:23:47 UTC
Permalink
questions:

when fork() is called it creates a copy of parent process
there exit two processes the parent and the child?

#include <stdio.h>

int main()
{
fork();

return 0;
}


/**********/

#include <stdio.h>

int main()
{
fork(); /* 1 */

fork(); /* 2 */

return 0;
}


fork() 1 creates creates child so
there exist parent and child with copies of the code that follows?
so the child has a fork(),2, and the parent has a fork(),2,

the child executes fork()2 creating a new child
so there now exist the child_parent and child_parent's_new_child
2 processes

AND

the parent executes fork(),2, creating a new child
so there now exist the parent_parent and the parent_parent's_new_child
2 process

so, if asked, how many processes have been created? the answer would be 4?


/***************/


#include <stdio.h>

int main()
{
fork(); /* 1 */

fork(); /* 2 */

fork(); /* 3 */

return 0;
}

fork() 1 creates creates child so
there exist parent and child with copies of the code that follows?
so the child has a fork(),2,and fork(),3. the parent has a fork(),2,
fork(),3

so, after fork() 1 there are 2 processes created? not really right.
the child is the only process being created?

child --- fork()2
\
\-- fork()3


parent --- fork()2
\
\--fork()3


-------------------------------


fork() 2 is executed leaving fork()3 in the code
to be executed

child --- fork()2 ---- child ----- fork()3
\ \
| \--- parent ---- fork()3
|
|
|
|
|
-- fork()2 ---- child ----- fork()3
\
\--- parent ---- fork()3

...

after fork()3 is executed there are 8 processes.

the question, how many process are created? 8?


so 2 raise to the number of forks equals the number
of processes.

ouch!
Rainer Weikusat
2020-08-04 14:11:56 UTC
Permalink
Post by G G
when fork() is called it creates a copy of parent process
there exit two processes the parent and the child?
[...]
Post by G G
#include <stdio.h>
int main()
{
fork(); /* 1 */
fork(); /* 2 */
return 0;
}
fork() 1 creates creates child so
there exist parent and child with copies of the code that follows?
so the child has a fork(),2, and the parent has a fork(),2,
the child executes fork()2 creating a new child
so there now exist the child_parent and child_parent's_new_child
2 processes
AND
the parent executes fork(),2, creating a new child
so there now exist the parent_parent and the parent_parent's_new_child
2 process
so, if asked, how many processes have been created? the answer would be 4?
Not quite. The first fork creates 1 process. The second fork creates two
processes because it's executed both by the original process and the
additional process created by the first fork. This means the total
number of processes terminating after the return 0 is 4. One process
originally created to run the program and 3 additional processes created
in the course of that.
Post by G G
/***************/
#include <stdio.h>
int main()
{
fork(); /* 1 */
fork(); /* 2 */
fork(); /* 3 */
return 0;
}
fork() 1 creates creates child so
there exist parent and child with copies of the code that follows?
so the child has a fork(),2,and fork(),3. the parent has a fork(),2,
fork(),3
Considering this, a total of 4 processes will execute the 3rd fork,
hence, 4 additional processes will be created.

[...]
Post by G G
so 2 raise to the number of forks equals the number
of processes.
ouch!
A classic fork-bomb (I'm not going to quote the shell cuteism) would
look like this:

while (1) fork();

This will obviously overwhelm any system with finite resources quickly.
John A. Zoidberg
2020-08-04 14:17:05 UTC
Permalink
Post by G G
#include <stdio.h>
What made you think to include stdio.h here?
Post by G G
so, if asked, how many processes have been created? the answer would be 4?
Why don't you check it yourself?

#include <sys/types.h>
#include <unistd.h>

int main(void)
{
fork();
fork();
pause();
return 0;
}

$ ps xf | grep forktest
26129 pts/9 S+ 0:00 | \_ /tmp/forktest
26130 pts/9 S+ 0:00 | \_ /tmp/forktest
26132 pts/9 S+ 0:00 | | \_ /tmp/forktest
26131 pts/9 S+ 0:00 | \_ /tmp/forktest
--
The attention span of a computer is only as long as its electrical
cord.
Loading...