/*The Unix fork system call copies the process into memory, gives it a new*/ /*process ID and starts executing it. The problem with this approach is */ /*That the orginal process, called the parent process, and the spawned-off*/ /*process, called the child process are now both running at the same time*/ /*and can produce interleaved indeterminate results*/ /*vfork is like fork, except that it allows the child process to complete*/ /*executing before the parent process*/ #include /*The following are needed for _exit(0) and exit(0), respectively:*/ #include #include int main() { int pid, i; /*vfork instead of fork here:*/ pid = vfork(); /*Some stuff to identify the process..*/ long process; process = getpid(); /*The child process, the pid for the first child process is 0:*/ if (pid == 0){ printf("I am the child process, number %ld\n", process); /*Note that the child process gets its own exit, returning control*/ /*to the parent, called _exit(0)*/ _exit(0); } /*The parent process, the pid here is always the process number:*/ else { printf("I am the parent process, number %ld\n", process); exit(0); } }