Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:06:18+00:00 2026-05-28T06:06:18+00:00

void child(int pid){ printf(Child PID:%d\n,pid); exit(0); } void parent(int pid){ printf(Parent PID:%d\n,pid); exit(0); }

  • 0
void child(int pid){
    printf("Child PID:%d\n",pid);
    exit(0);    
}
void parent(int pid){
    printf("Parent PID:%d\n",pid);
    exit(0);
}

void init(){
    printf("Init\n");//runs before the fork
}


int main(){

    init();//only runs for parent i.e. runs once
    printf("pre fork()");// but this runs for both i.e. runs twice
    //why???

    int pid = fork();

    if(pid == 0){
        child(pid); //run child process
    }else{
        parent(pid);//run parent process
    }
    return 0;
}

output:

Init
pre fork()Parrent PID:4788
pre fork()Child PID:0

I have a process in a Unix OS (Ubuntu in my case). I can’t for the life of me understand how this works. I know the fork() function splits my programs in two processes but from where? Does it create a new process and run the whole main function again, and if so why did the init() only run once and the printf() twice?

Why does the printf("pre fork()"); run twice and the init() function only once?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:06:18+00:00Added an answer on May 28, 2026 at 6:06 am

    There’s only one process until the fork. That is, that path is executed only once. After the fork there are 2 processes so the code following that system call is executed by both processes. What you ignore is that both terminate and both will call exit.

    In your code you’re not flushing stdio. So both processes do that (exit flushes stdio buffers) – that’s why you’re seeing that output.

    Try this:

    printf("pre fork()\n");
                      ^^ should flush stdout
    

    Or maybe

    printf("pre fork()\n");
    fflush(stdout);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<sys/types.h> #include<stdlib.h> int turn; int flag[2]; int main(void) { int pid,parent=1; printf(before vfork\n);
I work on code something like this ... HEADERS ... int *var; void child()
class Parent implements Serializable{ ........ } class Child extends Parent{ private void writeObject(ObjectOutputStream oos)
typedef struct child {int count; char word[100]; inner_list*next;} child; typedef struct parent { char
I have a super class like this: class Parent { public: virtual void Function(int
The following program forks off a child, that runs /bin/sleep 10 repeatedly. The parent
I want use pointer to get the pid of parent process with child process
Given this code : #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { int
void test() { unsigned char c; c = (~0)>>1 ; printf(c is %u\n,c); }
- (void)TargetHit:(int)target{ void (^threadBlock)(void) = ^{ NSLog(@respond to selector %d, [self respondsToSelector:@selector(changeImageOfTarget:)]); [[NSOperationQueue mainQueue]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.