deep19
deep19

Reputation: 91

fork() call in c

I have used fork() to create 2 different processes operating on 2 different address spaces. Now, in parent process I need the value of a variable from child's address space or if the child process can modify the variable in parent's address space. Is this possible?

Upvotes: 3

Views: 245

Answers (4)

Anshul garg
Anshul garg

Reputation: 233

As you have created two process using fork command Both Process will be in different address space so they will only communicate by IPC, message passing ,Piping , Shared Memory etc. otherwise one process can't access other process data as thay do have Process specific data and similarly threads also have thread specific data

Upvotes: 0

masoud
masoud

Reputation: 56479

If you mean exchanging data between these two processes you can not. You can do it by system APIs like SharedMemory, Message Passing, Pipeline, Socket, ...

Upvotes: 2

mouviciel
mouviciel

Reputation: 67829

Once you have two processes, sharing data needs interprocess communication: file, pipe or shared memory.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881403

No, once you've forked, each process gets its own address space and you'll have to look into either:

  • some form of IPC between the processes to access each others data (such as shared memory or message queues).
  • some more lighweight variant of fork that allows sharing of data (including possibly threading).

Upvotes: 5

Related Questions