Skip to content

Instantly share code, notes, and snippets.

@manwar
Last active March 1, 2025 16:38
Show Gist options
  • Select an option

  • Save manwar/ea245ff7df5b3e43d2f30eb7daef84da to your computer and use it in GitHub Desktop.

Select an option

Save manwar/ea245ff7df5b3e43d2f30eb7daef84da to your computer and use it in GitHub Desktop.

Revisions

  1. manwar revised this gist Mar 1, 2025. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions process-lifecycle.md
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,8 @@ Processes can communicate and synchronize using various mechanisms e.g. pipes, m

    Let's show in `Perl` first:

    [**[Source Code]**](https://github.com/manwar/Concurrent-Parallel-Programming/blob/main/scripts/process-lifecycle/process.pl)

    ```
    #!/usr/bin/env perl
    @@ -68,6 +70,8 @@ Run the code:

    In `Python`, we would do something like below:

    [**[Source Code]**](https://github.com/manwar/Concurrent-Parallel-Programming/blob/main/scripts/process-lifecycle/process.py)

    ```
    #!/usr/bin/env python3
  2. manwar revised this gist Feb 24, 2025. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion process-lifecycle.md
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,6 @@ Let's show in `Perl` first:
    #!/usr/bin/env perl
    use v5.38;
    use POSIX ':sys_wait_h';
    sub worker {
    say "\tChild process (PID: $$) started.";
  3. manwar revised this gist Feb 22, 2025. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions process-lifecycle.md
    Original file line number Diff line number Diff line change
    @@ -4,27 +4,27 @@
    `Process Lifecycle` stages are listed as below:

    #### #1: Creation

    ***
    `Forking` is the common method of creating new process. In `Python`, you use `os.fork()` whereas in `Perl`, you use `fork()`.

    #### #2: Execution

    ***
    During execution stage, the `worker()` subroutine runs the child process, while the parent process waits for the child to finish using `os.waitpid()` in `Python` and `waitpid()` in `Perl`.

    #### #3: Termination

    ***
    Normal termination is when a process can terminate normally by completing its execution or by calling an exit function.

    Abnormal termination is when a process may terminate abnormally due to an error or a signal (e.g. `SIGKILL`), or being killed by another process.

    In `Python`, in the child process, you use `os._exit(0)` in `Python` to terminate and `exit(0)` in `Perl`.
    In `Python`, in the child process, you use `os._exit(0)` to terminate and `exit(0)` in `Perl`.

    #### #4: Cleanup

    ***
    The operating system releases resources and cleans up the process table entry.

    #### #5: IPC / Signals

    ***
    Processes can communicate and synchronize using various mechanisms e.g. pipes, message queues, shared memory, and sockets. It can handle signals for various events.

    Let's show in `Perl` first:
  4. manwar revised this gist Feb 22, 2025. 1 changed file with 77 additions and 0 deletions.
    77 changes: 77 additions & 0 deletions process-lifecycle.md
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,83 @@ The operating system releases resources and cleans up the process table entry.

    Processes can communicate and synchronize using various mechanisms e.g. pipes, message queues, shared memory, and sockets. It can handle signals for various events.

    Let's show in `Perl` first:

    ```
    #!/usr/bin/env perl
    use v5.38;
    use POSIX ':sys_wait_h';
    sub worker {
    say "\tChild process (PID: $$) started.";
    sleep(2);
    say "\tChild process (PID: $$) finished.";
    exit(0);
    }
    sub parent {
    say "Parent process (PID: $$) started.";
    my $pid = fork();
    if ($pid == 0) {
    worker();
    } else {
    say "\tChild process (PID: $pid) created.";
    waitpid($pid, 0);
    say "Parent process (PID: $$) finished.";
    }
    }
    parent();
    ```

    Run the code:

    $ perl process.pl
    Parent process (PID: 7654) started.
    Child process (PID: 7655) created.
    Child process (PID: 7655) started.
    Child process (PID: 7655) finished.
    Parent process (PID: 7654) finished.
    $

    In `Python`, we would do something like below:

    ```
    #!/usr/bin/env python3
    import os
    import time
    def worker():
    print(f"\tChild process (PID: {os.getpid()}) started.")
    time.sleep(2)
    print(f"\tChild process (PID: {os.getpid()}) finished.")
    os._exit(0)
    def parent():
    print(f"Parent process (PID: {os.getpid()}) started.")
    pid = os.fork()
    if pid == 0:
    worker()
    else:
    print(f"\tChild process (PID: {pid}) created.")
    os.waitpid(pid, 0)
    print(f"Parent process (PID: {os.getpid()}) finished.")
    if __name__ == "__main__":
    parent()
    ```

    Run the code now:

    $ py process.py
    Parent process (PID: 7668) started.
    Child process (PID: 7669) created.
    Child process (PID: 7669) started.
    Child process (PID: 7669) finished.
    Parent process (PID: 7668) finished.
    $

    -- [**Back to Index**](https://github.com/manwar/Concurrent-Parallel-Programming) --
    ***
  5. manwar created this gist Feb 22, 2025.
    32 changes: 32 additions & 0 deletions process-lifecycle.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    -- [**Back to Index**](https://github.com/manwar/Concurrent-Parallel-Programming) --
    ***

    `Process Lifecycle` stages are listed as below:

    #### #1: Creation

    `Forking` is the common method of creating new process. In `Python`, you use `os.fork()` whereas in `Perl`, you use `fork()`.

    #### #2: Execution

    During execution stage, the `worker()` subroutine runs the child process, while the parent process waits for the child to finish using `os.waitpid()` in `Python` and `waitpid()` in `Perl`.

    #### #3: Termination

    Normal termination is when a process can terminate normally by completing its execution or by calling an exit function.

    Abnormal termination is when a process may terminate abnormally due to an error or a signal (e.g. `SIGKILL`), or being killed by another process.

    In `Python`, in the child process, you use `os._exit(0)` in `Python` to terminate and `exit(0)` in `Perl`.

    #### #4: Cleanup

    The operating system releases resources and cleans up the process table entry.

    #### #5: IPC / Signals

    Processes can communicate and synchronize using various mechanisms e.g. pipes, message queues, shared memory, and sockets. It can handle signals for various events.


    -- [**Back to Index**](https://github.com/manwar/Concurrent-Parallel-Programming) --
    ***