fork command.

Discussion in 'Perl Misc' started by rajendra, Jul 26, 2007.

  1. rajendra

    rajendra Guest

    Hello All,
    The perl documentation says the fork command generates two copies of the
    program ,one parent and one child.
    If this is correct,can this fork command be used for multitasking?.
     
    rajendra, Jul 26, 2007
    #1
    1. Advertising

  2. rajendra

    -berlin.de Guest

    rajendra <> wrote in comp.lang.perl.misc:
    > Hello All,
    > The perl documentation says the fork command generates two copies of the
    > program ,one parent and one child.
    > If this is correct,can this fork command be used for multitasking?.


    Yes, that is its very purpose.

    Anno
     
    -berlin.de, Jul 26, 2007
    #2
    1. Advertising

  3. On Thu, 26 Jul 2007 15:43:24 +0530, "rajendra"
    <> wrote:

    >The perl documentation says the fork command generates two copies of the
    >program ,one parent and one child.
    >If this is correct,can this fork command be used for multitasking?.


    Well, yes: but what do you mean exactly? AIUI multitasking is better
    seen as a property of "some" osen and fork() is one way to use it to
    one's advantage...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jul 26, 2007
    #3
  4. rajendra wrote:
    > The perl documentation says the fork command generates two copies of
    > the program ,one parent and one child.


    Well, no. It creates _one_ additional copy of the running process, commonly
    known as child process.

    > If this is correct,can this fork command be used for multitasking?.


    That is its main purpose, yes.

    jue
     
    Jürgen Exner, Jul 26, 2007
    #4
  5. rajendra

    Ted Zlatanov Guest

    On 26 Jul 2007 11:29:14 GMT -berlin.de wrote:

    a> rajendra <> wrote in comp.lang.perl.misc:
    >> The perl documentation says the fork command generates two copies of the
    >> program ,one parent and one child.
    >> If this is correct,can this fork command be used for multitasking?.


    a> Yes, that is its very purpose.

    I usually point people to the Stevens books (Advanced Programming in the
    Unix Environment 2nd ed., especially) or some equivalent tutorial,
    whenever they ask about fork(). I think using fork() requires
    understanding the way it works on the OS level--at least IPC and IO
    stream behavior, if nothing else.

    Ted
     
    Ted Zlatanov, Jul 26, 2007
    #5
  6. rajendra

    -berlin.de Guest

    Ted Zlatanov <> wrote in comp.lang.perl.misc:
    > On 26 Jul 2007 11:29:14 GMT -berlin.de wrote:
    >
    > a> rajendra <> wrote in comp.lang.perl.misc:
    > >> The perl documentation says the fork command generates two copies of the
    > >> program ,one parent and one child.
    > >> If this is correct,can this fork command be used for multitasking?.

    >
    > a> Yes, that is its very purpose.
    >
    > I usually point people to the Stevens books (Advanced Programming in the
    > Unix Environment 2nd ed., especially) or some equivalent tutorial,


    Just for the record, _Advanced Programming in the Unix Environment_
    itself is *not* a tutorial but a hard-core reference.

    Anno
     
    -berlin.de, Jul 27, 2007
    #6
  7. rajendra

    rajendra Guest

    One more query:
    Can I use a variable common to both child and parent process block?.
    "rajendra" <> wrote in message
    news:f89s46$i07$...
    > Hello All,
    > The perl documentation says the fork command generates two copies of the
    > program ,one parent and one child.
    > If this is correct,can this fork command be used for multitasking?.
    >
    >
    >
     
    rajendra, Jul 30, 2007
    #7
  8. rajendra

    -berlin.de Guest

    rajendra <> wrote in comp.lang.perl.misc:
    > One more query:
    > Can I use a variable common to both child and parent process block?.


    You can. Note that the same name addresses different variables in
    the parent and the child. They will have the same initial value in
    both, but changes in one don't reflect in the other.

    Anno
     
    -berlin.de, Jul 30, 2007
    #8
  9. rajendra

    rajendra Guest

    If i want to update(modify) a in both child and parent process, how can I do
    this?....
    <-berlin.de> wrote in message
    news:...
    > rajendra <> wrote in comp.lang.perl.misc:
    > > One more query:
    > > Can I use a variable common to both child and parent process block?.

    >
    > You can. Note that the same name addresses different variables in
    > the parent and the child. They will have the same initial value in
    > both, but changes in one don't reflect in the other.
    >
    > Anno
     
    rajendra, Jul 30, 2007
    #9
  10. On Mon, 30 Jul 2007 17:12:06 +0530, "rajendra"
    <> wrote:

    >If i want to update(modify) a in both child and parent process, how can I do
    >this?....


    You must establys some sort of IPC between them. Read

    perldoc perlipc

    to find out some ways to do so.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jul 30, 2007
    #10
  11. rajendra

    -berlin.de Guest

    rajendra <> wrote in comp.lang.perl.misc:
    > <-berlin.de> wrote in message
    > news:...
    > > rajendra <> wrote in comp.lang.perl.misc:


    Please don't top-post. I have moved your additional question in
    context.

    > > > One more query:
    > > > Can I use a variable common to both child and parent process block?.

    > >
    > > You can. Note that the same name addresses different variables in
    > > the parent and the child. They will have the same initial value in
    > > both, but changes in one don't reflect in the other.


    > If i want to update(modify) a in both child and parent process, how can I do
    > this?....


    Supposing "a" should be "a variable".

    You can't, at least not in the sense that you change a variable in one
    process and have the corresponding variable in another process change as
    well. If processes need to exchange information you need some kind of
    inter-process communication (IPC). See "perldoc perlipc" for
    possibilities.

    An alternative would be to use threads, which do support shared
    variables. However, Perl threads are laden with problems, most
    notably that many modules don't work with threads as they should.

    Anno
     
    -berlin.de, Jul 30, 2007
    #11
  12. rajendra

    Guest

    "rajendra" <> wrote:
    > <-berlin.de> wrote in message
    > news:...
    > > rajendra <> wrote in comp.lang.perl.misc:
    > > > One more query:
    > > > Can I use a variable common to both child and parent process block?.

    > >
    > > You can. Note that the same name addresses different variables in
    > > the parent and the child. They will have the same initial value in
    > > both, but changes in one don't reflect in the other.


    > If i want to update(modify) a in both child and parent process, how can I
    > do this?....


    (top posting fixed, please don't top post.)

    You can use forks::shared, or something like it. Although it is generally
    best to arrange things so you don't need to do what it is that you think
    you want to do.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jul 30, 2007
    #12
  13. rajendra

    Ted Zlatanov Guest

    On Mon, 30 Jul 2007 17:12:06 +0530 "rajendra" <> wrote:

    r> If i want to update(modify) a in both child and parent process, how
    r> can I do this?....

    As I mentioned earlier, you should learn about the Unix environment and
    what fork() does before you can understand the answer to this question.
    It's just not a simple answer, that's all. Here's the one-paragraph
    version:

    Unix processes use fork() to copy their memory space (I'm simplifying)
    and start another copy of themselves. Thus, any changes each copy makes
    won't affect the original. The copy doesn't keep a magic connection to
    the original process. Unlike lightweight thread models, Unix processes
    need to communicate using Unix IPC methods (sometimes this is called the
    heavyweight thread model). Thus, Unix processes can't communicate using
    their own functions *alone*. The nice thing is that you can use IPC to
    communicate between your fork()ed processes, *and* with other processes.

    I am suggesting that you learn this for your benefit; please don't take
    the simple answer of "you do it with module X" but instead learn what
    Unix actually does with processes and IPC. You'll be a much better
    Unix and Perl programmer if you do.

    Ted
     
    Ted Zlatanov, Jul 31, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Nichols

    objects and fork command?

    Doug Nichols, Aug 6, 2004, in forum: Perl
    Replies:
    1
    Views:
    823
  2. San Diego Guy
    Replies:
    0
    Views:
    582
    San Diego Guy
    Aug 7, 2003
  3. Lucas Cowald
    Replies:
    4
    Views:
    1,139
    Tohid
    Oct 23, 2003
  4. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    602
    Eric Snow
    Jan 8, 2009
  5. Jacob JKW
    Replies:
    12
    Views:
    213
    Jacob JKW
    Jun 20, 2008
Loading...

Share This Page