accessing variable from child process

Discussion in 'C Programming' started by bill, Nov 4, 2010.

  1. bill

    bill Guest

    I have a need to create a child process that can access 2 variables
    from the parent process. fork is out as that creates a copy of memory
    as the child can't access the parents memory. I tried pthread_create
    but the child process ends up with the value from when the thread was
    created and the current value.

    I modified the following code as a proof of concept using 1 variable
    from https://computing.llnl.gov/tutorials/pthreads/

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    #define NUM_THREADS 50

    void *PrintHello(void *threadid){
    long tid;
    sleep(5);
    tid = (long)threadid;
    printf("Hello World! It's me, thread #%ld!\n", tid);
    pthread_exit(NULL);
    }

    int main (int argc, char *argv[]){
    pthread_t threads[NUM_THREADS];
    int rc;
    long t=3;
    rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
    if (rc){
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }
    for(t=0; t<NUM_THREADS; t++){
    printf("In main: creating thread %ld\n", t);
    t*=2;
    }
    sleep(10);
    pthread_exit(NULL);
    }

    The output is
    Hello World! It's me, thread #3!
    and it should be
    Hello World! It's me, thread #31!

    I found http://www.cs.cf.ac.uk/Dave/C/node27.html which shows shard
    memory but the client and server are two separate programs. That
    doesn't work for my application.

    So what is the best way for a child process to access the current
    value of a variable from a parent process?

    Thank you in advance.
    bill, Nov 4, 2010
    #1
    1. Advertising

  2. bill

    Ian Collins Guest

    On 11/ 4/10 02:09 PM, bill wrote:
    > I have a need to create a child process that can access 2 variables
    > from the parent process. fork is out as that creates a copy of memory
    > as the child can't access the parents memory. I tried pthread_create
    > but the child process ends up with the value from when the thread was
    > created and the current value.


    On most systems creating a thread != creating a process.

    You'll probably get more relies on comp.unix.programmer.

    --
    Ian Collins
    Ian Collins, Nov 4, 2010
    #2
    1. Advertising

  3. bill <> writes:

    > I modified the following code as a proof of concept using 1 variable
    > from https://computing.llnl.gov/tutorials/pthreads/
    >
    > #include <pthread.h>


    You'll get better answers in comp.programming.threads or
    comp.unix.programmer. Threaded code is not standard C and you need to
    find the pthreads experts.

    <snip code>
    > The output is
    > Hello World! It's me, thread #3!
    > and it should be
    > Hello World! It's me, thread #31!


    At first glace, I see no reason to expect what you do. Printing 3 is
    exactly what the program should do.

    <snip>
    --
    Ben.
    Ben Bacarisse, Nov 4, 2010
    #3
  4. On Nov 4, 1:09 am, bill <> wrote:
    > I have a need to create a child process that can access 2 variables
    > from the parent process.  fork is out as that creates a copy of memory
    > as the child can't access the parents memory.  I tried pthread_create
    > but the child process ends up with the value from when the thread was
    > created and the current value.
    >
    > I modified the following code as a proof of concept using 1 variable
    > fromhttps://computing.llnl.gov/tutorials/pthreads/
    >
    > #include <pthread.h>
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <unistd.h>
    >
    > #define NUM_THREADS     50
    >
    > void *PrintHello(void *threadid){
    > long tid;
    >    sleep(5);
    >    tid = (long)threadid;
    >    printf("Hello World! It's me, thread #%ld!\n", tid);
    >    pthread_exit(NULL);
    >
    > }
    >
    > int main (int argc, char *argv[]){
    > pthread_t threads[NUM_THREADS];
    > int rc;
    > long t=3;
    >    rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
    >    if (rc){
    >       printf("ERROR; return code from pthread_create() is %d\n", rc);
    >       exit(-1);
    >    }
    >    for(t=0; t<NUM_THREADS; t++){
    >       printf("In main: creating thread %ld\n", t);
    >       t*=2;
    >    }
    >    sleep(10);
    >    pthread_exit(NULL);
    >
    > }
    >
    > The output is
    > Hello World! It's me, thread #3!
    > and it should be
    > Hello World! It's me, thread #31!
    >
    > I foundhttp://www.cs.cf.ac.uk/Dave/C/node27.htmlwhich shows shard
    > memory but the client and server are two separate programs.  That
    > doesn't work for my application.
    >
    > So what is the best way for a child process to access the current
    > value of a variable from a parent process?
    >
    > Thank you in advance.


    If you don't want to *change* the value in the child process, just put
    it on the command line and use system.

    Martin
    MartinBroadhurst, Nov 4, 2010
    #4
  5. bill

    Nobody Guest

    On Wed, 03 Nov 2010 18:09:32 -0700, bill wrote:

    > I have a need to create a child process that can access 2 variables
    > from the parent process. fork is out as that creates a copy of memory
    > as the child can't access the parents memory.


    Well you're out of luck, as fork() is the *only* way to create a child
    process.

    [Technically, there's also vfork(), but that either behaves the same as
    fork() or is unusable in cases where the child doesn't call exec().]

    > I tried pthread_create
    > but the child process ends up with the value from when the thread was
    > created and the current value.


    pthread_create() creates a thread, not a process.

    > I found http://www.cs.cf.ac.uk/Dave/C/node27.html which shows shard
    > memory but the client and server are two separate programs. That
    > doesn't work for my application.


    Shared memory works fine between parent and child, regardless of whether
    the child exec()s.

    > So what is the best way for a child process to access the current
    > value of a variable from a parent process?


    Shared memory. But don't forget to use "volatile" where needed, otherwise
    the compiler may optimise away the reads.
    Nobody, Nov 5, 2010
    #5
  6. Nobody <> writes:
    > On Wed, 03 Nov 2010 18:09:32 -0700, bill wrote:
    >
    >> I have a need to create a child process that can access 2 variables
    >> from the parent process. fork is out as that creates a copy of memory
    >> as the child can't access the parents memory.

    >
    > Well you're out of luck, as fork() is the *only* way to create a child
    > process.
    >
    > [Technically, there's also vfork(), but that either behaves the same as
    > fork() or is unusable in cases where the child doesn't call exec().]


    That applies only to Unix-like systems, which means (as several people
    have already pointed out) that Bill should be asking his question
    elsewhere.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 5, 2010
    #6
  7. bill

    gwowen Guest

    On Nov 4, 1:09 am, bill <> wrote:
    > int main (int argc, char *argv[]){
    > pthread_t threads[NUM_THREADS];
    > int rc;
    > long t=3;
    > rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);


    Here you're passing the thread the value "3", not the address of the
    variable t.

    Try pthread_create(..., (void*) &t);

    with

    void *PrintHello(void *threadid){
    volatile long *tid = (volatile long *)threadid;
    while(1){
    sleep(5);
    printf("Hello World! It's me, the thread: t is now %ld\n",*tid);
    fflush(NULL);
    }

    pthread_exit(NULL);
    }

    int main()
    {
    volatile long t=0;
    pthread_t thread;
    pthread_create(&thread,NULL,PrintHello,(void *) &t);
    // check for success omitted
    while(1){
    ++t;
    t = t%1024;
    }
    }

    Also, you seem to be using the variable "t" for two or three distinct
    things... Is it the shared variable? Is it a loop variable for
    printing messages and creating threads? Is it a unique thread-id
    passed to each thread on creation?

    In my code, its the address of a variable shared between the threads.
    So, the main thread modifies the variable, and the child thread can
    read the new value by dereferencing the variables address (which wont
    ever change).
    gwowen, Nov 5, 2010
    #7
  8. "bill" <> wrote in message
    news:...
    [...]

    > So what is the best way for a child process to access the current
    > value of a variable from a parent process?


    The parent process generally would "provide" an offset into a common base
    shared memory address directly to the client process. You can choose from a
    number of methods to pass the offset; shared memory, sockets, pipes, mpi,
    ect, ect... Also, you kind of "need" to pass offsets instead of pointers
    simply because the child process could map the shared memory to a different
    base address... After the client has the offset, it can "access" the
    variable. Keep in mind that proper memory synchronization methods are going
    to be needed if the client process depends on the value of the shared
    variable to represent the state of an unrelated object: e.g., shared
    variable with value of 1 means that shared object is in coherent state; you
    need sync for that to work...

    One more thing, perhaps you should move this conversation over to
    `comp.programming.threads'.

    ;^)
    Chris M. Thomasson, Nov 5, 2010
    #8
  9. bill

    Nobody Guest

    On Fri, 05 Nov 2010 11:30:48 -0700, Chris M. Thomasson wrote:

    >> So what is the best way for a child process to access the current
    >> value of a variable from a parent process?

    >
    > The parent process generally would "provide" an offset into a common base
    > shared memory address directly to the client process. You can choose from a
    > number of methods to pass the offset; shared memory, sockets, pipes, mpi,
    > ect, ect... Also, you kind of "need" to pass offsets instead of pointers
    > simply because the child process could map the shared memory to a different
    > base address...


    In this case, he shouldn't need to pass anything; AFAICT, this is a
    fork-without-exec scenario, so the child already knows the offset, and
    possibly even the address (if the parent attaches a shared memory segment
    prior to the fork, the child can just inherit it rather than re-attaching).
    Nobody, Nov 5, 2010
    #9
  10. bill

    James Guest

    "Nobody" <> wrote in message
    news:p...
    > On Fri, 05 Nov 2010 11:30:48 -0700, Chris M. Thomasson wrote:
    >
    >>> So what is the best way for a child process to access the current
    >>> value of a variable from a parent process?

    >>
    >> The parent process generally would "provide" an offset into a common base
    >> shared memory address directly to the client process. You can choose from
    >> a
    >> number of methods to pass the offset; shared memory, sockets, pipes, mpi,
    >> ect, ect... Also, you kind of "need" to pass offsets instead of pointers
    >> simply because the child process could map the shared memory to a
    >> different
    >> base address...

    >
    > In this case, he shouldn't need to pass anything; AFAICT, this is a
    > fork-without-exec scenario, so the child already knows the offset, and
    > possibly even the address (if the parent attaches a shared memory segment
    > prior to the fork, the child can just inherit it rather than
    > re-attaching).


    Is the child process guaranteed to get the _exact_same_ base address of
    shared memory that the parent process created?
    James, Nov 5, 2010
    #10
  11. bill

    Seebs Guest

    On 2010-11-05, James <> wrote:
    > Is the child process guaranteed to get the _exact_same_ base address of
    > shared memory that the parent process created?


    This is wildly off-topic, but if it didn't have the same address space,
    it wouldn't exactly be a duplicate, now would it? Discussion of the
    semantics of fork() sounds like comp.unix.programmer to me.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Nov 5, 2010
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page