accessing variable from child process

B

bill

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.
 
I

Ian Collins

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.
 
B

Ben Bacarisse

bill said:
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.

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>
 
M

MartinBroadhurst

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
 
N

Nobody

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.
 
K

Keith Thompson

Nobody said:
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.

[...]
 
G

gwowen

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).
 
C

Chris M. Thomasson

[...]
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'.

;^)
 
N

Nobody

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).
 
J

James

Nobody said:
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?
 
S

Seebs

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top