parenp process controlling child

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default parenp process controlling child

    How can parent process control a child??
    I tried to kill the child, but the code didn;t worked to my
    expectation.

    Actually i wrote the following code -

    # fork first child
    if(defined($pid= fork))
    {
    print "1) pare $pid \n\n";
    if($pid) ## If parent process then fork another child to start
    timer
    { # fork new child
    my $child_pid = fork;

    # if parent process then wait for one of the child to terminate
    if($child_pid)
    { # wait for children
    my $terminated_id = wait;

    # kill the still active child
    if($terminated_id == $pid)
    { kill 9, $child_pid; }
    else
    { kill 9, $pid ; }
    }
    else ## child that start the timer
    {
    print "2 start child $child_pid \n";
    my ($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;

    LOOP_ON_TIMER :
    print "\nLOOOOOOOP
    ($luser_time,$lsys_time,$lcu_time,$lcs_time)\n\n";
    ($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;

    goto LOOP_ON_TIMER if ($luser_time <= 2);
    print "\t\t 2) child $pid exiting\n\n";
    }
    }
    else # if first child, then fire the test case
    { print "1 start child $pid";
    &run_test_case($i);
    $flag =1;

    }
    }

    Here parent forks two childeren,- One childeren is suppose to simulate
    some system task ..
    another child works as watch dog.

    bothchilderen and paren should terminate once any of the child
    terminates....

    Can anyone suggest me better way to implement this ??

    Thanks in advance ...
    Monika Talwar Guest

  2. Similar Questions and Discussions

    1. #37998 [Asn->Fbk]: Parent process lost MySQLi connection after child process gone
      ID: 37998 Updated by: tony2001@php.net Reported By: dbs at is dot ua -Status: Assigned +Status: ...
    2. How to get the child process pid ?
      Dear all , I would like to find an (easy) way to automatically get the process id of the latest child process of a user per tty. (the process...
    3. Beginners question : controlling an child
      I have an 'beginners" question..... I use the following line tot rotate my 3dmax model member("Filename").model("car").rotate(0,0,1,#self) ...
    4. How to impersonate the child process of an ASP.NET application
      I am running a web service on IIS 6.0, impersonation is set to true, so my web service can access resources depending on the client's privileges,...
    5. Forking child process with WWW::Automate
      Hi all, I'm attempting to automate a login to a web server with WWW::Automate and then to continue navigating through the website. I've hit a...
  3. #2

    Default Re: parenp process controlling child

    Monika Talwar <monikatalwar@hotmail.com> wrote in comp.lang.perl.misc:
    > How can parent process control a child??
    > I tried to kill the child, but the code didn;t worked to my
    > expectation.
    >
    > Actually i wrote the following code -
    >
    > # fork first child
    > if(defined($pid= fork))
    > {
    > print "1) pare $pid \n\n";
    > if($pid) ## If parent process then fork another child to start
    > timer
    > { # fork new child
    > my $child_pid = fork;
    >
    > # if parent process then wait for one of the child to terminate
    > if($child_pid)
    > { # wait for children
    > my $terminated_id = wait;
    >
    > # kill the still active child
    > if($terminated_id == $pid)
    > { kill 9, $child_pid; }
    > else
    > { kill 9, $pid ; }
    > }
    Signal 9 is a little radical. It should only be used when a process
    can't be killed by other means. I'd use a TERM signal.
    > else ## child that start the timer
    > {
    > print "2 start child $child_pid \n";
    > my ($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;
    >
    > LOOP_ON_TIMER :
    > print "\nLOOOOOOOP
    > ($luser_time,$lsys_time,$lcu_time,$lcs_time)\n\n";
    > ($luser_time,$lsys_time,$lcu_time,$lcs_time) = times;
    >
    > goto LOOP_ON_TIMER if ($luser_time <= 2);
    > print "\t\t 2) child $pid exiting\n\n";
    > }
    > }
    I don't think this loop times what you want to time. You are waiting
    for this child process to accumulate two seconds of CPU time. Depending
    on system load, this could take any amount of real time from two seconds
    upwards. I expect you actually want to wait for two seconds real time.

    Also, what you have there is a busy loop -- the process will continuously
    be actively querying its CPU time and produce system load.

    To repair both, use the sleep() command (see perldoc -f sleep).
    > else # if first child, then fire the test case
    > { print "1 start child $pid";
    > &run_test_case($i);
    > $flag =1;
    >
    > }
    > }
    >
    > Here parent forks two childeren,- One childeren is suppose to simulate
    > some system task ..
    > another child works as watch dog.
    You don't need a separate process to establish a time limit for a
    process. Simply set an alarm timer (perldoc -f alarm). It will
    kill your process after a set time.

    If you need to do things after the alarm timer fires, there is
    "perldoc -q 'slow event'" which shows how to catch the timer
    signal.
    > bothchilderen and paren should terminate once any of the child
    > terminates....
    >
    > Can anyone suggest me better way to implement this ??
    You don't need a separate process for the timeout. Set an alarm timer
    in the working child process. Your use of "wait" to let the parent die
    with the kid looks fine (I didn't run your progeam). It will be
    simplified when there is only one process to wait for.

    Anno
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139