If you are already familiar with such concepts as processes and threads, you also probably know that without them you couldn't launch any application on your computer. Let's learn what it takes for a computer to launch any software program.
Inside a computer
Computers are complex hardware systems. The following image roughly depicts the main blocks of the computer system with two processors (CPU). The processors communicate with the random access memory (RAM) and input/output devices using the system bus. Broadly speaking, the system bus is a wire bundle connecting all parts of a computer into one whole unit.
Most modern computers have multiple processors and much more complex architecture. They can run many applications at the same time and process big data. To harness all their power, each modern computer runs special software called the operating system.
Operating System
An operating system is a set of system software that serves as a bridge between computer hardware and application software.
Without the operating systems, the computer would be a bunch of dead hardware. Modern operating systems are pretty complex; we won't consider them in detail now. Our interest at the moment is how the operating system manages the execution of the application software.
An operating system provides several services to the user and application software. As an example, here's the list of some of them and what they can do:
The file system provides access to the files on hard disk drives.
The I/O system provides access to the input/output operations.
The program execution service creates the application's process and schedules its execution.
The communication service arranges interprocess communications.
The path of starting an application by the program execution service is a chain of the following actions:
It allocates application memory inside the RAM space.
Loads the program code (an executable file) into the application memory space.
Creates a new process and all necessities for execution.
Loads the main thread of the process and puts the newly created thread into the queue for execution by CPU.
Creating and destroying processes and their threads as well as running and stopping threads is the responsibility of the operating system.
Multiple processes can run under the operating system control concurrently and in parallel. Processes can communicate with each other and with the operating system using a communication service provided by the operating system.
Process
The operating system creates a process while running an application. The process is an instance of an application allocated in a computer's memory and scheduled for launching or already running. It has its properties and structure. Let's take a quick look at them.
The process has its own memory space. The operating system creates threads, the data structures that contain information about the running process's tasks. The rest of the process memory is a so-called heap.
The heap is a part of the memory shared between threads. That's where a process keeps shared resources such as code of the program, descriptors of opened files. It also stores the memory available for threads to dynamically allocate instances of the objects and the data produced during code execution.
Thread
Thread is the data structure representing one of the running tasks of the application. Threads exist only inside a process, and every process has at least one. Threads share the heap and can communicate through it with each other. They can get concurrent access to any objects in the process heap.
The operating system creates a new process with only one thread. This thread is usually called the main thread. It can initiate the creation of additional threads inside the application, and every newly created one can do it as well.
While concurrency among processes is the responsibility of the operating system, arranging the concurrent execution of threads inside the process is the concern of the application threads.
So, what distinguishes threads management from process management is that the application can independently make decisions about their creation or destruction, as well as manage their execution.
Conclusion
Modern computers are complex hardware systems managed through the services an operating system provides.
The operating system is a set of software that serves as a bridge between hardware and application software.
The process is a data structure the operating system creates in a computer's memory, and it has its memory space mapped to the computer's physical memory. The process contains threads and heap memory shared among them. The heap contains program code, file descriptors, and shared memory, free for dynamic allocation during program execution.
Threads are data structures inside process memory space representing the application tasks.