Fork()

does fork() spawn only the parent process, what if fork() is looped, does it spawn the parent and the child?

fork forks a process, so the process which called fork is parent, and which got forked is child.

when you have a loop, then the child will again call fork, by which it will become parent of another process which gets forked by the fork.

Finally, not only forking is possible in parent. any process can call fork, and it gets a child process forked for it.

If this does not explains you what you wanted, then make the question more clear please.

you've answered 50% of it, if a parent is forked, then fork is called again, will both the parent and the child be forked or only the child, in other words will both of them be forking in parallel?

fork()
PID1 -> PID2
then fork() again
PID1->PID3
PID2->PID4
?

Yes, you are right.

An example to clarify the matter better,

use strict;
use warnings;

print "PARENT 1: $$\n";
my $pid = fork ();
if ( $pid )  {
    print "Parent of 1st fork $$\n";
} else {
    print "Child of 1st fork $$\n";
}

my $next_pid = fork ();

if ( $next_pid )  {
    print "Parent of next fork $$\n";
} else {
    print "Child of next fork $$\n";
}

And its output,

PARENT 1: 27844
Child of 1st fork 27845
Child of next fork 27846
Parent of next fork 27845
Parent of 1st fork 27844
Child of next fork 27847
Parent of next fork 27844

It is so clear that, the first process id is: 27844, which gets forked into another as: 27845.

Then the next is two parents: 27844, 27845 which gets two childs as: 27846, 27847.

now it's very clear and 100% answers my question, thanks.