#include < stdio.h >
#include < unistd.h >
int main ()
... {
if (fork () = = 0)
... {
printf ("Child Process say hello to you!");
Sleep (5);
Exit (0);
}
Else
printf ("Parent Process, Hello");
return 0;
}
1: You typically use GDB to debug a subprocess by inserting the sleep () statement in a subprocess. Run GDB to debug this program, wait until the child process is fork, get its process number, and then run GDB at the other terminal, with this process number attach on the child process, we can debug the child process. GDB childID (gdb) Attach childID
2: Another way is to set Follow-fork-mode as a child in GDB, which is used in the form of "set follow-fork-mode Child". The default GDB tracks the parent process. 3: Invoke the subroutine through System (): Method 1 applies. If the source files of the subroutine and the source files of the parent program are not shared, that is, you cannot set a breakpoint on the subroutine after the parent program, and you will not have the opportunity to set breakpoints after using Method 2, and you cannot debug. After using Method 2, it seems impossible to attach the child process.