-1

i am struggling a bit to understand the output to the following c program

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void) {
   int i = 1;   
   pid_t childId = fork();
   pid_t pid = getpid();

   if (childId == -1) {
    perror("fork() failed");
    } else if (childId == 0) {            
        printf("child PID: %d\n", pid);
        int *j = NULL;
        j = &i;
        (*j)++;            
        printf("value i in child: %d\t@adresse: %p\n",i ,&i);

    } else {
        pid_t pid = getpid();
        printf("parent PID: %d\nwait for child\n", pid);
        waitpid(childId, NULL, 0);          
        printf("value i in parent: %d\t@adresse: %p\n",i ,&i);          
    }
    return 0;
}

output:

parent PID: 10656
wait for child
child PID: 10657
value i in child: 2 @address: 0x7fff93b720c0
value i in parent: 1    @address: 0x7fff93b720c0

The child thread increments 'i' which is stored at the given address. After waiting for the child the parent thread continues execution and prints the 'i' at the same address but with the initial value. Shouldnt this be 2 as well?

mlp
  • 809
  • 7
  • 21
  • 4
    What threads? `fork()` creates a separate *process* with it's own address space, not a separate *thread* in the same address space. – Andrew Henle Jul 20 '18 at 13:33
  • for ease of readability and understanding: 1) Please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. 2) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* Note: treat the closing brace '}' as a complete statement. – user3629249 Jul 20 '18 at 13:38
  • Possible duplicate: https://stackoverflow.com/questions/9724473/how-the-memory-is-mapped-when-fork-is-used – izlin Jul 20 '18 at 13:39
  • the posted code does not compile! Amongst other things, it is missing the needed `#include` statements. – user3629249 Jul 20 '18 at 13:42
  • 2
    There are no 'threads' in the posted code. Suggest learning about the functions: `pthread_create()`, `pthread_join()`, `pthread_exit()` and the data type: `pthread_t` – user3629249 Jul 20 '18 at 13:44

1 Answers1

1

User applications use virtual memory. If you use fork() you create a separate process that uses virtual memory with the same addresses as the parent process.

So both processes use a virtual address space with the same addresses (as your printfs shows) but they may operate on separate physical memory.

Even more detailed explanation from Wikipedia (emphasis mine):

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. In modern UNIX variants that follow the virtual memory model from SunOS-4.0, copy-on-write semantics are implemented and the physical memory need not be actually copied. Instead, virtual memory pages in both processes may refer to the same pages of physical memory until one of them writes to such a page: then it is copied.

izlin
  • 2,129
  • 24
  • 30