Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get child PID in C?

Tags:

c

fork

pid

I'm creating child processes in a for-loop. Inside the child process, I can retrieve the child PID with getpid().

However, for some reason when I try to store the value of getpid() into a variable declared by the parent process, the change is nullified when I check for it in the parent process. I'm assuming this has to do with some sort of process variable scope. Not really familiar with C, so can't be too sure.

Anyhow what is a way of storing the result of getpid() of a child PID (when called from the child process) into a variable in the parent process?

Or maybe another approach is storing fork() into a variable in the parent and calling some function on that variable to retrieve the child's PID? I don't know how to do this either, so if this is the better way, how would you do this?

like image 571
Derek Avatar asked Feb 05 '12 07:02

Derek


People also ask

How do you find the PID of all child processes?

Using the /proc File System It contains information about the kernel, system, and processes. We can find the PIDs of the child processes of a parent process in the children files located in the /proc/[pid]/task/[tid] directories.

How do you find PID in C?

Getpid() is the function used to get the process ID of the process that calls that function. The PID for the initial process is 1, and then each new process is assigned a new Id. It is a simple approach to getting the PID.

What is child PID?

The child process has a unique process ID (PID) that does not match any active process group ID. The child has a different parent process ID, that is, the process ID of the process that called fork(). The child has its own copy of the parent's file descriptors.

Does fork () Create a child?

The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call.


2 Answers

fork already returns the child's pid. Just store the return value.

look at man 2 fork:

RETURN VALUES

 Upon successful completion, fork() returns a value of 0 to the child process and
 returns the process ID of the child process to the parent process.  Otherwise, a
 value of -1 is returned to the parent process, no child process is created, and
 the global variable errno is set to indicate the error.
like image 54
MByD Avatar answered Oct 01 '22 02:10

MByD


As mentioned in previous answer that "fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process." So, the code can be written in this way:

pid = fork(); /* call fork() from parent process*/
if (0 == pid)
{
  /* fork returned 0. This part will be executed by child process*/
  /*  getpid() will give child process id here */
}
else
{
  /* fork returned child pid which is non zero. This part will be executed by parent process*/
  /*  getpid() will give parent process id here */
} 

This link is very helpful and explains in detail.

like image 32
Amal Roshan Avatar answered Oct 01 '22 00:10

Amal Roshan