I am running XCODE on Max OS X 10.6.6 under VMWare on a PC. Generally everything works wells. I was playing with a simple program that creates a child process using the fork() system call. When I run the program with break points off everything runs as expected. However, if I set a breakpoint after line 2 (pid = fork()
executes, I get the following output and no prompt for a carriage return. It seems like the wait(NULL) doesn't wait on the child when I break after the fork(). Any clues on why this is happening?
Output:
I am the parent process!
Child Complete
I am child or parent!
Expected output:
I am the parent process!
I am the child process!
<file names in directory>
Child Complete
I am child or parent!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
printf ("I am the child process!\n");
getc(stdin);
execlp("/bin/ls", "ls", NULL);
printf ("I am the child process again!\n");
}
else { /* parent process */
printf ("I am the parent process!\n");
/* parent will wait for the child to complete */
wait (NULL);
printf ("\nChild Complete\n");
//exit(0);
}
printf ("I am child or parent!\n");
}
Output:
I am the parent process!
Child Complete
I am child or parent!
Expected output:
I am the parent process!
I am the child process!
<file names in directory>
Child Complete
I am child or parent!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
printf ("I am the child process!\n");
getc(stdin);
execlp("/bin/ls", "ls", NULL);
printf ("I am the child process again!\n");
}
else { /* parent process */
printf ("I am the parent process!\n");
/* parent will wait for the child to complete */
wait (NULL);
printf ("\nChild Complete\n");
//exit(0);
}
printf ("I am child or parent!\n");
}