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?

2
  • 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. Jun 13, 2016 at 23:00
  • Thanks for the help! Jun 13, 2016 at 23:04

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.