C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
sharing variables on using fork Posted by gautam on 18 Aug 2006 at 4:24 AM
Hi,

Is there a way to share variables when using fork() with the parent and child other than by using somekind of file descriptor. As in if the child makes a change with the variable then the parent process should also see the change.

Thanks
Report
Re: sharing variables on using fork Posted by Lundin on 18 Aug 2006 at 6:06 AM
: Hi,
:
: Is there a way to share variables when using fork() with the parent and child other than by using somekind of file descriptor. As in if the child makes a change with the variable then the parent process should also see the change.
:
: Thanks
:


I don't know the answer to your question, but the reason why fork() is sort of obsolete is because it makes a copy of a whole process - including the process' memory space. The modern way is to use threads instead, they will share the same memory. Maybe it would be possible to use pthreads instead? (assuming that you are using unix)
Report
Re: sharing variables on using fork Posted by gautam on 17 Sept 2006 at 10:24 PM
Hi,

I did end up using pthreads but did find the answer to my question. In case someone wants to know - there are functions shmat, shmget, shm* which allow shared memory access across processes which can be used when using fork to create a process.

: : Hi,
: :
: : Is there a way to share variables when using fork() with the parent and child other than by using somekind of file descriptor. As in if the child makes a change with the variable then the parent process should also see the change.
: :
: : Thanks
: :
:
:
: I don't know the answer to your question, but the reason why fork() is sort of obsolete is because it makes a copy of a whole process - including the process' memory space. The modern way is to use threads instead, they will share the same memory. Maybe it would be possible to use pthreads instead? (assuming that you are using unix)
:

Report
Re: sharing variables on using fork Posted by bluj91 on 15 Nov 2006 at 3:45 PM
: Hi,
:
: I did end up using pthreads but did find the answer to my question. In case someone wants to know - there are functions shmat, shmget, shm* which allow shared memory access across processes which can be used when using fork to create a process.
:
: : : Hi,
: : :
: : : Is there a way to share variables when using fork() with the parent and child other than by using somekind of file descriptor. As in if the child makes a change with the variable then the parent process should also see the change.
: : :
: : : Thanks
: : :
: :
: :
: : I don't know the answer to your question, but the reason why fork() is sort of obsolete is because it makes a copy of a whole process - including the process' memory space. The modern way is to use threads instead, they will share the same memory. Maybe it would be possible to use pthreads instead? (assuming that you are using unix)
: :
:
:

The replys to your question were weird. People use threads and processes for different reasons. ie. Processes are safer and threads are more efficent. To communicate between processes you can use sockets. I do not have much experience with UNIX sockets because I've limiting to using windows however the little I know of them come beej's guide to UNIX IPC avalible at http://beej.us/guide/

Good luck
Report
Re: sharing variables on using fork Posted by Lundin on 23 Nov 2006 at 1:18 AM
: : Hi,
: :
: : I did end up using pthreads but did find the answer to my question. In case someone wants to know - there are functions shmat, shmget, shm* which allow shared memory access across processes which can be used when using fork to create a process.
: :
: : : : Hi,
: : : :
: : : : Is there a way to share variables when using fork() with the parent and child other than by using somekind of file descriptor. As in if the child makes a change with the variable then the parent process should also see the change.
: : : :
: : : : Thanks
: : : :
: : :
: : :
: : : I don't know the answer to your question, but the reason why fork() is sort of obsolete is because it makes a copy of a whole process - including the process' memory space. The modern way is to use threads instead, they will share the same memory. Maybe it would be possible to use pthreads instead? (assuming that you are using unix)
: : :
: :
: :
:
: The replys to your question were weird.

As opposed to replying to 2 month old posts...?

:People use threads and processes for different reasons. ie. Processes are safer and threads are more efficent. To communicate between processes you can use sockets. I do not have much experience with UNIX sockets because I've limiting to using windows however the little I know of them come beej's guide to UNIX IPC avalible at http://beej.us/guide/
:
: Good luck
:


What I ment when I said that fork() is obsolete is that it creates a copy of the current process rather than making a new, dedicated one. Unix programmers tend to use fork() for one reason only, and that is that they want multitasking, no matter how. I wouldn't call threads less safe if you know what you are doing. Further, posix threads are probably safer than Win32 threads because the former comes with all kind of nice, standardized thread management libraries.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.