Previous: Short Notes, Next: XorgUp: Linux

X In Y Minutes

x in y minutes saga for random linux stuff.

# Use du to check disk usage, ls -l is not recursive
$ du -h -d 1 org
28K     org/linux
704K    org/maths
1.5M    org/.git
144K    org/programming
12K     org/.github
2.5M    org

# linux distinguishes virtual memory into user space and kernel space.  Device drivers,
# kernel extensions, and the kernel itself run in kernel space whereas user
# applications run in user space

# syscalls are function calls from user space to kernel space, to see a list of
# available syscalls for your system run 'man syscalls'.  Using strace we can see
# what syscalls an application makes:
$ strace
strace echo hi | cat
execve("/usr/bin/echo", ["echo", "hi"], 0x7ffc25fd9838 /* 70 vars */) = 0
brk(NULL)                               = 0x563b21676000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=212631, ...}) = 0
mmap(NULL, 212631, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f7719896000
close(3)                                = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@>\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1905632, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7719894000
mmap(NULL, 1918592, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f77196bf000
mmap(0x7f77196e1000, 1417216, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x22000) = 0x7f77196e1000
mmap(0x7f771983b000, 323584, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17c000) = 0x7f771983b000
mmap(0x7f771988a000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ca000) = 0x7f771988a000
mmap(0x7f7719890000, 13952, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f7719890000
close(3)                                = 0
arch_prctl(ARCH_SET_FS, 0x7f7719895580) = 0
mprotect(0x7f771988a000, 16384, PROT_READ) = 0
mprotect(0x563b20f85000, 4096, PROT_READ) = 0
mprotect(0x7f77198f4000, 4096, PROT_READ) = 0
munmap(0x7f7719896000, 212631)          = 0
brk(NULL)                               = 0x563b21676000
brk(0x563b21697000)                     = 0x563b21697000
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=3041504, ...}) = 0
mmap(NULL, 3041504, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f77193d8000
close(3)                                = 0
fstat(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
write(1, "hi\n", 3)                     = 3
hi
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++

# File descriptors index a per-process file descriptor table which in turn
# indexes into a system-wide table of files opened by all process called the file table
$ nop &
$ ls /proc/$!/fd  # You may need to escape the ! with zsh
0  1  2
$ file /proc/$!/fd/2
/proc/PID/fd/2: symbolic link to /dev/pts/5

# Note /prod/PID/fd/0, /prod/PID/fd/1, /prod/PID/fd/2 always point to stdin, stdout and
# stderr, for the previous command, all 3 however will point to the psuedo-terminal
# /dev/pts/5.  If instead we use the command:
$ nop > out.file &
$ file /proc/$!/fd/0
/proc/PID/fd/1: symbolic link to /home/laurencewarne/out.file

# We see stdout indeed points to a file.

#############
# Processes #
#############
# On linux processes are grouped into groups, which are further grouped into sessions.
# Lets have a look at what commands are running in our shell session:
$ ps -o command,pid,ppid,pgid,sid --ppid $$
COMMAND                         PID    PPID    PGID     SID
ps -o command,pid,ppid,pgid   19765    4739   19765    4739

# PGID and SID point to the process group leader and session leader respectively.
# Here we see our ps process is a process group leader (the sole member of its group),
# but not a session leader, in fact the session group leader is the shell process
# (zsh in my case).

$ ps -o command,pid,ppid,pgid,sid --ppid $$ | cat
COMMAND                         PID    PPID    PGID     SID
ps -o command,pid,ppid,pgid   20135    4739   20135    4739
cat                           20136    4739   20135    4739

# Here we see a slight difference, cat has it's process group id set to the pid of
# the ps command.  Process groups are useful as it's possible to direct signals
# to whole process groups instead of singular processes using the killpg() system
# call or prepending '-' to the kill command.

# Sessions are usually tied to a controlling terminal, e.g. bash.  If the exiting
# process is a session leader and its controlling terminal is the controlling
# terminal of the session, then each process in the foreground process group of
# this controlling terminal is sent a SIGHUP signal, and the terminal is
# disassociated from this session, allowing it to be acquired by a new controlling
# process.More info can be found here: https://man7.org/linux/man-pages/man3/exit.3.html
# another link is here: https://man7.org/linux/man-pages/man3/exit.3.html.

Author: root

Created: 2024-12-14 Sat 19:47

Validate