1

If I ask for memory size of an int from malloc and I create 'n' child processes from one parent. Is it posible for each child to update(add one) the value inside that memory, so by the end the parent process reads the value?

Miguel Guerra
  • 145
  • 1
  • 8
  • 9
    No. Each child has its own copy of the memory, which it can modify. It is wholly separate from each other child's copy of the memory, and also wholly separate from the parent's copy of the memory. The children cannot change the parent's copy of the memory (nor any sibling's copy). To do what you want, you need to use 'shared memory'. You will probably also need to deal with synchronizing access to the shared memory — at the least, the parent will need to know when each child has finished writing to its portion of the shared memory so it can safely get at the information the child wrote. – Jonathan Leffler Jun 13 '16 at 23:00
  • Thanks for the help! – Miguel Guerra Jun 13 '16 at 23:04

1 Answers1

5

No, there is no common memory between child and parent. To have a communication between child and parent you could use:

Shared memory // All POSIX systems, Windows

Pipes , (Example of Named Pipes) , Pipe tutorial // All POSIX systems, Windows

FIFO files // Most operating systems

sockets // Most operating systems

For more information on other methods check the Inter-process communication

Community
  • 1
  • 1
sg7
  • 6,108
  • 2
  • 32
  • 40