[c] fork() child and parent processes

I am trying to create a program that uses fork() to create a new process. The sample output should look like so:

This is the child process. My pid is 733 and my parent's id is 772.
This is the parent process. My pid is 772 and my child's id is 773.

This is how I coded my program:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), fork());

    return 0;
}

This results in the output:

This is the child process. My pid is 22163 and my parent's id is 0.
This is the child process. My pid is 22162 and my parent's id is 22163.

Why is it printing the statement twice and how can I get it to properly show the parent's id after the child id displays in the first sentence?

EDIT:

#include <stdio.h>
#include <stdlib.h>

int main() {
int pid = fork();

if (pid == 0) {
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), getppid());
}
else {
    printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), pid);
}

return 0;
}

This question is related to c fork parent

The answer is


It is printing twice because you are calling printf twice, once in the execution of your program and once in the fork. Try taking your fork() out of the printf call.


This is the correct way for getting the correct output.... However, childs parent id maybe sometimes printed as 1 because parent process gets terminated and the root process with pid = 1 controls this orphan process.

 pid_t  pid;
 pid = fork();
 if (pid == 0) 
    printf("This is the child process. My pid is %d and my parent's id 
      is %d.\n", getpid(), getppid());
 else 
     printf("This is the parent process. My pid is %d and my parent's 
         id is %d.\n", getpid(), pid);

It is printing the statement twice because it is printing it for both the parent and the child. The parent has a parent id of 0

Try something like this:

 pid_t  pid;
 pid = fork();
 if (pid == 0) 
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(),getppid());
 else 
    printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), getppid() );

We control fork() process call by if, else statement. See my code below:

int main()
{
   int forkresult, parent_ID;

   forkresult=fork();
   if(forkresult !=0 )
   {
        printf(" I am the parent my ID is = %d" , getpid());
        printf(" and my child ID is = %d\n" , forkresult);
   }
   parent_ID = getpid();

   if(forkresult ==0)
      printf(" I am the  child ID is = %d",getpid());
   else
      printf(" and my parent ID is = %d", parent_ID);
}

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to fork

How to use execvp() Example of waitpid() in use? How to make parent wait for all child processes to finish? fork: retry: Resource temporarily unavailable fork() child and parent processes How to kill a child process by the parent process? How to use shared memory with Linux in C The difference between fork(), vfork(), exec() and clone() fork() and wait() with two child processes Redirecting exec output to a buffer or file

Examples related to parent

parent & child with position fixed, parent overflow:hidden bug How can I return to a parent activity correctly? How to call a parent method from child class in javascript? Make div (height) occupy parent remaining height jQuery: get parent tr for selected radio button Select parent element of known element in Selenium fork() child and parent processes How do I get the n-th level parent of an element in jQuery? How to delete parent element using jQuery PHP Accessing Parent Class Variable