LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-10-2005, 02:50 AM   #1
johnhardey
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Rep: Reputation: 0
GCC fork() - Can a child change a parent variable?


Here is are pieces of my code:
Code:
<includes> ...

void recv_msg(char *msg);

int turn;

int main() {
     turn = 0;

     ...

     if(!fork()) {
          recv_msg("1");
     }

     ...

     while(1) {
          if(turn == 0)  printf("turn is 0");
          else  printf("turn is 1");
     }
}

void recv_msg(char *msg) {
     turn = atoi(msg);
}
my code is somewhat like this. It does not produce any error in compilation or runtime. I already checked if the child process went to zombie because of some error in runtime but it is still running. I also placed several printf()'s to check whether the parent and child are running simultaneously and they are.

So is it possible to change the int variable turn using the child process or there might be some other bad thing I am doing?

*Cheers
 
Old 03-10-2005, 06:48 AM   #2
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
sorry, i cant answer your question, but i noticed the following mistake.

if(!fork()) {
recv_msg("1");
}

should be

if(!fork()) {
recv_msg('1');
}
 
Old 03-10-2005, 07:34 AM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
No, it shouldn't. If you look at the signature of his recv_msg() function it expects one parameter of type pointer-to-char (should really be const char *), not just a char.
 
Old 03-10-2005, 10:03 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'm no expert on forking, but as I understand it, when you fork, you create an entirely new process (complete with its own distinct memory space). Therefore, and attempts to modify variables would modify the variables in the new process's memory space. Provided that's true, then the only way you could have the fork'ed process modify the original process's variables would be to pass a pointer to the memory of the original process. You could do that with a file, a pipe, or perhaps some other mechanism.
 
Old 03-10-2005, 10:49 AM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Quote:
then the only way you could have the fork'ed process modify the original process's variables would be to pass a pointer to the memory of the original process.
That won't work either because linux (or any modern operating system) uses virtual memory. No matter what address you choose in the child, 0x00000000 to 0xffffffff, it won't point to memory inside the parent. If you want a child and a parent which are able to affect eachothers' memory, you should use threads instead of processes. Other ways to handle communication between processos are pipes via pipe(), file i/o, messages via msgget() and friends, shared memory with shmget() and friends, communication over tcp or unix domain sockets, unix signals via kill(2), or, in very narrow cases, the exit code given to exit() in the child. (which the parent can see with wait(2) or waitpid(2)). There's More Than One Way To Do It(tm).

One thing you should watch out for with fork() is that you make sure the child process exits when it's supposed to. In your code it looks like the child will fall out of the if statement and begin executing the same code that the parent is supposed to. (You can fix this by having it exit() appropriately.)

Last edited by aluser; 03-10-2005 at 01:10 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change environment variables of a parent envrionment from a child one. pulsez Linux - Software 1 11-09-2005 06:24 AM
change child name (c fork) alaios Programming 15 09-11-2005 04:51 PM
Getting a parent to communicate with its child -- fork() kamel Programming 3 06-02-2004 03:04 AM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM
child-parent-&-fork mukul Programming 2 03-24-2001 01:12 PM


All times are GMT -5. The time now is 02:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration