the scop of variables in child process

Hi

I have a question about the scope of variables for parent and a child
I had written my code here and the output of this but only in child
process the information is completely right even in main the informatin is wrong
well the child process will see the global variable te2 and can change it in its scope
but not the parent nore the main process don't see the changes the child has been made
so what do I have to do ?

thanks for your help and attention

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

struct test
{
int i;
int j;
};

struct test te;
struct test te2;

void test(struct test);

int main()
{
int i=6;
while(i>0)
{
test(te);
i--;
}
printf("\n \n main(): te2.i=%d,te2.j=%d",te2.i,te2.j);
return 0;
}

void test(struct test t)
{
pid_t pid;
int fd[2];
int retval;

if\(pipe\(fd\)&lt;0\)
\{
     printf\("\\n pipe error"\);
 exit\(0\);
 \}
 
 if \(\(pid=fork\(\)\)&lt;0\)
 \{
      printf\("\\n fork error"\);
  exit\(0\);
  \}
  
  if \(pid==0\)//child
  \{
  
        struct test m;
    int retval;
    retval=read\(fd[0],&m,sizeof\(m\)\);
    if \(retval&lt;0\)
    \{
         printf\("\\n child error"\);
	 exit\(0\);
     \}
     
     te2.j=7;
    
     printf\("\\n child : t.i=%d, te2.i=%d te2.j=%d",m.i,te2.i,te2.j\);
     
     exit\(EXIT_SUCCESS\);
       
  
  
  \}
  
  else
  \{
       
       te2.i=8;
   t.i=1;
   printf\("\\n parent : t.i=%d , te2.i=%d te2.j=%d",t.i,te2.i,te2.j\);
   
   write\(fd[1],&t,sizeof\(t\)\);
   getchar\(\);
   
   close\(fd[1]\);
   close\(fd[0]\);
   
\}

}

and the output:

parent : t.i=1 , te2.i=8 te2.j=0
child : t.i=1, te2.i=0 te2.j=7

parent : t.i=1 , te2.i=8 te2.j=0
child : t.i=1, te2.i=8 te2.j=7

parent : t.i=1 , te2.i=8 te2.j=0
child : t.i=1, te2.i=8 te2.j=7

parent : t.i=1 , te2.i=8 te2.j=0
child : t.i=1, te2.i=8 te2.j=7

parent : t.i=1 , te2.i=8 te2.j=0
child : t.i=1, te2.i=8 te2.j=7

parent : t.i=1 , te2.i=8 te2.j=0
child : t.i=1, te2.i=8 te2.j=7

main(): te2.i=8,te2.j=0

I believe, that parent and child have two different spaces in memory, and so they shouldnt be sharing the global variables. If you see the man page for fork, it tells you what things are shared between parent and child, and I dont think, global program variables is one of them. It is like having two different processes in memory share global variables of one process with other, hmm do u think that would work.
To share data across variables, what do you do commonly, use IPC techniques, so proabbaly that is what you should be looking for.

yes you are right !!!

parent and the child have different address space,
once child is created as a result of fork child gets a seperate copy of variables from parent.

Parent and the child share the following
file descriptors that are duplicated
file offset (unpredictable output to the file )

Here is simple code describing global variables are not shared between parent and child

#include <stdio.h>

int glb=1;

int main()
{
int val;
int pid;
pid=fork();
if(pid == 0 )
{
while (1)
{
printf("Child: %d\n", glb);
sleep(12);
glb++;
}
}
if ( pid > 0 )
{
while (1)
{
printf("Parent : %d\n", glb);
sleep(2);
glb++;
}
}
return 0;
}

hi

thanks for your attention

well if the child and the parent can not share their global structures so what do you think I can do for these
two process and why the threads of the software I am working on can change the global variable
maybe I have to use threads what do you think?

bye.

If you really must use direct variables, you can create a shared memory area with

#include <sys/mman.h>
#include <unistd.h>
// etc

// Allocates a memory area of one system page in size.  Size must always be
// a multiple of system pages.  Child processes will share this memory.
// You must control access to this memory somehow to prevent race conditions.
char *shared_mem=(char *)mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
  MAP_SHARED|MAP_ANONYMOUS, -1,0);

// Write to memory
strcpy(shared_mem,"Hello World");

// Print string in memory
printf("%s\n",shared_mem);

// Get rid of memory
munmap(shared_mem,getpagesize());

See "man mmap" for details.

You can also use pipes, sockets, and System V IPC to communicate.

Threads all access the same global variables because threads exist inside one process, whereas a child process is a whole new process with it's whole own memory space.

The link below is an interesting one to start to learn IPCS. shared memory is one of the IPC technique. you have many of them.
And yes if you know how to use pthreads, go ahead and use them. Yes threads share variables, but again not all. The link below can has chapter on IPC and even threads.

http://www.cs.cf.ac.uk/Dave/C/CE.html