LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   GCC fork() - Can a child change a parent variable? (https://www.linuxquestions.org/questions/programming-9/gcc-fork-can-a-child-change-a-parent-variable-299903/)

johnhardey 03-10-2005 02:50 AM

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

qwijibow 03-10-2005 06:48 AM

sorry, i cant answer your question, but i noticed the following mistake.

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

should be

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

Hivemind 03-10-2005 07:34 AM

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.

Dark_Helmet 03-10-2005 10:03 AM

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.

aluser 03-10-2005 10:49 AM

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


All times are GMT -5. The time now is 05:36 AM.