Ph.D. in Computer Science at Rutgers University

Processes and Threads

After equipped with how the kernel is booted in the previous article, we're gonna see two of the most important concepts in operating systems - Processes and Threads.


Process is rarely a unfamiliar concepts for those who use computer every day. When you open the task manager in Windows, you will see how many processes is running, how many RAM they're using, and so are the CPU usage. However, only a few people could pin point the exact meaning of process and its difference to another concept - program.

Process and Program

An executable program is a static concept, to be specific, a set of meaningful source code after being compiled, assembled, linked and eventually loaded into executable file is just a piece of binary record lying in the hard disk drive. While a process of a program is a rather dynamic concept. A process is a running instance (entity) of its corresponding program in the main memory. For example, when we double click the Notepad in Windows or just type the 'ssh' command in Unix, the operating system load the program from hard disk drive into main memory; then do some initialization for the memory address space and flags (in Linux, the fork() system call is used to create a process, copying the address space from the father process), then keeps the fetch-decode-execute loop (in Linux, the execve() system call is invoked to set up the new address space, registers, stack pointers, flags and then grab and execute instructions), which was a runtime status of that program; And we call that a process. A program could have multiple processes when running.

State of Process

There're roughly three states of a process: running, ready and blocked. There's a finite state machine model of the three states:


1 When a process is created, typically, it goes to ready status. And it's gonna be inserted into ready task queue unless it's with a very high priority.

2 When a process with ready status in the ready task queue is brought into the front and about to execute, it goes to running state.

3 When a process is in running status, and its time share is exausted or it's chosen to be the victim to give up CPU usage, it goes to ready status and is inserted into task queue waiting for the next time share.

4 When a process is in running status, and it's waiting for input or it's waiting for data transfer between memory and hard disk. We could say it was blocked by the I/O. Thus this process will go to blocked status.

5 When a process is in blocked status, which means it might be waiting for input, and the input is ready, it'll goto ready status and being inserted to the end of ready task queue.

Process Control Block

As I just said above, a process has it's own memory address space and has its code and data need to be executed. A data structure lays in memory keeps the status, identification info and control info of a process call a Process Control Block.

Remembering the first article in this category, modern OSes is like a virtual machine that different users could use the same system at the same time as if they were the only user of this machine thanks to multi-processes and virtual memory. Because each process has a quota of CPU usage so every one get a share. The switching time is so small that nobody could sense it. For virtual memory, I'm gonna talk about it in the next several articles.

A process is just a running entity has its own memory address space, code and data. Thus, the corresponding process control block contains the following information: (referenced from Wikipedia)


A thread, like a process, is also an executing context, or an sequence of programmed instructions. But the granularity of thread is more fined than process. The implementation of threads and processes differs between operating systems, but in most cases a thread is a component of a process. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources. In particular, the threads of a process share its executable code and the values of its variables at any given time.


As the picture illustrated above, the left one is one process only have one thread, thus the code in this process could only be executed in serial, while the right one shows the same process could be divided into three threads that has their own registers and stack, and could be executed in parallel. Intel x86 processor has a technology called hyper-threading, which allows one physical core be divided into two or more seperate cores that has seperate register files, can be individually halted, interrupted, and could let different threads running at the same time.

Kernel-level Thread

Kernel-level Thread means this kind of threads are totally scheduled and managed by kernel, which means when do context switch between kernel threads, trapping into kernel is needed. The advantage of kernel-level threads is that in a multi-core machine or with hyper-threading, kernel-level threads could run in parallel, taking full advantage of CPU resource. The disadvantage of kernel-level threads is that context switch overhead between kernel-level threads are quite considerable compared with that between user-level threads, because each context switch need to go in kernel mode for scheduling.

User-level Thread

User-level Threads means this kind of threads are scheduled and managed in user space, which means the context switch only happens in user space. One thing need to be pointed out is that user-level threads also require system call to operate. The advantage of user-level threads is that context switch overhead is less than that of kernel-level thread. The disadvantage of user-level threads is pure level-threads (N-to-1 Thread Model) could not run in parallel, which means CPU resouce are not fully used.

Threads Models

Difference between Process and Thread

Inter-Process Communication

As it said above, different processes have different memory address space. Thus, to communicate between different process, we need some new approaches: (reference from wikipedia)