getpid () pit_t
Nov 11, 2021 at 3:25pm UTC
This a small progam to print the process ID
I got this result
The process id: 165949
which process is this ? As I know is the getpid() is to return the ID of the calling process, is that mean the process which the system called or just random process?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void )
{
pid_t process_id;
process_id = getpid();
printf("ID is : %d\n" ,process_id);
return 0;
}
Nov 11, 2021 at 3:40pm UTC
> which process is this ?
It's the process ID of the thing you're running
- starts at main
- calls printf
- ends at return 0
All that is running with the pid of what you see on screen.
Nov 11, 2021 at 4:09pm UTC
so is that mean that to get :
1- the child process or the current process we use
and
2- the parnet process of the current process is
p_process_id = getppid();
or the current process and the child process is not the same !
Nov 11, 2021 at 4:21pm UTC
I dunno where you're getting the idea of child within the file itself, when it's not the thing actually creating any processes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
$ cat foo.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void )
{
pid_t process_id, parent_pid;
process_id = getpid();
parent_pid = getppid();
printf("My ID is : %d\n" ,process_id);
printf("My parent is %d\n" ,parent_pid);
return 0;
}
$ gcc foo.c
$ ./a.out
My ID is : 17429
My parent is 16267
$ echo $$
16267
Notice anything interesting here?
$$ is the shell variable indicating the process ID of itself, which will naturally become the parent process ID of anything the shell creates (like the above mentioned program).
https://www.tutorialspoint.com/unix/unix-special-variables.htm
Topic archived. No new replies allowed.