If fork() is successful, the return value is 0 in the child and > 0 (actually the PID of the child) in the parent. An example of how to do this might look something like this:
Code:
pid_t ret = fork();
if (ret == -1) {
// no child process created - handle error
perror("fork()");
exit(EXIT_FAILURE);
} else if (ret == 0) {
// this code is executed in the child process
while (1) {
printf("This is the child process\n");
sleep(1);
}
} else {
// this code is executed in the parent process
while (1) {
printf("This is the parent process (our child has pid %d)\n", ret);
sleep(1);
}
}
In other words, if fork() is successful then both parent and child are executing concurrently (conceptually, at least...) and a program can differentiate between whether it is the child or parent by examining the return value of fork().