#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
int
main ()
{
pid_t pid;
pid=fork();
if (pid < 0)
printf("error in fork!");
else if (pid == 0)
{
int i = 1 ;
while(i)
{
sleep(1);
}
pid =10;
printf("i am the child process, my process id is %d/n",getpid());
}
else
{
int i = 1 ;
while(i)
{
sleep(1);
}
pid =1;
printf("i am the parent process, my process id is %d/n",getpid());
}
return 0;
}
调试技巧:
1、运行该程序
2、ps -ef 查到这对父子进程的pid
3、gdb -p pid
4、设定break point 到 sleep(1);
5、set var i=0
6、continue,就可以进入进程调试
重点是要进程进入后
int i = 1 ;
while(i)
{
sleep(1);
}
^_^