DEV Community

Sunbeom Kweon (Ben)
Sunbeom Kweon (Ben)

Posted on • Updated on

[Operating System] 1 - Processes & Threads

This post refers to Modern Operating Systems by Andrew S. Tanenbaum

1. Process

What is a process?

According to the author of the book, "process is an abstraction of a running program."

"In any multiprogramming system, the CPU switches from process to process quickly, running each for tens or hundreds of miliseconds." (Andrew S. Tanenbaum)

But if we open System Manager on Windows or Activity Monitor on Mac or htop on Linux, we can see there are so many concurrently running processes. How is this possible when we only have one CPU?

screenshot

Activity Monitor screen - Lots of processes are running at the same time

First, the computation is so fast that human eyes cannot detect the lag between the switch to switch. "Strictly speaking, at any one instant the CPU is running only one process. But in the course of 1 second it may work on several of them, giving the illusion of parallelism." (Andrew S. Tanenbaum)

Second, for the most of the time, the fact that computer has only one CPU is not true. CPU contains many cores and each core are capable of handling a process at a time.

But even though with the additional cores, typically one PC contains 4 to 8 cores per CPU. And also, typically computer has one CPU. While for the most of the PC's are achieving parallel computing thank to multiple cores, because of too many processes that PC's need to handle(more than thousands) CPUs still need to switch from one process to another. Therefore, though it may look like computer is processing all the tasks at the same time, it is not true.

Process vs Program

"The difference between a process and a program is subtle, but absolutely crucial."(Andrew S. Tanenbaum)

"A process is an activity. It has a program, input, output, and a state. In contrast, a program is something that may be stored on disk, not doing anything."(Andrew S. Tanenbaum)

My understanding: A process is created when the bytecodes of a program is copied onto a computer's RAM, and starts to process all the instructions. Before the process, the program is just bytecodes stored in the disk.

Daemons

"When an operating system is booted, typically numerous processes are created. Some of these are to interact directly with users(humans), but others run in the background, not associated with the users." (Andrew S. Tanenbaum) For example, a process designed to listened to specific port's incoming request.

These background running processes are called daemons. In Linux, you can use ps command to list all of them.

Process Creation

In UNIX, there is only one system call to create a new process: fork.

After a process is created, the parent and child have their own distinct address spaces. They can access to each other's memory, but cannot modify it.

Process States

Process is an independent entity, with its won program counter and internal state.

But in many occasions, processes often need to interact with other processes. For example, a process may take a long time to return the output and the other process is waiting for the output to process it (waiting for an input yet unavailable).

Process States

Process States Diagram

There are four transitions (except creation and deletion) and three states.

* Transitions explained

  1. Blocking: Process blocks for input

  2. Timeout: Scheduler picks another process

  3. Dispatch: Scheduler picks this process (from the queue)

  4. Unblocking: Input becomes available

2. Threads

What is a thread?

Threads are mini-processes inside of a process. A single process can have multiple threads, and at least one. But why do we need threads when we already have processes?

There are several reasons for having these mini-processes, called threads.

  1. In many applications, multiple activities are going on at once. To achieve the concurrency, we need to decompose such an application into multiple sequential threads so that the programming model becomes simpler.

  2. Threads are lighter weight than processes, they are easier and faster to create and destroy than processes.

  3. Threads yield no performance gain when all of them are CPU bound, but when there is substantial computing and also substantial I/O, having threads allows these activities to overlap, thus speeding up the application (have no idea what he says)

  4. Threads are useful on systems with multiple CPUs, where real parallelism is possible.

Top comments (0)