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

  • SEARCH
  • Home
  • 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 3437874
In Process

The Archive Base Latest Questions

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

I have studied that during a fork, the data and code segment of the

  • 0

I have studied that during a fork, the data and code segment of the parent process gets duplicated into the child process.

Kindly see the program below.

int main()
{
   int a = 5;
   pid_t pid;

   pid = fork();

   if(pid == 0)
   {
      printf("In child a = %d",a);
   }

   else
   {
     printf("In parent a = %d",a);
   }

   return 0;
}

Here a is in the stack segment of parent process as it is declared inside the function, main(). The child process should only get copy of the code and data segment of the parent process and not the stack during fork(). But when I run the program, I can see that the child process is able to access the variable ‘a’ also. Thats means somehow the stack of parent process is also copied into the child process.

Kindly tell me the reason for this and correct me if my understanding is wrong.

  • 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-18T08:07:07+00:00Added an answer on May 18, 2026 at 8:07 am

    You should check the docs again. fork creates an “exact copy of the calling process”. Admittedly, there are a lot of exceptions, but the stack is not one of them.

    Also, if the stack wasn’t duplicated, the very common idiom (also used in your code) of checking the return value (almost always a stack variable) from fork would fail. There wouldn’t be a stack position for pid unless the stack (including stack pointer) was duplicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have studied the Variable Elimination recently and the teacher emphasizes that it is
i have studied design patterns and want to use them to code an open
I have studied a bit into the lifespan of the DataContext trying to work
I have a partition, formatted as NTFS. I have studied that devices formatted as
Recently I've studied the backpropagation network and have done some manual exercise. After that,
I've studied C programming in college some years ago and have developed some medium
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have been working on an iPhone app that has a feature of uploading
I have recently started to examine ASP.NET MVC. I have studied quite a few
I have a project that adds elements to an AutoCad drawing. I noticed that

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.