No tags found on this site yet.

Operating Systems 101: Linux Internals and Process Management

05 Jul 2021 - [ os  linux  fundamentals  internals  processes  ]

Operating Systems 101: Linux Internals and Process Management


User Space vs. Kernel Space

Linux operates in two main execution contexts:

To interact with hardware (like opening files or creating sockets), user-space programs must go through the kernel.


System Calls: The Interface Between Worlds

System calls (syscalls) are the gateway between user space and kernel space.

Example:

Analogy: User space is the restaurant guest; kernel space is the kitchen. Syscalls are how you place an order—you can’t go cook it yourself.


Privilege Rings (CPU Security Levels)

Linux uses hardware-supported privilege levels (rings):

Ring 0: Kernel code (Most privileged)
Ring 1–2: Device drivers (rare) (Intermediate)
Ring 3: User applications (Least privileged)

Only Ring 0 can execute privileged CPU instructions. All syscalls switch from Ring 3 to Ring 0 temporarily.


Process Management: The Lifecycle

What is a Process?

A process is an instance of a program running in memory. It has:

Process Creation: fork() and execve()

When you run ls, here’s what happens:

  1. The shell calls fork() to create a child process.
  2. The child calls execve("/bin/ls", ["ls"], envp) to replace its memory with the ls binary.
  3. The parent waits with wait() until the child exits.

Copy-on-Write (CoW): fork() doesn’t duplicate memory immediately—only when parent/child write to it.

Process Termination

Signals and kill()

Sending a signal (e.g., via kill command) results in the kernel delivering that signal to the target process, if permitted.


Process States

States you might see in ps or top:

R - Running or runnable: TASK_RUNNING
S - Sleeping (interruptible): TASK_INTERRUPTIBLE
D - Uninterruptible sleep (I/O wait): TASK_UNINTERRUPTIBLE
Z - Zombie (terminated, not reaped): EXIT_ZOMBIE
T - Stopped or traced (e.g., SIGSTOP): TASK_STOPPED / TASK_TRACED


Process Memory Layout

Linux process memory is organized as:

Stack grows downward, heap grows upward. Collision = stack overflow.


Threads vs Processes

| Feature     | Threads                       | Processes                  |
|-------------|-------------------------------|----------------------------|
| Memory      | Shared                        | Isolated                   |
| Overhead    | Low (lightweight)             | Higher                     |
| Security    | Lower (shared memory)         | Higher                     |
| Switching   | Fast                          | Slower (context switching) |

Threads are great for performance, processes are better for isolation.



Memory Management

Types of Memory

Check with:

cat /proc/meminfo

Virtual Memory and Paging

Linux provides each process its own virtual memory space, mapped to physical memory via page tables.


Swapping

When memory is low:


File and Device Access

Devices as Files

Linux treats devices as files under /dev.

$ ls -l /dev
crw-rw---- 1 root tty 4, 1 /dev/tty1 # Character device
brw-rw---- 1 root disk 8, 0 /dev/sda # Block device

Permissions and SUID/SGID

When a syscall like open() is made, the kernel checks:

SUID/SGID Execution:


Inter-Process Communication (IPC)

Methods:

  1. Files: Simple but inefficient
  2. Pipes: Stream data between processes (ps aux | grep ssh)
  3. Shared Memory: Fastest, requires synchronization
  4. Message Queues: Structured message passing
  5. Sockets: Network communication or local IPC

Synchronization: Mutexes and Semaphores


I/O Types and Monitoring

Monitor with:

iostat -x

Tools: strace for Syscall Tracing

strace shows you what syscalls a process makes:

strace ls
strace -e open,read,write cat /etc/hosts
strace -p <PID>

Useful for debugging file access, permission issues, and syscall failures (EACCES, ENOENT, etc.).


Hands-On Challenges

  1. Use strace -o trace.txt ls -l /etc and identify:
    • execve, openat, getdents64, write
  2. Try to create a permission-denied error:
    strace touch /root/test
    

    Look for EACCES.

  3. Trace a pipeline:
    strace -f bash -c "ps aux | grep systemd"
    

    Identify use of pipe(), fork(), execve(), dup2().

Further Reading