System call vs Function call

A system call is a call into kernel code, typically performed by executing an interrupt. The interrupt causes the kernel to take over and perform the requested action, then hands control back to the application. This mode switching is the reason that system calls are slower to execute than an equivalent application-level function. fopen is … Read more

What is the difference between FUTEX_WAIT and FUTEX_WAIT_PRIVATE?

This is an optimization done by linux/glibc to make futexes faster when they’re not shared between processes. Glibc will use the _PRIVATE versions of each of the futex calls unless the PTHREAD_PROCESS_SHARED attribute is set on your mutex It’s explained in more detail here: http://lwn.net/Articles/229668/ For the purposes of your debugging, you can just ignore … Read more

What is the purpose of MAP_ANONYMOUS flag in mmap system call?

Anonymous mappings can be pictured as a zeroized virtual file. Anonymous mappings are simply large, zero-filled blocks of memory ready for use. These mappings reside outside of the heap, thus do not contribute to data segment fragmentation. MAP_ANONYMOUS + MAP_PRIVATE: every call creates a distinct mapping children inherit parent’s mappings childrens’ writes on the inherited … Read more

How do system calls work?

Your understanding is pretty close; the trick is that most compilers will never write system calls, because the functions that programs call (e.g. getpid(2), chdir(2), etc.) are actually provided by the standard C library. The standard C library contains the code for the system call, whether it is called via INT 0x80 or SYSENTER. It’d … Read more

Golang catch signals

There are three ways of executing a program in Go: syscall package with syscall.Exec, syscall.ForkExec, syscall.StartProcess os package with os.StartProcess os/exec package with exec.Command syscall.StartProcess is low level. It returns a uintptr as a handle. os.StartProcess gives you a nice os.Process struct that you can call Signal on. os/exec gives you io.ReaderWriter to use on … Read more

How do I get a thread ID from an arbitrary pthread_t?

Since pthreads do not need to be implemented with Linux threads (or kernel threads at all, for that matter), and some implementations are entirely user-level or mixed, the pthreads interface does not provide functions to access these implementation details, as those would not be portable (even across pthreads implementations on Linux). Thread libraries that use … Read more

What is the difference between the functions of the exec family of system calls like exec and execve?

There is no exec system call — this is usually used to refer to all the execXX calls as a group. They all do essentially the same thing: loading a new program into the current process, and provide it with arguments and environment variables. The differences are in how the program is found, how the … Read more

Is it true that fork() calls clone() internally?

For questions like this, always read the source code. From glibc’s nptl/sysdeps/unix/sysv/linux/fork.c (GitHub) (nptl = native Posix threads for Linux) we can find the implementation of fork(), which is definitely not a syscall, we can see that the magic happens inside the ARCH_FORK macro, which is defined as an inline call to clone() in nptl/sysdeps/unix/sysv/linux/x86_64/fork.c … Read more