3

I just can't figure out why this code works the way it does (rather than I'd expect):

 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> #include <sys/types.h> int main() { int buffer; int* address; address=&buffer; if(fork()==0) { *address=27; printf("Address %ld stores %d\n",(long)address,*address); exit(0); } wait(NULL); printf("Address %ld stores %d\n",(long)(&buffer),buffer); return 0; } 

Why does the system store different variables even if they're pointed to the same memory address?

NOTE: I never really expected this code to work, since otherwise the whole bunch of pipes and stuff wouldn't make any sense; I'd just like to understand what's going on here.

5
  • 1
    What is your output and what do you expect? I expect a garbage value for buffer and that is what I get: ideone.com/ESqx1I Commented Jul 3, 2018 at 9:38
  • 2
    You have to familiarize yourself with the difference between physical and virtual memory addresses Commented Jul 3, 2018 at 9:38
  • @mch That's what I get as well, it just feels weird to read something like: Memory adrress X stores 27, and memory address X stores 0 (I mean, provided X=X...) Commented Jul 3, 2018 at 9:44
  • @Ctx it looks like I'm thinking of virtual memory as though it was physical. Is there a way to force a proces to write in a specific physical address anyway? Commented Jul 3, 2018 at 9:49
  • 2
    @Guillermo.D.S. There is no portable way to write to a specific physical address but I do not see a good reason for this anyway. There are other mechanisms for sharing memory between processes for example (threads, posix shared memory, etc). Commented Jul 3, 2018 at 9:54

2 Answers 2

6

This isn't really a C question, it's about the behavior of (modern) operating systems.

In short: A userspace program on a modern OS runs in some private virtual address space. When accessing memory, the virtual address is translated to a physical address. The mapping between actual memory and virtual address space is set up by the operating system -- the memory is split into pages and a page can be "mapped" into the address space of a process.

fork() typically just maps the same memory to the second process it creates, but as soon as this memory is written to, the page is copied and the copy is mapped ("copy on write"). A user space program will never see memory that is private to a different user space program.

I'm sure you can easily find more details searching for the key words given in this answer.

Sign up to request clarification or add additional context in comments.

9 Comments

Thx @Felix Palmen. So it's all up to the OS? isn't there any trick to force a process to write within a specific physical address?
@Guillermo.D.S. also look up MMU (memory management unit) .. the translation of addresses is done in hardware, so, no, there isn't a way around for a process that doesn't have kernel privileges, at least not without a hardware flaw or some exploitable hole in the OS itself. Only the kernel can set up the page tables used by the MMU.
Indeed there are sometimes OS-specific ways (under intel linux, for example, you can map /dev/mem). But this is higly platform and architecture specific.
@Ctx yes, but if there are such mechanisms, they are typically reserved to some "privileged" processes. And it's still the kernel actually accessing the physical memory.
@FelixPalmen Yes, of course, you have to be root (or change the permissions of /dev/mem). But no, it is not the kernel then, but the pages are mapped directly into the processes virtual memory
|
1

From wikipedia: The fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process.

So basically you create a new process with a different COPY of your original process memory. You change something on the duplicate process's memory, which is identical but a copy and expect to see it in the original. Normally, processes don't share memory directly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.