the scop of variables in child process


 
Thread Tools Search this Thread
Top Forums Programming the scop of variables in child process
# 1  
Old 01-05-2006
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)<0)
{
printf("\n pipe error");
exit(0);
}

if ((pid=fork())<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<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
# 2  
Old 01-05-2006
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.
# 3  
Old 01-05-2006
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
Code:
#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;
}

# 4  
Old 01-06-2006
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.
# 5  
Old 01-06-2006
Quote:
Originally Posted by netman
Hi.

Thanks for your attention.

Well, if the child and the parent can not share their global structures, what do you think I can do for these two processes, and why can the threads of the software I am working on 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
Code:
#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.
# 6  
Old 01-06-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

2. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

3. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

4. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

5. Shell Programming and Scripting

Child Process Name

Hi , I want to find out the child process name given its PID. I have used the ps command but it displays the parent process name against child PID. Is there any way to find out name of child program executing under any parent program? (1 Reply)
Discussion started by: sneha_heda
1 Replies

6. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

7. Shell Programming and Scripting

killing a child process

I am calling another script from my main script and making it run in the background,based upon the value of the input provided by the user I want to kill the child process ,I have written this code timer.ksh & PID=$$ print "\n Do you wish to continue .. (Y/N) : \c " read kill_proc if ]... (4 Replies)
Discussion started by: mervin2006
4 Replies

8. UNIX for Dummies Questions & Answers

about child process

hello every one, i want to know more about creation of child process. UNDER WHAT CIRCUMSTANCES child process is created? WHAT ARE THE PREREQUISITES for a child process to be created? let us say we have a prog.c, prog.obj(compiled.c),.a\.out files. is any child PROCESS CREATED... (12 Replies)
Discussion started by: compbug
12 Replies

9. UNIX for Dummies Questions & Answers

KDM child process

Hello all, I got this little problem. I don't know what happen, but its not stopping work but is more of an FYI. I have this funny process running when I do ps -aef (on RedHat AS3 ) server I get this funny child process. root 2345 1 .... /usr/bin/kdm -nodaemon root... (6 Replies)
Discussion started by: larryase
6 Replies

10. Programming

Reference Variables To A Child Process Created With Fork

Hi! IN THE FOLLOWING PROGRAM THE VALUE OF j REMAINS UNCHANGED . WHY ? IF I WANT A VARIABLE VALUE TO CHANGE LIKE THIS , IS THERE ANY WAY TO DO IT ? Or do we have to use shared memory variables? main() { int return_pid, i, total; int j=1; total = TOTALRECS+1; for... (2 Replies)
Discussion started by: AJAY BHATIA
2 Replies
Login or Register to Ask a Question