红联Linux门户
Linux帮助

父子进程都调用getpid,为什么子进程追踪不到?

发布时间:2009-11-30 19:02:34来源:红联作者:jipingwt
[i=s] 本帖最后由 jipingwt 于 2009-11-30 19:07 编辑 [/i]

我写了一个小程序(test.c),如下:
#include
#include
#include
#include

int main()
{
int i;
pid_t pid,p;

if((pid=fork())==0){
sleep(3);
p=getpid();
printf("%ld\n",p);
exit(0);
}
else if(pid<0){
printf("0error");
exit(1);
}
p=getpid();
printf("%ld\n",p);
wait(&i);
return 0;
}

编译后运行能得到父子进程的pid号,但奇怪的是用strace -f 跟踪程序启动,能追踪到父进程getpid的系统调用,而子进程调用getpid却追踪不到!(如下)
execve("./test", ["./test"], [/* 43 vars */]) = 0
……
clone(Process 3858 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7fce708) = 3858
[pid 3857] getpid() = 3857
[pid 3857] fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 4), ...}) = 0
[pid 3857] mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fe4000
[pid 3857] write(1, "3857\n", 53857
) = 5
[pid 3857] wait4(-1, Process 3857 suspended

[pid 3858] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
[pid 3858] rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
[pid 3858] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
[pid 3858] nanosleep({3, 0}, {3, 0}) = 0
[pid 3858] fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 4), ...}) = 0
[pid 3858] mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fe4000
[pid 3858] write(1, "3858\n", 53858
) = 5
[pid 3858] exit_group(0) = ?
Process 3857 resumed
Process 3858 detached
<... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 3858
--- SIGCHLD (Child exited) @ 0 (0) ---
exit_group(0) = ?

我在sys_getpid()中加了行printk语句,通过dmesg查看,发现父进程执行了,而子进程没有执行。子进程似乎没有执行sys_getpid,那么子进程是如何获得自己的pid号的?

请高人解决!
文章评论

共有 0 条评论