The parent and child processes call getpid. why cannot the child process be tracked?

Source: Internet
Author: User
The parent and child processes call getpid. why cannot the child process be tracked? -- Linux general technology-Linux programming and kernel information. The following is a detailed description. [I = s] This post was last edited by jipingwt

I wrote a small program (test. c), as follows:
# 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 ("0 error ");
Exit (1 );
}
P = getpid ();
Printf ("% ld \ n", p );
Wait (& I );
Return 0;
}

The pid number of the parent and child processes can be obtained after the compilation, but it is strange that the strace-f tracking program is used to start and the system call of the getpid of the parent process can be tracked, however, the sub-process cannot trace the call to getpid! (As shown below)
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 () = 1, 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 suincluded

[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) =?

I added the printk statement in sys_getpid () and checked it through dmesg. I found that the parent process was executed, but the child process was not executed. If the sub-process does not seem to execute sys_getpid, how does the sub-process obtain its own pid number?

Please solve this problem!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.