home *** CD-ROM | disk | FTP | other *** search
- From: sk@hpcupt3.cup.hp.com (S Kandasamy)
- Date: Fri, 14 Aug 1992 23:17:32 GMT
- Subject: Re: Parents and children
- Message-ID: <118390007@hpcupt3.cup.hp.com>
- Organization: Hewlett Packard, Cupertino
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!hpscdc!hplextra!hpcc05!hpcuhb!hpcupt3!sk
- Newsgroups: comp.unix.questions
- References: <1992Aug12.085025.14750@nuscc.nus.sg>
- Lines: 31
-
- / hpcupt3:comp.unix.questions / ccechk@nuscc.nus.sg (Heng Kek) / 1:50 am Aug 12, 1992 /
- >From preliminary tests from simple programs, I notice that parents
- >and child processes do not share the same data. To be more
- >specific, allow me to illustrate with a simple example:
- >
- >freddy = 888;
- >if(fork()==0){
- > freddy = 111;
- > exit;
- >}
- >printf("freddy is %d\n", freddy); /* prints 888 */
- >
- >Question: What must I do so that the variable 'freddy' is 'updated'
- >in the parent? Must I use pipes and make parent and child
- >communicate the desired change in variable values?
- >
- >Sorry if this seems trivial, and thanks for any responses.
- >
- >Kek
- >"Relatively new to parent/child processes"
- >----------
-
- Use shared memory to have the common data that is shared between parent and
- child. See the man pages of shmget(2) to create a shared memory segment and
- shmop(2) to attach it. The parent attaches using shmop(2) and forks the child
- also can use the address space. Since both and parent shares the same memory
- addresses you should exercise caution in updating as there could be race
- conditions(like parent updates before child reads ... etc.). You may have
- to use the semaphores to synchronize the shared memory operations.
-
- SK.
-