home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10104 < prev    next >
Encoding:
Internet Message Format  |  1992-08-15  |  1.6 KB

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