Kill child process created with fork

user2953575

The following will output :

Start of script
PID=29688
Start of script
PID=0
Running child process 1
Done with child process
PID=29689
Start of script
PID=0
Running child process 1
Done with child process

It's working as intended, however I would like to kill the previous child PID.

How can a kill the PID of the child with out kiling the MAIN ?

Thank you !

my $bla = 1;

while (1) {

print "Start of script\n";
run_sleep();

}

sub run_sleep {

    sleep(3);
    my $pid = fork;

    return if $pid;     # in the parent process
    print("PID=" . $pid . "\n");
    print "Running child process " . $bla++ . "\n";
    exit(0);  # end child process

}
Len Jaffe

When you fork a child, and then fail to wait() on it, it will become a defunct process (a zombie in Unix parlance) when it exits. You'll notice that its parent process ID becomes 1, and it will not go away until the OS rebooted.

So the traditional pseudocode for forking looks something like this:

if ($pid = fork()) {
   # pid is non-zero, this is the parent
   waitpid($pid)   # tell OS that we care about the child

   do other parental stuff
}
else {
   # pid is 0 so this is the child process
   do_childish_things()
}

Your code doesn't do that, so you're probably getting zombies, and then getting frustrated that you can't get rid of them.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Returning a value from the child process to its parent in C created with fork()

分類Dev

How to kill a child process started by process in java?

分類Dev

python subprocess.Popen kill process with child process

分類Dev

Parent and child process end randomly (linux, c, fork())

分類Dev

How to create shared memory after fork or in child process?

分類Dev

Git Bash Error: Could not fork child process: Permission denied (-1)

分類Dev

Why do `login` and `sudo` fork a child process, and not exec() straight away?

分類Dev

when parent process is killed by 'kill -9', how to terminate child processes

分類Dev

How can I kill a child process without the child process 'exit' event triggering?

分類Dev

Supervisor.restart_child / 2またはProcess.exit(pid、:kill)?

分類Dev

How to make sure that a child process of a supervisor job dies when I kill supervisor

分類Dev

Calling fork on a multithreaded process

分類Dev

Check if process is running and kill it

分類Dev

kill unresponsive process

分類Dev

Kill Local Process

分類Dev

Kill #### but process still there?

分類Dev

Capturing kill code of go process?

分類Dev

How to kill a process on a port on ubuntu

分類Dev

Kill system process in Thread ruby

分類Dev

How to kill process of another user?

分類Dev

How to kill a daemon process in linux?

分類Dev

Kill my process if the other process is killed

分類Dev

in vfork child are created repeatedly

分類Dev

child_process.fork()から結果を取得し、クライアントに応答します

分類Dev

このカーネルメッセージの解釈方法:cgroup out of memory:Kill process 1234 .... score 1974 or sacrificed child?

分類Dev

Forward aliases to child process

分類Dev

How to kill a running process using ansible?

分類Dev

Trying to find and kill a process by PowerShell script

分類Dev

Cannot Kill a process that has a thread started

Related 関連記事

  1. 1

    Returning a value from the child process to its parent in C created with fork()

  2. 2

    How to kill a child process started by process in java?

  3. 3

    python subprocess.Popen kill process with child process

  4. 4

    Parent and child process end randomly (linux, c, fork())

  5. 5

    How to create shared memory after fork or in child process?

  6. 6

    Git Bash Error: Could not fork child process: Permission denied (-1)

  7. 7

    Why do `login` and `sudo` fork a child process, and not exec() straight away?

  8. 8

    when parent process is killed by 'kill -9', how to terminate child processes

  9. 9

    How can I kill a child process without the child process 'exit' event triggering?

  10. 10

    Supervisor.restart_child / 2またはProcess.exit(pid、:kill)?

  11. 11

    How to make sure that a child process of a supervisor job dies when I kill supervisor

  12. 12

    Calling fork on a multithreaded process

  13. 13

    Check if process is running and kill it

  14. 14

    kill unresponsive process

  15. 15

    Kill Local Process

  16. 16

    Kill #### but process still there?

  17. 17

    Capturing kill code of go process?

  18. 18

    How to kill a process on a port on ubuntu

  19. 19

    Kill system process in Thread ruby

  20. 20

    How to kill process of another user?

  21. 21

    How to kill a daemon process in linux?

  22. 22

    Kill my process if the other process is killed

  23. 23

    in vfork child are created repeatedly

  24. 24

    child_process.fork()から結果を取得し、クライアントに応答します

  25. 25

    このカーネルメッセージの解釈方法:cgroup out of memory:Kill process 1234 .... score 1974 or sacrificed child?

  26. 26

    Forward aliases to child process

  27. 27

    How to kill a running process using ansible?

  28. 28

    Trying to find and kill a process by PowerShell script

  29. 29

    Cannot Kill a process that has a thread started

ホットタグ

アーカイブ