Week 2: Linux syscalls, tasks and ProcFS
The CPU does instruction, one at a time, with the fetch decode execute cycle.
You can simulate a sum with the following instructions:
But wait, how can we interact with external devices? We don’t have the instructions on our code!
The devices connected to your computer, use a mechanism called “interrupts” for being aware of the events.
Warning
This is a super-summarized path (and incorrect one), there’s no PIC controller, the IRQ numbers probably are wrong. But it’s only for academic purposes.
Well, yes, one of the first steps of the kernel is to tell the cpu to point the IDT table to his part of the code that has the logic for the IDT table.
So, now, when we receive an interrupt is the kernel code the one that will be executed! Nice!
Tip
You can see the IDT table inside the linux kernel source code, and where it is setup.
Warning
Due to restricted access to memory and I/O ports in Ring 4, userspace can do almost nothing to the outside world without calling on the kernel. It can’t open files, send network packets, print to the screen, or allocate memory.
But, I can do a lot of stuff with my userspace programs… How is that possible?
Tip
You can see all the linux kernel syscalls on this list
Does the following:
Tip
You can see the system calls the program is doing under the hood, with the strace tool
"notify"
the kernel.For example, if we want to execute the open
system call, we could, in userspace:
Tip
Since the Linux Kernel 2.6, it supports the syscall instruction instead of doing an interrupt 128 (0x80) for syscalls. You can read more here
The CPU executes instructions linearly, one after another.
To interact with the outside world, it relies on interrupts.
The CPU has different privilege levels, called rings, which help the operating system enforce security and control. Our programs usually run in the least privileged ring (user mode), which means we can’t execute certain instructions directly.
If we want to perform I/O operations or access system resources, we need to ask the kernel to do it for us.
We communicate with the kernel through system calls (syscalls), which are essentially a type of interrupt.
The kernel also sets up interrupts on its own to manage processes (like pausing one, switching to another, or handling various system events.) You can read more here.
Are you already familiar with those?
The kernel has an abstraction of a running program, and calls it “task (task_struct)”.
Depending of the type of task, we can differentiate between:
cat /etc/passwd
.$cat file.txt file2.txt file3.txt
could create one thread per file.Threads and processes, can be created with the clone
syscall.
cat
program.You’ll study them on SCP
, where you’ll create them with the glibc-pthread library (which uses the clone
syscall underneath).
A thread is the basic unit that the kernel process scheduler uses to allow applications to run the CPU. Has the following characteristics:
They’re identified via tgid
.
Example: If I have multiple files on the cat
program maybe I need only different execution paths, and I can create one thread for each one.
Sometimes the kernel core or device drivers need to perform blocking operations and thus they need to run in process context.
Kernel threads are used exactly for this and are a special class of tasks that don’t “userspace” resources.
A tgid
will also be used to identify them.
Example: ksoftirqd/X
. Where X is the number of cores. Is the scheduler kthread for the core X
on your computer.
ls -la /proc
shows a the list of pids/tgids of processes and kthreads (NOT Threads!).$cat ...
), running $ls /proc/${pidOfCat}/task
will list a single thread. This thread’s tgid
(thread group ID) will be the same as pidOfCat
, since the process consists of only one thread of execution (one task_struct inside the kernel).cat /proc/${pid}/stat
or cat /proc/${pid}/status
ls -la /proc
hhows a the list of pids/tgids of processes and kthreads (NOT Threads!).cat /proc/${tgid}/stat
1 or cat /proc/${tgid}/status
cat /proc/${tgid}/exe
should give no such file or directory
.But each process can have multiple threads spawned, this are listed on: /proc/{pid_of_process}/tasks/{tgid}
Warning
Be aware that, if we have a process that spawns two threads, it will have 3 entries inside the tasks folder:
tgid == pid
)Once you have a tgid
, you can do ls -la /proc/${tgid}
or cd
into it.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <sys/types.h>
void* read_file(void* filename) {
char* file = (char*)filename;
pid_t pid = getpid(); // Process ID
pid_t tid = gettid(); // Thread ID (TID/TGID)
pid_t ppid = getppid(); // Parent PID
printf("Reading %s | PID=%d, TGID=%d, PPID=%d\n", file, pid, tid, ppid);
sleep(1); // Fake read_file implementation...
pthread_exit(NULL);
}
int main() {
pid_t pid = getpid();
pid_t tid = gettid(); // Main thread ID
printf("cat process started | PID=%d, TGID=%d, PPID=%d\n", pid, tid, getppid());
pthread_t t1, t2, t3;
pthread_create(&t1, NULL, read_file, "one.txt");
pthread_create(&t2, NULL, read_file, "two.txt");
pthread_create(&t3, NULL, read_file, "three.txt");
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
printf("cat process exiting | PID=%d\n", pid);
return 0;
}
And in Htop, you should see the following:
Why we need syscalls?
Can we create kthreads with the clone
syscall?
Can I have a thread without a process?
Where’s The /proc filesystem saved?
What are tgids and pids, and the confusion between these: unviversity of toronto blog and hongyu blog.
Explaining how htop is implemented: Peteris blog
Why htop shows kthreads, threads and not only processes: Htop Issue
How a Single Bit Inside Your Processor Shields Your Operating System’s Integrity: Youtube Video
Linux Kernel Internals: Process by Mapple Circuit: Youtube Video
Journey into the proc filesystem: blog post by Fernando Villalba
If you really want to understand a little bit more what happens under the hood, you can do the following exercices. Be aware that you should read the “Really Recommended References” first, and then try to do this exercices.
Ready to have some fun? Check out the second AMSA activity here!
AMSA 2025-2026 - 🏠 Home