printf(getpid());
This may crash the program. printf()
expects its first argument be a string, but getpid()
will return a integer, so this integer will be used as a pointer to an array of character, this very likely leads to a crash, i.e. segmentation fault.
Besides that, what this program does is
fork()
a child process andwait()
- this child process will execuate the same program again
- so it will
fork()
andwait()
- and so on, until your system does not have enough resource to create new process
- then
fork()
will fail, these different level child processes will exit one by one - finally, the first process created by your shell will exit, and the program ends.