# man > fork(3am)

---
type: CommandReference
command: fork
mode: man
section: 3am
source: man-pages
---

## Quick Reference

- `fork()` — create a new process; returns 0 in child, PID in parent, -1 on error
- `waitpid(pid)` — wait for a specific child process to terminate
- `wait()` — wait for the first child to terminate

## Name

fork, wait, waitpid — basic process management for gawk

## Synopsis

shell
@load "fork"
pid = fork()
ret = waitpid(pid)
ret = wait()
## Options / Functions

- `fork()` — Creates a new process. Returns 0 in the child, the child's PID in the parent, or -1 on error (sets `ERRNO`). Updates `PROCINFO["pid"]` and `PROCINFO["ppid"]` in the child.
- `waitpid(pid)` — Waits for the process with given PID. Returns the value of the `waitpid(2)` system call.
- `wait()` — Waits for the first child to die. Returns the value of the `wait(2)` system call.

### Limitations

- No corresponding `exec()` function is provided.
- The interface could be enhanced to provide more facilities (e.g., extracting return status bits).

## Examples

awk
@load "fork"
...
if ((pid = fork()) == 0)
    print "hello from the child"
else
    print "hello from the parent"
## See Also

- [GAWK: Effective AWK Programming](https://www.chedong.com/phpMan.php/man/GAWK/1/markdown)
- [filefuncs(3am)](https://www.chedong.com/phpMan.php/man/filefuncs/3am/markdown), [fnmatch(3am)](https://www.chedong.com/phpMan.php/man/fnmatch/3am/markdown), [inplace(3am)](https://www.chedong.com/phpMan.php/man/inplace/3am/markdown), [ordchr(3am)](https://www.chedong.com/phpMan.php/man/ordchr/3am/markdown), [readdir(3am)](https://www.chedong.com/phpMan.php/man/readdir/3am/markdown), [readfile(3am)](https://www.chedong.com/phpMan.php/man/readfile/3am/markdown), [revoutput(3am)](https://www.chedong.com/phpMan.php/man/revoutput/3am/markdown), [rwarray(3am)](https://www.chedong.com/phpMan.php/man/rwarray/3am/markdown), [time(3am)](https://www.chedong.com/phpMan.php/man/time/3am/markdown)
- [fork(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown), [wait(2)](https://www.chedong.com/phpMan.php/man/wait/2/markdown), [waitpid(2)](https://www.chedong.com/phpMan.php/man/waitpid/2/markdown)