pcntl_fork

pcntl_fork(): int

Unix process forking (PCNTL extension, unavailable on Windows builds): the return value is the identity — the parent gets the child PID, the child gets 0; pair every fork with a waitpid reap to avoid zombies.

C C23

C23
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void) {
    /* fork(2) duplicates the process: the parent gets the child PID, the child gets 0 */
    printf("start\n");
    fflush(stdout); // flush before fork so the child does not duplicate buffered output
    pid_t pid = fork();
    if (pid < 0) {
        printf("fork failed\n");
        return 1;
    }
    if (pid == 0) {
        printf("child\n");
        return 0;
    }
    int status;
    waitpid(pid, &status, 0); // reap the child before printing
    printf("parent done\n");
    return 0;
}
Output
start
child
parent done

C++ C++26

C++26
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
    // C++ on Unix uses the same fork(2)+waitpid(2) pair as C
    printf("start\n");
    fflush(stdout); // flush before fork so the child does not duplicate buffered output
    pid_t pid = fork();
    if (pid < 0) {
        printf("fork failed\n");
        return 1;
    }
    if (pid == 0) {
        printf("child\n");
        return 0;
    }
    int status;
    waitpid(pid, &status, 0); // reap the child before printing
    printf("parent done\n");
    return 0;
}
Output
start
child
parent done

C# v11.0.100

v11.0.100
// The CLR has no fork: Task.Run is the in-process concurrency unit
var task = Task.Run(() => Console.WriteLine("worker"));
task.Wait(); // the analogue of waitpid: block until the worker task ends
Console.WriteLine("main done");
Output
worker
main done

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"sync"
)

func main() {
	// Go has no fork: goroutines share one address space, there is no PID duality
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		fmt.Println("worker")
	}()
	wg.Wait() // the analogue of waitpid: block until the child task ends
	fmt.Println("main done")
}
Output
worker
main done

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) throws InterruptedException {
        // The JVM has no fork: threads are the in-process concurrency unit
        Thread t = new Thread(() -> System.out.println("worker"));
        t.start();
        t.join(); // the analogue of waitpid: block until the worker thread ends
        System.out.println("main done");
    }
}
Output
worker
main done

JavaScript v26.13.1

v26.13.1
// JavaScript has no fork: one thread runs the event loop; async tasks interleave
async function main() {
  await new Promise((resolve) => {
    console.log("worker");
    resolve();
  });
  console.log("main done");
}
main();
Output
worker
main done

PHP v8.6.0

v8.6.0
// fork() duplicates the process: parent gets the child PID, child gets 0
echo "start\n";
$pid = pcntl_fork();
if ($pid === -1) {
    echo "fork failed\n";
    exit(1);
}
if ($pid === 0) {
    // child branch: mark and exit immediately so the parent can reap us
    echo "child\n";
    exit(0);
}
// parent branch: reap the child first, fixing the output order
pcntl_waitpid($pid, $status);
echo "parent done\n";
Output
start
child
parent done

Python v3.15.2

v3.15.2
import os
import sys

print("start")
sys.stdout.flush()  # flush before fork so the child does not duplicate buffered output
pid = os.fork()
if pid == 0:
    print("child")
    sys.stdout.flush()  # flush before _exit (it skips cleanup)
    os._exit(0)  # leave at once so the parent can reap us
os.waitpid(pid, 0)  # reap the child before printing
print("parent done")
Output
start
child
parent done

Rust v1.99.1

v1.99.1
use std::thread;

fn main() {
    // Rust has no fork in the standard library: threads share one address space
    let handle = thread::spawn(|| {
        println!("worker");
    });
    handle.join().unwrap(); // the analogue of waitpid: block until the child thread ends
    println!("main done");
}
Output
worker
main done

TypeScript v26.13.1

v26.13.1
// TypeScript has no fork: one thread runs the event loop; async tasks interleave
async function main(): Promise<void> {
  await new Promise<void>((resolve) => {
    console.log("worker");
    resolve();
  });
  console.log("main done");
}
main();
Output
worker
main done

Language differences

PHP: pcntl_fork is Unix-only (the PCNTL extension is not built on Windows, the function does not even exist); the return value is the identity — the parent branch gets the child PID, the child branch gets 0; in the demo the child prints its mark and exits immediately while the parent prints only after waitpid reaps it, so the output order is fully controlled; zombie defense = pair every fork with a wait-family call (waitpid/pcntl_wait).

C/C++: fork(2)+waitpid(2) are the same-family prototypes (<unistd.h>+<sys/wait.h>); fflush before fork prevents the child from duplicating buffered output.

Python: os.fork is the one-to-one counterpart; os._exit skips cleanup, so flush manually before exiting.

Go: no fork — goroutines sharing one address space is a deliberate design (fork copies file descriptors and connection state, a rich source of bugs); the waitpid analogue is WaitGroup/Channel.

Rust: no fork in the standard library (the nix crate offers unistd::fork, unsafe); thread + join is the safe counterpart.

Java/C#: the JVM/CLR single-process managed model has no fork; real subprocesses use ProcessBuilder/ProcessStartInfo, while thread join/Task.Wait is the semantic counterpart of waitpid.

JavaScript/TypeScript: a single-threaded event loop with no process concept; async/await is just sequencing.