---
title: "System Calls"
canonical: "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/InputOutput/system-calls-2.html"
source: "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/InputOutput/system-calls-2.md"
part: "The Magma Language"
chapter: "Input and Output"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "The Magma Language", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/index-magma-language.md"}
  - {"title": "Input and Output", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/InputOutput/index-input-output.md"}
prev: {"title": "Memory Usage", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/InputOutput/system-calls.md"}
next: {"title": "Creating Names", "source": "https://docs.magma-maths.org/v2.30-1/MagmaLanguage/InputOutput/create-name.md"}
intrinsics:
  - {"name": "Alarm", "signature": "Alarm(s): RngIntElt"}
  - {"name": "ChangeDirectory", "signature": "ChangeDirectory(s): MonStgElt"}
  - {"name": "GetCurrentDirectory", "signature": "GetCurrentDirectory()"}
  - {"name": "Getpid", "signature": "Getpid()"}
  - {"name": "Getuid", "signature": "Getuid()"}
  - {"name": "Fork", "signature": "Fork() -> RngIntElt"}
  - {"name": "System", "signature": "System(C): MonStgElt -> RngIntElt"}
  - {"name": "%!", "signature": "%! shell-command"}
examples:
  - "ex-52e3aa"
---

# System Calls

<a id="procedure-alarm-rngintelt"></a>
## `Alarm(s): RngIntElt`

A procedure which when used on UNIX systems, sends the signal SIGALRM to the Magma process after $s$ seconds. This allows the user to specify that a Magma-process should self-destruct after a certain period.

<a id="procedure-changedirectory-monstgelt"></a>
## `ChangeDirectory(s): MonStgElt`

Change to the directory specified by the string $s$. Tilde expansion is allowed.

<a id="procedure-getcurrentdirectory"></a>
## `GetCurrentDirectory()`

Returns the current directory as a string.

<a id="procedure-getpid"></a>
## `Getpid()`

Returns Magma’s process ID (value of the Unix C system call `getpid()`).

<a id="procedure-getuid"></a>
## `Getuid()`

Returns the user ID (value of `the Unix C system call getuid()`).

<a id="function-fork"></a>
## `Fork() -> RngIntElt`

```magma
ExitSummary: BoolElt                    Default: false
```

Execute the Unix/Linux system call `fork()` and return the process ID of the child (if now the parent), or return 0 (if now the child). If the parameter `ExitSummary` is set to `true`, then the child process prints the usual exit summary (`"Total time: ..."`) when it exits; otherwise this is suppressed (the default).

<a id="function-system-monstgelt"></a>
## `System(C): MonStgElt -> RngIntElt`

Execute the system command specified by the string $C$. This is done by calling the C function `system()`. This also returns the system command’s return value as an integer. On most Unix systems, the lower 8 bits of this value give the process status while the next 8 bits give the value given by the command to the C function `exit()` (see the Unix manual entries for `system(3)` or `wait(2)`, for example). Thus one should normally divide the result by 256 to get the exit value of the program on success. See also the `Pipe` intrinsic function.

<a id="example-ex-52e3aa"></a>
## `Example: Current Date from a System Call (ex-52e3aa)`

The `System` intrinsic can be used to access the current date and time.

```magma
> System("date");
Tue Feb 28 13:02:03 EST 2017
```

<a id="literal-literal-shell-command"></a>
## `%! shell-command`

Execute the given command in the Unix shell then return to Magma. Note that this type of shell escape (contrary to the one using a `System` call) takes place entirely outside Magma and does not show up in Magma’s history.
