Fork command in shell to print process ids - c

I am trying to print the pid of the processes after running the fork() command. Here is my code-
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid;
pid=fork();
if(pid==0)
printf("I am the child.My pid is %d .My parents pid is %d \n",getpid(),getppid());
else
printf("I am the parent.My pid is %d. My childs pid is %d \n",getpid(),pid);
return 0;
}
This is the answer I am getting-
I am the parent.My pid is 2420. My childs pid is 3601
I am the child.My pid is 3601 .My parents pid is 1910
Why is the parents id in 2nd line not 2420.Why am I getting 1910 How can I get this value?

The parent is exiting before the child performs its printf call. When the parent exits, the child gets a new parent. By default this is the init process, PID 1. But recent versions of Unix have added the ability for a process to declare itself to be the "subreaper", which inherits all orphaned children. PID 1910 is apparently the subreaper on your system. See https://unix.stackexchange.com/a/177361/61098 for more information about this.
Put a wait() call in the parent process to make it wait for the child to exit before it continues.
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid;
pid=fork();
if(pid==0) {
printf("I am the child.My pid is %d .My parents pid is %d \n",getpid(),getppid());
} else {
printf("I am the parent.My pid is %d. My childs pid is %d \n",getpid(),pid);
wait(NULL);
}
return 0;
}

Related

How to obtain the child process PID in C

In order to get the PID of the child process I am doing this.
pid_t pid;
pid=fork();
if(pid==0){
//child's work
}else{
printf("The child's PID is %d",pid);
}
I want to print the child's pid from parent! So is it ok if I printf pid or do I need to use getpid()?
On success fork returns the pid of the child process, so pid will be the pid of the child process, So I'd say it's correct.
getpid is to get the pid of the current process, so it's not suited to get the child's pid.
I think this sums up the question :
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
printf("In child => Own pid : %d\n", getpid());
printf("In child => Parent's pid : %d\n", getppid());
}
else{
printf("In Parent => Child's pid is %d\n", pid);
printf("In Parent => Own pid : %d\n", getpid());
}
return 0;
}

How to write a program that creates 2 parent processes and one child for each?

I'm not sure what does this code do.
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
for(int i=0; i<2; i++)
{
int pid=fork();
if(pid==0)
{
printf("child process \n");
printf("Child pid id [%d], parent pid is [%d]\n", (int) getpid(), (int) getppid());
} else if(pid>0)
{
int stats;
wait(&stats);
printf("parent process \n");
printf("Child pid id [%d], parent pid is [%d]\n", (int) getpid(), (int) getppid());
}
}
return 0;
}
I call fork() and assign it's value to variabe pid. Then we go to int stats, next line returns pid=0, then the program displays child process and then the parent process. It works pretty nice, but only when i<1. I thought that it is possible to do the same thing once again, but it's strange. fork() creates a new child process, so if it is used only once, if should create a child process, which parent is an IDE. Why am I wrong and what should I change to make 2 parents and 1 child for each, basically 4 processes?
I am not completely sure what you mean by "2 parents". In any case, you need to return the child so that they do not loop again:
printf("Child pid id [%d], parent pid is [%d]\n", (int) getpid(), (int) getppid());
return 0;
Otherwise you will have the children spawning other processes.

Forking a process into 7 children without looping

How can I create 7 processes using 1 parent in just 3 forks() calls? Looping is not allowed out of 7 1 is the parent one
I tried
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
printf("master process is %d.\n", getpid());
fork();
fork();
fork();
wait(0);
printf("my pid is %d; my parent pid is %d\n", getpid(), getppid());
return 0;
}
and got the output
master process is 28982.
my pid is 28982; my parent pid is 31294
my pid is 28985; my parent pid is 28982
my pid is 28984; my parent pid is 28982
my pid is 28983; my parent pid is 1
my pid is 28986; my parent pid is 28984
my pid is 28988; my parent pid is 28983
my pid is 28987; my parent pid is 1
my pid is 28989; my parent pid is 28987
Total number of processes created using fork() can be found using 2^n where n is the number of times fork() is being called. Here if we calls fork() 3 times, then 2^3 processes will be created i.e 8.

