Tag: #system-calls
How does swapping stdin and stderr work?
The magic shell string
3>&2 2>&1 1>&3-
swaps stdout and stderr. It does with the dup2
system call to swap file descriptors. 2018-02-22What are the
domain
and type
arguments to the socket
system call? The
domain
and type
arguments to socket()
describe the protocol family and socket type, respectively. The protocol
argument specifies the actual protocol to use, which may be inferred from the domain
and type
. 2017-02-27How to write a TCP server using the
fork
syscall A TCP server that uses the
fork
system call to create a new child process for each accepted connection, allowing it to handle multiple clients concurrently. 2017-02-25What is
ssize_t
in C? ssize_t
is a signed version of size_t
, used to represent return values from system calls that may need to indicate error with -1
. 2017-02-22What is a a FIFO, or “named pipe”? What is
mkfifo
in C? A FIFO is a special file that allows inter-process communication. The
mkfifo
system call creates a FIFO, enabling processes to read from and write to it. 2017-02-21How to write an assembly ‘hello world’ on macOS
A “Hello, world!” program in x86-64 assembly for macOS, using the NASM assembler and system calls for writing to stdout and exiting. 2017-02-20
What is
lsof
? lsof
lists open system resources, including pipes, sockets, and yes, files. It shows their type, owner, and location. 2017-02-20How do I close a file descriptor in C?
To close a file descriptor in C, use the
close
system call. Multiple descriptors can reference the same underlying file or pipe. The pipe is only closed when all references are closed. 2017-02-16How do I duplicate a file descriptor in C?
Use the
dup
system call to duplicate a file descriptor in C, allowing two references to the same underlying pipe. 2017-02-15How do I call a program from C?
To call a program from C, use `fork` then `execve`. There is no more direct way! 2017-02-07
How do I use
fork
in C? fork
duplicates the current process. It returns 0
in the child process. In the parent process, it returns the child’s new process id. 2017-02-06How do I use
execve
in C? execve
replaces the current process with a new one. It takes a path, an argument array, and an environment array. The process never returns unless execve
fails. 2017-02-05What system calls does macOS have?
All system calls on macOS 10.12, categorized by the functionality they provide, such as process management, file operations, memory management, etc. 2017-01-31
How can I write a file with
mmap
in C? To write to a file, open the file,
mmap
the file descriptor, then write to memory. 2017-01-28What is
mmap
in C? mmap
is a system call in C that allows programs to manipulate underlying hardware resources, such as physical memory or files, using ordinary memory manipulation. 2017-01-26How do I measure program execution time in C? How do I use the
times
function? Use the
times()
system call in C to measure the CPU time used by a process, distinguishing between time charged to the process itself and time charged to the kernel on its behalf. 2016-12-26What is
perror
in C? perror
in C prints a human-readable description of the last error that occurred, as stored in the global errno
variable. 2016-12-24What syscalls does a UDP server need?
The syscalls needed for a simple UDP echo server are
socket
, bind
, recvfrom
, sendto
, and close
. 2016-12-19How to write a TCP server with the
kqueue
API Kqueue is a more efficient alternative to
select
for managing multiple TCP connections, providing a publish-subscribe model for tracking events in the kernel. 2016-12-18How to write a TCP server with the
select
syscall The
select
syscall allows a process to sleep and wake up when a file descriptor is ready for reading, writing, or has an exceptional condition. This enables a TCP server to handle multiple clients concurrently. 2016-12-16What syscalls does a TCP server need?
A minimal TCP server in C uses the
socket
, bind
, listen
, accept
, recv
, send
, and close
syscalls to manage connections. 2016-12-14All content copyright James Fisher.