Home » NO IDEA » parent id and child id

parent id and child id

Blog Stats

  • 14,863 hits
April 2013
M T W T F S S
« Mar   Oct »
1234567
891011121314
15161718192021
22232425262728
2930  

In this post we shall see about the parent and child id ,as we discusses in the previous post each process carries it own process id when new child process is created  new process id is created again

getpid()-gets the process of the current process

getppid()-gets the process of the parent process

#include<stdio.h>

#include<sys/types.h>
#include<unistd.h>
int main( )
{
int pid ;
pid = fork( ) ;
if ( pid == 0 )
{
printf ( "Child : Hello I am the child process\n" ) ;
printf ( "Child : Child’s PID: %d\n", getpid( ) ) ;
printf ( "Child : Parent’s PID: %d\n", getppid( ) ) ;
}
else
{
printf ( "Parent : Hello I am the parent process\n" ) ;
printf ( "Parent : Parent’s PID: %d\n", getpid( ) ) ;
printf ( "Parent : Child’s PID: %d\n", pid ) ;
}
}

Parent : Hello I am the parent process
Parent : Parent’s PID: 10635
Child : Hello I am the child process
Parent : Child’s PID: 10636
Child : Child’s PID: 10636
Child : Parent’s PID: 10635

 

In order to find the process running in the computer use the command $ ps -a

eg:

PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 kthreadd
3 ? 00:00:02 ksoftirqd/0
6 ? 00:00:00 migration/0
7 ? 00:00:00 migration/1

23 ? 00:00:00 khungtaskd
24 ? 00:00:02 kswapd0
25 ? 00:00:00 ksmd
26 ? 00:00:00 khugepaged
:

:
10687 ? 00:00:00 gnome-pty-helpe
10688 pts/1 00:00:00 bash
10745 pts/1 00:00:00 ps

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: