# How to implement a system call?

**URL:** <https://discuss.ocaml.org/t/how-to-implement-a-system-call/10273>\
**Category:** Learning\
**Tags:** unix\
**Created:** [August 7, 2022, 10:27am UTC](https://discuss.ocaml.org/t/how-to-implement-a-system-call/10273 "2022-08-07T10:27:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vdolgov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vdolgov/32/3430_2.png) [@vdolgov](https://discuss.ocaml.org/u/vdolgov)\
**Post date:** [August 7, 2022, 10:27am UTC](https://discuss.ocaml.org/t/how-to-implement-a-system-call/10273/1 "2022-08-07T10:27:20Z")

</div>

Hi everybody! 👋

I’m trying to figure out how to make a system call which is not present in `Unix` module. Suppose I need `shm_open` to access the shared memory, what is the general flow of implementing such thing? It would be great to make it compatible with other function that work with Unix file descriptors and not something special like `shm_descr`.

I’m noob in system programming, but it happened I need to start somewhere…

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [August 7, 2022, 11:35am UTC](https://discuss.ocaml.org/t/how-to-implement-a-system-call/10273/2 "2022-08-07T11:35:42Z")

</div>

You can call an external function by just using its name in an `external` declaration:

```ocaml
external my_syscall : int -> string -> int = "caml_my_syscall"

```

where `caml_my_syscall` is a symbol that will be resolved at link time.

Conceptually, that is what you will need to do to use your syscall. However, you usually cannot call a C function directly, but you call a wrapper function that converts between OCaml values and C values. When programming these wrappers, you need to follow [some guidelines](https://v2.ocaml.org/manual/intfc.html#s%3Ac-gc-harmony) so that the C code interacts well with the garbage collector.

The page I just linked to contains a number of examples of such wrapper functions. You can also take a look at [the implementation of `Unix`](https://github.com/ocaml/ocaml/tree/trunk/otherlibs/unix): the `external` declarations are in `unix_unix.ml` (for Unix-like systems) or `unix_win32.ml` (for Windows), and the C functions referred to are in small-ish C files, e.g. [the implementation of `chown`](https://github.com/ocaml/ocaml/blob/trunk/otherlibs/unix/chown.c).

> [@vdolgov](#):
>
> It would be great to make it compatible with other function that work with Unix file descriptors and not something special like `shm_descr`.

On Linux, values of type `file_descr` are just OCaml integers, so you just need to convert to C integers (there’s a macro for that, see the docs) to use them in syscalls.

---

<div class="post-metadata">

**Author:** ![otini](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/otini/32/3533_2.png) [@otini](https://discuss.ocaml.org/u/otini)\
**Post date:** [August 7, 2022, 11:36am UTC](https://discuss.ocaml.org/t/how-to-implement-a-system-call/10273/3 "2022-08-07T11:36:49Z")

</div>

P.S. if you use dune, compiling and linking your C stubs is straightforward: [General Concepts — dune documentation](https://dune.readthedocs.io/en/stable/concepts.html#foreign-sources-and-archives)

---

<div class="post-metadata">

**Author:** ![art-w](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/art-w/32/4728_2.png) [@art-w](https://discuss.ocaml.org/u/art-w)\
**Post date:** [August 7, 2022, 12:11pm UTC](https://discuss.ocaml.org/t/how-to-implement-a-system-call/10273/4 "2022-08-07T12:11:28Z")

</div>

Hello! You might find some inspiration from looking at the implementation of this function from ocamlnet: [netsys\_posix.ml](https://gitlab.com/gerdstolpmann/lib-ocamlnet3/-/blob/master/code/src/netsys/netsys_posix.mli#L1007) + [netsys\_c\_shm.c](https://gitlab.com/gerdstolpmann/lib-ocamlnet3/-/blob/master/code/src/netsys/netsys_c_shm.c#L32). If you just wanted to use it, it’s at [`Netsys_posix.shm_open`](http://projects.camlcity.org/projects/dl/ocamlnet-4.1.9/doc/html-main/Netsys_posix.html#VALshm_open) 🙂
