sharing variables on using fork - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

sharing variables on using fork

gautamgautam Posts: 642Member
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

Comments

  • LundinLundin Posts: 3,711Member
    : 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)
  • gautamgautam Posts: 642Member
    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)
    :

  • bluj91bluj91 Posts: 133Member
    : 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
  • LundinLundin Posts: 3,711Member
    : : 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.

    [blue]As opposed to replying to 2 month old posts...?[/blue]

    :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
    :


    [blue] 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.
    [/blue]
Sign In or Register to comment.