Pthreads and sharing process space. What are the values on LINE C and LINE P respectively? https://gist.github.com/1591399 My best guess is 5 and 0. The default is not to share any date (heap, stack) right? I guess whether the int value is in heap or stack is implementation dependent but I'd like some feedback on that.
Code might not be exact, typed it from an image. I see an uf instead of if. Besides syntax problems everything is fine in a linux or pthread friendly os. sorry!~
From what I understand, without a lock or in this case a mutex around the variable, the forked processes create their own instance of the variable to use. So the child processes do not update the parents variable.
Yeah I guess pthreads is a bit different. I'm sure there are options for fork() that allow variable degrees of sharing data (heap, stack) but again I'm unclear. I guess I was more curious as to where the int outside of the functions were stored (heap?)
To be clear, forking a process always *effectively* results in a separate stack and heap. In certain UNIX implementations, something called Copy-on-Write is used, where the child process will “use” the parent process's memory space until it writes to memory, at which point it will copy out the relevant memory to modify it. This is a performance optimization, but as far as the child process is concerned, it has a completely separate memory space. Because of this, it's irrelevant what the pthreads are doing. You forked, and therefore the first chunk is running in a separate memory address from the second chunk entirely, so they see their own data. The first part could have used pthreads or not, it doesn't matter. The fork is the important component here.
Totally understand. I let the mixed elements (fork vs just the pthreads) slide under the radar completely! I have heard of copy on write although not for this purpose specifically. Very good to know.
Join our real-time social learning platform and learn together with your friends!