How exactly does fork() work?
The following code
#include <stdio.h>
int main (int argc, char const *argv[])
{
printf("Hi\n");
int i;
for(i = 1; i < argc; i++)
{
printf("Argument %d is %s\n", i, argv[i]);
fork();
printf("Forked in for loop increment %d\n", i);
}
return 0;
}
gives the following output
/a.out hello world
Argument 1 is hello
Forked in for loop increment 1
Argument 2 is world
Forked in for loop increment 2
Forked in for loop increment 1
Argument 2 is world
Forked in for loop increment 2
Forked in for loop increment 2
What code does fork execute first, in general. I would like to know the principles of fork() rather than based on just this one example. I could have had multiple arguments on the command line.