Whu I value doesn't change ? - Unix

This is a discussion on Whu I value doesn't change ? - Unix ; Dear all, consider the following program #include #include main() { int pid,*i,j; i = &j; *i= 10; pid = fork(); if(pid == 0) { printf("\n address of i = %p",i); printf("\n initially i value in child = %d",*i); *i = ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Whu I value doesn't change ?

  1. Whu I value doesn't change ?

    Dear all,

    consider the following program

    #include
    #include

    main()
    {
    int pid,*i,j;

    i = &j;
    *i= 10;

    pid = fork();

    if(pid == 0)
    {
    printf("\n address of i = %p",i);
    printf("\n initially i value in child = %d",*i);
    *i = *i +10;
    printf("\n after incrementation i value in child = %d",*i);
    puts("Child terminated....");
    }
    else
    {
    wait( (int*) 0);

    printf("\n address of i = %p",i);
    printf("\n value of i in parent = %d",*i);
    }

    i am getting o/p as:-

    address of i = 0xbfe6c01c
    initially i value in child = 10
    after incrementation i value in child = 20
    Child terminated....

    address of i = 0xbfe6c01c
    value of i in parent = 10

    the address of i is same in both parent and child process but
    the change made in child process is not reflected in the
    parent process why ?

  2. Re: Whu I value doesn't change ?

    sophia.agnes@gmail.com wrote:
    > the address of i is same in both parent and child process but
    > the change made in child process is not reflected in the
    > parent process why ?


    Each process essentially has its own address space (pages, segments, however
    the hardware and operating system work together to do it). So the value of
    a pointer is an address relative to the that particular process's address
    space, and NOT within the entire system's address space.
    --
    Kurt M. Weber


  3. Re: Whu I value doesn't change ?

    On Nov 25, 7:58 pm, sophia.ag...@gmail.com wrote:

    > address of i = 0xbfe6c01c
    > initially i value in child = 10
    > after incrementation i value in child = 20
    > Child terminated....
    >
    > address of i = 0xbfe6c01c
    > value of i in parent = 10
    >
    > the address of i is same in both parent and child process but
    > the change made in child process is not reflected in the
    > parent process why ?


    Why should it be? They're two separate processes.

    Note that the addresses you are printing out are process virtual
    addresses. Each process has its own memory map and address space. This
    is why system virtual memory is not normally considered a scarce
    resource.

    DS

+ Reply to Thread