How to get child process from parent process
Just use :
pgrep -P $your_process1_pid
How to get child PID in C?
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.
getting child process ID from the parent using C
After a minute of googling I found this page, where everything you need is written:
System call fork() is used to create processes. It takes no arguments and returns a process ID.
I highlighted the part which is importand for you, so you don't need to do anything to get the process IDs of the children. You have them already in fork1
and fork2
!
Here is some code which will print the PIDs from parent and children.
#include <stdio.h>
int main() {
int fork1 = fork();
if (fork1 < 0) {
printf("error\n");
} else if (fork1 == 0) {
printf("I'm child 1\n");
printf("child 1: parent: %i\n", getppid());
printf("child 1: my pid: %i\n", getpid());
} else {
int fork2 = fork();
if (fork2 < 0) {
printf("error\n");
} else if (fork2 == 0) {
printf("I'm child 2\n");
printf("child 2: parent: %i\n", getppid());
printf("child 2: my pid: %i\n", getpid());
} else {
printf("I'm the parent\n");
printf("The PIDs are:\n");
printf("parent: %i\n", getpid());
printf("child 1: %i\n", fork1);
printf("child 2: %i\n", fork2);
}
}
return 0;
}
How to find all child process of a parent process (in C)
Following would be very basic steps you would like to do:
PS: Assuming you have not completed recursive function calls yet.
STEP1: First get the process id PID
STEP2:Find the child process IDs for PID and maintain a list (array) of them.
STEP3:Repeat STEP2 for all child process IDs saved in the list
Child process starts after parent process
The explanation is simple.
Scheduling of processes is up to the kernel. If this is a single core processor then in this run it decided to suspend the child execution and allow the parent process to run first. The parent ran for a few loops before being suspended in favor of the child, and so forth.
In a multiprocessor system both process can run in tandem but the console device will alternate the output depending on timing of interrupts.
There is no assurance a different run will result in the same output. There is also no assurance that different kernel version won't do something else.
If you want the processes to alternate between loops now is the time to learn interprocess communication.
Related Topics
Replacing Control Character in Sed
What Do These Kernel Panic Errors Mean
Create Zip File and Ignore Directory Structure
Why Do We Need a Bootloader in an Embedded Device
Bash Script to Remove 'X' Amount of Characters the End of Multiple Filenames in a Directory
Tar Archiving That Takes Input from a List of Files
Bash: Inserting a Line in a File at a Specific Location
Gcloud Compute Copy-Files': Permission Denied When Copying Files
Differencebetween Double-Ampersand (&&) and Semicolon (;) in Linux Bash
Why Does Sed Fail with International Characters and How to Fix
Should I Use Libc++ or Libstdc++
Will Read() Ever Block After Select()