I'm trying to get a grasp on socket programming in Linux. The thing is that my unix/linux programming skills aren't exactly what I thought they were. Oh well. I can pick up most everything except the fork() function. Here is the snippet I saw:
Code:
pid_t pid;
switch (pid = fork())
{
case -1:
/* Here pid is -1, the fork failed */
/* Some possible reasons are that you're */
/* out of process slots or virtual memory */
perror("The fork failed!");
break;
case 0:
/* pid of zero is the child */
/* Here we're the child...what should we do? */
/* ... */
/* but after doing it, we should do something like: */
_exit(0);
default:
/* pid greater than zero is parent getting the child's pid */
printf("Child's pid is %d\n",pid);
}
Now I can understand that it splits the one process into two processes. The PID of the child is zero and the PID of the parent is the old PID. Now it gets fuzzy with the switch() statement (which could be an if/else). What happens? Which executes when and why? Do both the parent and child go through the switch() at the same time? Does one go through and the other doesn't?
Please help, thanks