Stdlib.Sys(3)
πΈ Stdlib.Sys πΈ
okay bestie, welcome to your OCaml library era β this is the <pink>Stdlib.Sys</pink> page, i.e. the section 3 manual for the Sys module. it's giving "everything your program needs to know about the world it's living in" and honestly? iconic. π
β¨ Module β¨
π Module Sys
: (module Stdlib__Sys)
like, that's the whole vibe. it's a module. it's Stdlib__Sys. moving on, bestie, because there's a LOT. π«Ά
π Documentation π
val argv : string arrayβ the command-line arguments the process got, fr fr. element zero is the command name used to invoke the program, and everything after that is the actual command-line args. main character isargv.(0), no cap. βval executable_name : stringβ the name of the file containing the executable that's currently running. it may be absolute or relative to the current directory β depends on the platform and on whether the program was compiled to bytecode or to a native executable. so likeβ¦ don't assume, okay? πval runtime_executable : stringβ the name of the file containing the runtime that's currently running. for native code this is alwaysSys.executable_name, but in bytecode it may beocamlrun, for example. this name may be absolute or relative to the current directory depending on the platform (Linux,WindowsandmacOSshould all return absolute paths, slay). π«- (Since 5.5)
val file_exists : string -> boolβ test if a file with the given name exists. that's it. that's the whole function. vibe check passed. βval is_directory : string -> boolβ returnstrueif the given name refers to a directory,falseif it refers to another kind of file. ποΈ- (Since 3.10)
- Raises
Sys_errorif no file exists with the given name. π
val is_regular_file : string -> boolβ returnstrueif the given name refers to a regular file,falseif it refers to another kind of file. π- (Since 5.1)
- Raises
Sys_errorif no file exists with the given name. π
val remove : string -> unitβ remove the given file name from the file system. gone. deleted. not me being the one to do it though. πval rename : string -> string -> unitβ rename a file or directory.rename oldpath newpathrenames the file or directory calledoldpath, giving itnewpathas its new name, moving it between (parent) directories if needed. if a file namednewpathalready exists, its contents will be replaced with those ofoldpath. depending on the operating system, the metadata (permissions, owner, etc.) ofnewpathcan either be preserved or be replaced by those ofoldpathβ so like, platform-dependent, bestie, be aware. π₯- (Since 4.06 concerning the "replace existing file" behavior)
val getenv : string -> stringβ return the value associated to a variable in the process environment. π- Raises
Not_foundif the variable is unbound. π
- Raises
val getenv_opt : string -> string optionβ same energy, but returns the value associated to a variable in the process environment orNoneif the variable is unbound. no exceptions, just options. we love a safe queen. π- (Since 4.05)
val command : string -> intβ execute the given shell command and return its exit code. π
the argument of Sys.command is generally the name of a command followed by zero, one or several arguments, separated by whitespace. the given argument is interpreted by a shell: either the Windows shell cmd.exe for the Win32 ports of OCaml, or the POSIX shell sh for other ports. it can contain shell builtin commands such as echo, and also special characters such as file redirections > and <, which will be honored by the shell. β¨
conversely, whitespace or special shell characters occurring in command names or in their arguments must be quoted or escaped so that the shell does not interpret them. the quoting rules vary between the POSIX shell and the Windows shell. Filename.quote_command performs the appropriate quoting given a command name, a list of arguments, and optional file redirections. bestie, use it. π€
val time : unit -> floatβ return the processor time, in seconds, used by the program since the beginning of execution. β±οΈval chdir : string -> unitβ change the current working directory of the process. we're moving, girlies. π¦val mkdir : string -> int -> unitβ create a directory with the given permissions. π- (Since 4.12)
val rmdir : string -> unitβ remove an empty directory. oh and it has to be empty, okay? don't come for me. π- (Since 4.12)
val getcwd : unit -> stringβ return the current working directory of the process. where are we? who are we? πΈval readdir : string -> string arrayβ return the names of all files present in the given directory. names denoting the current directory and the parent directory ("."and".."in Unix) are not returned. each string in the result is a file name rather than a complete path. there is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order. so likeβ¦ don't sort-shame. π- (Since 4.12)
val io_buffer_size : intβ size of C buffers used by the runtime system and IO primitives of theunixlibrary. primitives that read from or write to values of typestringorbytesgenerally use an intermediate buffer of this size to avoid holding the domain lock. iconic efficiency behavior. π§- (Since 5.4)
val interactive : bool refβ this reference is initially set tofalsein standalone programs and totrueif the code is being executed under the interactive toplevel systemocaml. π
β οΈ Alertunsynchronized_access. theinteractivestatus is a mutable global state. be careful out there. π
val os_type : stringβ the operating system currently executing the OCaml program. one of:"Unix"(for all Unix versions, including Linux and Mac OS X), π"Win32"(for MS-Windows, OCaml compiled with MSVC++ or MinGW-w64), πͺ"Cygwin"(for MS-Windows, OCaml compiled with Cygwin). π§
type backend_type =β the moment you've been waiting for, a whole type:
type backend_type =
| Native
| Bytecode
| Other of string
currently, the official distribution only supports Native and Bytecode, but it can be other backends with alternative compilers, for example, javascript. π¦
- (Since 4.04)
val backend_type : backend_typeβ backend type currently executing the OCaml program. π- (Since 4.04)
val unix : boolβtrueifSys.os_type = "Unix". π- (Since 4.01)
val win32 : boolβtrueifSys.os_type = "Win32". πͺ- (Since 4.01)
val cygwin : boolβtrueifSys.os_type = "Cygwin". π§- (Since 4.01)
val word_size : intβ size of one word on the machine currently executing the OCaml program, in bits:32or64. β‘val int_size : intβ size ofint, in bits. it is31(resp.63) when using OCaml on a 32-bit (resp. 64-bit) platform. it may differ for other implementations, e.g. it can be32bits when compiling to JavaScript. π€―- (Since 4.03)
val big_endian : boolβ whether the machine currently executing the Caml program is big-endian. π- (Since 4.00)
val max_string_length : intβ maximum length of strings and byte sequences. πval max_array_length : intβ maximum length of a normal array (i.e. any array whose elements are not of typefloat). the maximum length of a float array ismax_floatarray_lengthif OCaml was configured with--enable-flat-float-arrayandmax_array_lengthif configured with--disable-flat-float-array. yes, this is real, yes you need both numbers. πΈval max_floatarray_length : intβ maximum length of afloatarray. this is also the maximum length of a float array when OCaml is configured with--enable-flat-float-array. πval runtime_variant : unit -> stringβ return the name of the runtime variant the program is running on. this is normally the argument given to-runtime-variantat compile time, but for byte-code it can be changed after compilation. π«- (Since 4.03)
val runtime_parameters : unit -> stringβ return the value of the runtime parameters, in the same format as the contents of theOCAMLRUNPARAMenvironment variable. π§- (Since 4.03)
val poll_actions : unit -> unitβ run any pending runtime actions, such as minor collections, major GC slices, signal handlers, finalizers, or memprof callbacks. like a little coffee break for the runtime, except productive. β- (Since 5.3)
π¦ Signal handling π¦
type signal = intβ the type for signal numbers. π
negative numbers are used by OCaml to provide a platform-independent number for signals recognised by OCaml. positive numbers are always the platform-dependent value for a given signal. the function Sys.signal_of_int converts known platform-dependent numbers to independent ones, and Sys.signal_to_int does the reverse. iykyk. π
- (Since 5.4)
type signal_behavior =β what to do when receiving a signal:
type signal_behavior =
| Signal_default
| Signal_ignore
| Signal_handle of (signal -> unit)
Signal_default: take the default behavior (usually: abort the program). π dramatic but fair.Signal_ignore: ignore the signal. not me ignoring my problems. πSignal_handle f: call functionf, giving it the signal number as an argument. π«Ά
val signal : signal -> signal_behavior -> signal_behaviorβ set the behavior of the system on receipt of a given signal. the first argument is the signal number. return the behavior previously associated with the signal. if the signal number is invalid (or not available on your system), anInvalid_argumentexception is raised. π¨
if a platform-dependent signal number is used, it will be converted to a platform-independent signal using Sys.signal_of_int before calling the handler. β¨
val set_signal : signal -> signal_behavior -> unitβ same asSys.signalbut the return value is ignored. lowkey the "i don't care what you were before, we're changing" version. π
π Signal numbers for the standard POSIX signals. π
val sigabrt : signalβ abnormal termination πval sigalrm : signalβ timeout β°val sigfpe : signalβ arithmetic exception βval sighup : signalβ hangup on controlling terminal π΄val sigill : signalβ invalid hardware instruction β οΈval sigint : signalβ interactive interrupt (ctrl-C) β¨οΈval sigkill : signalβ termination (cannot be ignored) β οΈval sigpipe : signalβ broken pipe π°val sigquit : signalβ interactive termination πval sigsegv : signalβ invalid memory reference π₯val sigterm : signalβ termination πval sigusr1 : signalβ application-defined signal 1 πval sigusr2 : signalβ application-defined signal 2 πval sigchld : signalβ child process terminated πΆval sigcont : signalβ continue βΆοΈval sigstop : signalβ stop (cannot be caught or ignored) πval sigtstp : signalβ interactive stop βΈοΈval sigttin : signalβ terminal read from background process πval sigttou : signalβ terminal write from background process βοΈval sigvtalrm : signalβ timeout in virtual time β³val sigprof : signalβ profiling interrupt πval sigbus : signalβ bus error π- (Since 4.03)
val sigpoll : signalβ pollable event π‘- (Since 4.03)
val sigsys : signalβ bad argument to routine π- (Since 4.03)
val sigtrap : signalβ trace/breakpoint trap πͺ€- (Since 4.03)
val sigurg : signalβ urgent condition on socket π¨- (Since 4.03)
val sigxcpu : signalβ timeout in cpu time π₯οΈ- (Since 4.03)
val sigxfsz : signalβ file size limit exceeded π¦- (Since 4.03)
val sigio : signalβ I/O is possible on a descriptor π¬- (Since 5.4)
val sigwinch : signalβ window size change πͺ- (Since 5.4)
val signal_to_string : signal -> stringβsignal_to_stringformats an OCaml signal as a C POSIX constant or"SIG(%d)"for platform-dependent signal numbers. π
π (finishing the one above, bestie) β Since 5.4. Raises Invalid_argument for unrecognised negative numbers.
val signal_of_int : int -> signalβsignal_of_int nturns a platform-dependent signal numberninto an OCaml signal number β¨- For a positive
n, that's justnitself β unless OCaml doesn't have a platform-independent signal number forn, in which case, well, it does its best. - Since 5.4
- Raises
Invalid_argumentifnis negative. No negative signal numbers for you π ββοΈ
- For a positive
val signal_to_int : signal -> intβ the other direction, babes:signal_to_int nconverts an OCaml signal numberninto a platform-dependent signal number.- For a positive
nthis is literallynitself. Iconic, effortless, no notes π - Since 5.4
- Raises
Invalid_argumentfor unrecognised negative numbers.
- For a positive
exception Breakβ the exception that gets raised on an interactive interrupt, but only ifSys.catch_breakis enabled. She's conditional, she's dramatic πval catch_break : bool -> unitβcatch_breakis the switch that decides whether an interactive interrupt (Ctrl+C) kills your program or raises theBreakexception instead π«Ά- Call
catch_break trueto enable raisingBreak. - Call
catch_break falseto let the system terminate the program on a user interrupt. - Inside multi-threaded programs, the
Breakexception will arise in any one of the active threads β and it'll keep arising on further interactive interrupts until all threads are terminated π Use signal masks fromThread.sigmaskto direct the interrupt towards a specific thread, iykyk.
- Call
val ocaml_version : stringβocaml_versionis the version of OCaml, as a string shaped exactly like"major.minor[.patchlevel][(+|~)additional-info]"πΈmajor,minor, andpatchlevelare integers.additional-infois an arbitrary string.- The
[.patchlevel]part was absent before version 3.08.0, and became mandatory from 3.08.0 onwards. Glow-up era β¨ - The
[(+|~)additional-info]part may be absent.
val development_version : boolβtrueif this is a development version,falseotherwise. A little vibe check on your compiler π¦- Since 4.14
type extra_prefix =π| Plus| Tilde- Since 4.14
type extra_info = extra_prefix * stringβ a prefix plus a string, that's the whole deal, no cap.- Since 4.14
type ocaml_release_info = {major : int ;minor : int ;patchlevel : int ;extra : extra_info option ;}- Since 4.14
val ocaml_release : ocaml_release_infoβocaml_releaseis the version of OCaml, but make it structured π (all the same info asocaml_version, just sliced up into fields).- Since 4.14
val enable_runtime_warnings : bool -> unitβ controls whether the OCaml runtime system can emit warnings onstderrπ₯- Currently, the only supported warning is the one triggered when a channel created by
open_*functions is finalized without being closed. (Yes, she noticed. She always notices. π) - Runtime warnings are disabled by default.
- Since 4.03
- Currently, the only supported warning is the one triggered when a channel created by
β οΈ Alert unsynchronized_access. The status of runtime warnings is a mutable global state. So likeβ¦ be normal about it in multithreaded code, bestie π
val runtime_warnings_enabled : unit -> boolβ returns whether runtime warnings are currently enabled. The vibe check for the setting above π- Since 4.03
β οΈ Alert unsynchronized_access. The status of runtime warnings is a mutable global state. Same warning, same energy.
β‘ Optimization β‘
val opaque_identity : 'a -> 'aβ for the purposes of optimization,opaque_identitybehaves like an unknown function, and thus a possibly side-effecting one. Sneaky on purpose π- At runtime,
opaque_identitydisappears altogether. Poof. Gone. β¨ - However, it does prevent the argument from being garbage collected until the location where the call would have occurred.
- A typical use of this function is to prevent pure computations from being optimized away in benchmarking loops. For example:
- At runtime,
for _round = 1 to 100_000 do
ignore (Sys.opaque_identity (my_pure_computation ()))
done
- Since 4.03
module Immediate64 : sig endβ that's the whole declaration. An empty signature. She's mysterious, she contains multitudes, she says nothing else in this page π
And that's the tea, bestie β that's the whole page, front to back, nothing skipped π«Άπ This little beauty is part of the standard library docs, rendered by OCamldoc, dated 2026-06-22, filed under Stdlib.Sys(3). Go forth and signal responsibly π
π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms