当前位置: 技术问答>linux和unix
关于fork()函数返回的问题
来源: 互联网 发布时间:2016-04-11
本文导语: 查资料看linux下fork()函数成功执行后有2个返回值:一个是父进程调用fork后的返回值,其值为刚创建的子进程的ID ;另一个是子进程中fork函数的返回值,其值为0。 编写测试程序 #include #include #include int main(void) { ...
查资料看linux下fork()函数成功执行后有2个返回值:一个是父进程调用fork后的返回值,其值为刚创建的子进程的ID
;另一个是子进程中fork函数的返回值,其值为0。
编写测试程序
#include
#include
#include
int main(void)
{
pid_t pid;
printf("Process Creation Studyn");
pid = fork();
switch(pid) {
case 0:
printf("Child process is runningn");
break;
case -1:
perror("Process creation failedn");
break;
default:
printf("Parent process is runningn");
break;
}
exit(0);
}
程序执行的结果是:
Process Creation Study
Child process is running
Parent process is running
根据打印结果,fork()函数确实返回了0和非0两个值,而且是先返回了0值。我想用gdb单步调试确认这个结果,在第7行设置断点,执行next,然后print pid,发现始终没有0值出现。请问这是为什么?
;另一个是子进程中fork函数的返回值,其值为0。
编写测试程序
#include
#include
#include
int main(void)
{
pid_t pid;
printf("Process Creation Studyn");
pid = fork();
switch(pid) {
case 0:
printf("Child process is runningn");
break;
case -1:
perror("Process creation failedn");
break;
default:
printf("Parent process is runningn");
break;
}
exit(0);
}
程序执行的结果是:
Process Creation Study
Child process is running
Parent process is running
根据打印结果,fork()函数确实返回了0和非0两个值,而且是先返回了0值。我想用gdb单步调试确认这个结果,在第7行设置断点,执行next,然后print pid,发现始终没有0值出现。请问这是为什么?
|
直接gdb只跟父进程走,多进程用gdb很不好调试
|
4.10 Debugging Programs with Multiple Processes
On most systems, gdb has no special support for debugging programs which create additional processes using the fork function. When a program forks, gdb will continue to debug the parent process and the child process will run unimpeded. If you have set a breakpoint in any code which the child then executes, the child will get a SIGTRAP signal which (unless it catches the signal) will cause it to terminate.
However, if you want to debug the child process there is a workaround which isn’t too painful. Put a call to sleep in the code which the child process executes after the fork. It may be useful to sleep only if a certain environment variable is set, or a certain file exists, so that the delay need not occur when you don’t want to run gdb on the child. While the child is sleeping, use the ps program to get its process ID. Then tell gdb (a new invocation of gdb if you are also debugging the parent process) to attach to the child process (see Section 4.7 [Attach], page 30). From that point on you can debug the child process just like any other process which you attached to.
On some systems, gdb provides support for debugging programs that create additional processes using the fork or vfork functions. Currently, the only platforms with this feature are HP-UX (11.x and later only?) and gnu/Linux (kernel version 2.5.60 and later).
By default, when a program forks, gdb will continue to debug the parent process and the child process will run unimpeded.
If you want to follow the child process instead of the parent process, use the command set follow-fork-mode.
On most systems, gdb has no special support for debugging programs which create additional processes using the fork function. When a program forks, gdb will continue to debug the parent process and the child process will run unimpeded. If you have set a breakpoint in any code which the child then executes, the child will get a SIGTRAP signal which (unless it catches the signal) will cause it to terminate.
However, if you want to debug the child process there is a workaround which isn’t too painful. Put a call to sleep in the code which the child process executes after the fork. It may be useful to sleep only if a certain environment variable is set, or a certain file exists, so that the delay need not occur when you don’t want to run gdb on the child. While the child is sleeping, use the ps program to get its process ID. Then tell gdb (a new invocation of gdb if you are also debugging the parent process) to attach to the child process (see Section 4.7 [Attach], page 30). From that point on you can debug the child process just like any other process which you attached to.
On some systems, gdb provides support for debugging programs that create additional processes using the fork or vfork functions. Currently, the only platforms with this feature are HP-UX (11.x and later only?) and gnu/Linux (kernel version 2.5.60 and later).
By default, when a program forks, gdb will continue to debug the parent process and the child process will run unimpeded.
If you want to follow the child process instead of the parent process, use the command set follow-fork-mode.
|
相关的命令如下,详细使用方法可以参考gdb手册,或者看help
set follow-fork-mode
show follow-fork-mode
set detach-on-fork
show detach-on-fork
info forks
delete fork
set follow-fork-mode
show follow-fork-mode
set detach-on-fork
show detach-on-fork
info forks
delete fork
|
4楼的不错,呵呵.
在case分句里让子进程sleep一段时间,然后新开一个terminal,ps一下子进程的pid, 然后在原先的gdb调试环境中输入: gdb program child_process_pid.
在case分句里让子进程sleep一段时间,然后新开一个terminal,ps一下子进程的pid, 然后在原先的gdb调试环境中输入: gdb program child_process_pid.