18891

Segments duplicated during fork()?

Question:

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.

Answer1:

You should check the <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fork.html" rel="nofollow">docs</a> 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 <em>be</em> a stack position for pid unless the stack (including stack pointer) was duplicated.

Answer2:

That isn't a good test - as Matthew has pointed out, fork() gives you an exact copy of the parent process, including the stack (else the child would be unable to return from this function).

A better test is to modify 'a' in the parent and observe it in the child, like this:

#include <stdio.h> #include <unistd.h> int main() { int a = 5; pid_t pid; pid = fork(); if (pid == 0) { sleep(5); printf("In child a = %d\n",a); } else { a++; printf("In parent a = %d\n",a); } return 0; }

and the result is correct:

pandora:~/tmp$ cc -o x x.c pandora:~/tmp$ ./x In parent a = 6 pandora:~/tmp$ In child a = 5

Recommend

  • Receive custom intent without activity restart
  • NHibernate proxyexception
  • Can't compile Arduino CapSense example
  • Excel distinct count with conditions
  • How do I add a File Type Association in a Windows Phone 8.1 app manifest?
  • can variables be set randomly when declaring them again?
  • Where these are stored?
  • Most efficient way to move table rows from one table to another
  • How do I remove all but some records based on a threshold?
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Display images in Django
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Functions in global context
  • Meteor: Do Something On Email Verification Confirmation
  • Where to put my custom functions in Wordpress?
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Apache 2.4 - remove | delete | uninstall
  • XCode can't find symbols for a specific iOS library/framework project
  • SVN: Merging two branches together
  • RestKit - RKRequestDelegate does not exist
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • How to format a variable of double type
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • apache spark aggregate function using min value
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?