实例代码
1 #include <unistd.h>
2 #include <stdio.h>
3 int main ()
4 {
5 pid_t fpid;
<span style="color:#3333ff;">6 printf("aaaaaaaaaaaaaaaaaa!\n");</span>
7 fpid=fork();
8 printf("bbbbbbbbbbbbbbbbbb!\n");
9 if (fpid < 0)
10 printf("error in fork!\n");
11 else if (fpid == 0) {
<span style="color:#3333ff;"> 12 printf("i am the child process, my process id is %d\n",getpid());</span>
13 } else {
<span style="color:#3333ff;"> 14 printf("i am the parent process, my process id is %d, my child process id is %d\n",getpid(), fpid);</span>
15 }
16 return 0;
17 }
fork多进程调试一般有一下3种方法:
1. follow-fork-mode
用法:set follow-fork-mode [parent|child]
进入gdb后,直接设置,默认是parent
所以如果想要调试子进程,进入gdb后设置set follow-fork-mode child,然后设置子进程的断点
可用使用show follow-fork-mode 来查询当前fork模式