Why does the child process think the parent's process id is 1?

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main ( void ) {
int pid, fpid, ppid;
fpid = fork();
printf ("fpid is is %d\n", fpid);
sleep(5);
if (fpid > 0) {
pid = getpid();
ppid = getppid();
printf ("\nThis is Parent. My pid %d. My parent's pid %d\n", pid, ppid);
} else if (fpid == 0) {
sleep(1);
pid = getpid();
ppid = getppid();
printf ("\nThis is Child. My pid %d. My parent'a pid %d\n", pid, ppid);
}
}
I think when the parent process ID is 1 it means that the parent process has been terminated, so the child process gets re-parented to 1 (init, the first process). Is there any reason why the parent process would be terminated?
Parent process doesn't wait (by means of wait(2)) for the child process to complete. So, if parent exits before the child (it becomes an orphan process), then child process will be re-parented (adopted) to init process whose process ID is usually 1. Thus the child process says its parent process ID is 1.
Note that the init process' ID isn't necessarily 1 on all systems. POSIX doesn't mandate any such thing.
Because the child sleeps, by the time it calls getppid(), its parent will have likely died and the child will have been reparented to the init process (pid == 1).

Creating too many processes and seems to only terminate parent processes

I have created a program that creates two child process for a parent process. The program is to output the parent's process showing its process ID and then the two child processes showing their IDs and the ID of the parent. The parent process is supposed to capture the child process using the wait() function after the program exits and print an output.
However, my program keeps creating parent processes and giving children to those processes. I only want one parent process for the two child processes. Inside the while loop is the wait() function that is supposed to check for the changed state of the children process and print " Child 'xxx' process terminated". Instead, it is terminating some of the parent processes and other random processes.
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t cpid, cpid2, wpid;
int child = fork();
int child2 = fork();
int status;
if ((child = fork()) == 0){
child = cpid;
}
if((child2 = fork()) == 0){
child2 = cpid2;
}
else{
printf("I am the parent %d\n", getppid());
printf("I am the process %d created by %d\n", cpid, getppid());
printf("I am the process %d created by %d\n", cpid2, getppid());
while ((wpid = wait(&status)) > 0){
printf("Child process %d terminated\n", wpid);
}
}
return (0);
}
My output is showing me this
I am the parent 5764
I am the process 2 created by 5764
I am the process 6411548 created by 5764
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 23612
I am the process 2 created by 23612
I am the process 6411548 created by 23612
I am the parent 15096
I am the process 2 created by 15096
I am the process 6411548 created by 15096
I am the parent 24276
I am the process 2 created by 24276
I am the process 6411548 created by 24276
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 13720
I am the process 2 created by 13720
I am the process 6411548 created by 13720
I am the parent 5764
I am the process 2 created by 5764
I am the process 6411548 created by 5764
Child process 17016 terminated
Child process 18584 terminated
Child process 13984 terminated
Child process 8480 terminated
Child process 10816 terminated
Child process 21968 terminated
Child process 23388 terminated
Child process 11452 terminated
Child process 2776 terminated
Child process 19328 terminated
Child process 17116 terminated
Child process 18352 terminated
Child process 24276 terminated
Child process 15096 terminated
Child process 5764 terminated
Once you fork(), both the child and the parent are calling fork() again if you want to call fork() in the parent process only, check the return value before forking again.
int child = fork();
// This will be called by both, the child and the parent
int child2 = fork();
when fork() returns it returns the child PID in the parent and 0 in the child.
Start by reading the fork man page as well as the getppid / getpid man pages.
From fork's documentation:
On success, the PID of the child process is returned in the parent's
thread of execution, and a 0 is returned in the child's thread of
execution. On failure, a -1 will be returned in the parent's context,
no child process will be created, and errno will be set appropriately.
if ((child = fork()) == 0){
printf(" %u and %u", getpid(), getppid());
} else{ /* avoids error checking*/
printf("Parent - %u ", getpid());
}
The first child you fork() is forking the second child and also you're not handling else for the first fork()
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t cpid, cpid2, wpid;
int child = fork();
int child2 = fork();
int status;
if ((child = fork()) == 0){
child = cpid;
}
else {
if((child2 = fork()) == 0){
child2 = cpid2;
}
else{
printf("I am the parent %d\n", getppid());
printf("I am the process %d created by %d\n", cpid, getppid());
printf("I am the process %d created by %d\n", cpid2, getppid());
while ((wpid = wait(&status)) > 0){
printf("Child process %d terminated\n", wpid);
}
}
The way you're doing it, the first child is forking it's own child which defines a handler for the case where the returned PID != 0. So you will spawn two of the second child and handle the parent case twice.

Resources