preface
The most central concept in an operating system is the process. A process is an abstraction of a running program. Without the abstraction of processes, modern computing would not exist. Everything else in the operating system revolves around the concept of processes. Therefore, a thorough understanding of the process is essential. Processes are one of the oldest and most important abstractions provided by operating systems. Even though only one CPU can be used, they also have the ability to support (pseudo) concurrent operations by converting a single CPU into multiple virtual cpus.
process
Modern computers often do more than one task at a time. For example, when the system is started, many processes are secretly started, including the process to receive mail, virus detection process, file printing process, and so on. The activities performed by these processes need to be managed, so a multi-programming system that supports multi-processes comes into being. Similar to multiprogramming systems are multiprocessor systems, but they have essential differences, which are described below.
Multichannel programming system: multichannel programming system is the simultaneous storage of several independent programs in the computer memory, so that they are under the control of the hypervisor, interspersing with each other. The state of two or more programs in a computer system between the beginning and the end. This is called the characteristic of multichannel technology operation: multichannel, macro concurrent, micro serial.
In multi-channel programming system, the execution of multiple programs is serial in nature, that is, time-sharing, because the processor quickly switches between the multi-channel programs and achieves the macro concurrent effect.
Multiprocessor system: A Multiprocessor system consists of two or more processors that exchange data with each other. All processors share memory, I/O devices, controllers, and peripheral devices. The entire hardware system is controlled by a unified operating system. Full parallelism of job, task, program, array, and element levels between processor and program.
Multiprocessor systems and multiprogramming systems are different
The difference between multiprocessor system and multiprogramming system is that one is hardware true parallelism and the other is software pseudo-parallelism. That is, because of the existence of multiple CPU cores, the former realizes the simultaneous execution of multiple independent programs on different CPU cores at the same time, realizing the real concurrent execution. The latter is due to the system scheduling, at the same time only one program can enjoy the CPU, so it is a pseudo-parallel.
We learned about pseudo-parallelism in multiprogramming systems, and we learned about true parallelism in multiprocessors. Because it is difficult to keep track of multiple parallel activities, operating system designers have, over the years, developed a conceptual model for describing parallelism called sequential processes. The model is an abstraction of the programs running in the system, with sequential processes making parallelism easier to handle. This model is the focus of this paper. In this article, we assume that the computer has only one CPU, that is, that the CPU is single-core (although this assumption is almost non-existent in modern computers).
Process model
In the process model, all runnable software on a computer, often including the operating system, is organized into sequential processes, or processes.
A process is an abstraction of the software running on a system. A process is an instance of an executing program, including the current values of program counters, registers, and variables. In my opinion, it is a dynamic and non-static concept, that is, a running program, rather than an inactive and stopped program.
Conceptually, each process has its own virtual CPU, although in practice the physical CPU switches back and forth between processes. Imagine a multiprogram computer with four programs in memory. These four programs are abstracted into four processes each with its own control flow (that is, with its own logical program counters). And each program runs independently. Since there is really only one physical program counter, when each program runs, its logical program counter is loaded into the physical program counter. When the CPU pauses (or finishes) execution of the program, the physical program counter is saved to the process’s logical program counter in memory. The CPU is so tired of switching back and forth between a number of programs, in order to achieve fairness and fairness, even rain.
We know that the CPU is switching back and forth very quickly between processes, so each process can perform its computation at an indeterminate speed. And when the same process runs again, the speed is usually not reproduced. Therefore, never make any assumptions about timing when programming a process.
A process is an activity of some type with programs, inputs, outputs, and states. A single processor, which can be shared by several processes, uses some scheduling algorithm to decide when to stop working on one process and switch to serving another.
The distinction between process and program is subtle but important. For example, the baker is the CPU, the recipe for baking a cake is the program, and the sum of the sequence of actions that the baker performs following the recipe is the process. When a baker is interrupted in the middle of baking a cake and turns to a recipe to cook it is a process switch. Bakers record the steps of the cake before turning it into a stir-fry and wait for it to finish cooking before continuing to bake the cake. Here, each process (baking a cake or stir-frying a dish) has its own program (baking recipe or cooking recipe), and the ingredients for cake and stir-frying are input data. The recorded cake baking step is the status. In our program, the essence of context switching is state saving and recovery.
If a program runs twice, it counts as two processes. For example, start the same program twice or two printers print a document at the same time. Two processes running a program with only one copy of the program read into memory, and two processes sharing a copy of read-only code, do not change the concept of two processes running.
Process creation
The operating system needs a way to create processes, or a way to create processes. Some very simple systems that are designed to run just one program, such as the controller in a microwave oven, may create all the processes needed when the system starts up. But for the general system, that is, the operating system we usually use, because it also supports users to dynamically start and close a program, so the operating system needs to provide some way to create and cancel processes on demand at runtime. Therefore, for a general-purpose system, the creation process scenarios are diverse, mainly including the following four events:
- The OS initializes the creation process
- The process creates a new process through a system call
- The user manually creates a new process
- Initialize a batch job to create a new process
The OS initializes the creation process
When the operating system starts, several processes are typically created. Some of these are foreground processes and some are postnatal processes. A foreground process is a process that interacts with a user (human) and does the work for the user. Background processes, also known as daemons, are not directly related to a specific user and have specific functions. Such as email, printing documents, and regular reminders. In UNIX (understood here as all POSIX-based systems, including Linux, FreeBSD, macOS, Android, iOS, Solaris), you can use the ps command to list running processes. In Windows, you can view it using task Manager.
The process creates a new process through a system call
In addition to the system initializing the creation process, a process (a running program) can also create one or more new processes at run time by issuing system calls to assist the former in its work.
The user manually creates a new process
On interactive systems (such as UNIX and Windows), a user can type a command from the command line or double-click an icon to launch a program. Both operations start a new process and run the selected program in the new process.
Initialize a batch job to create a new process
This process creation scenario is only applicable to mainframe batch systems. A user submits a batch job in such a system, possibly remotely, and when the operating system finds resources to run another job, a new process is created and then runs the job in its input queue.
Command to create a process
- There is only one system call to create a new process on UNIX systems: fork
- The system call to create a process in Windows is CreateProcess
Technically, all four of the above scenarios for creating a new process are created by an existing process executing a system call to create the process. The latter can be a running user process, a system process started by a keyboard or mouse, or a batch management process. System calls tell the operating system to create a new process and, directly or indirectly, specify programs to run in that process.
In general, the process that performs the system call requesting the creation of a new process is the parent process, and the new process created is called a child process.
About the address space of the parent process
After the child process is created, the parent and child processes have different address Spaces. That is, if one of the processes changes a word in its address space, the change is invisible to the other processes. In UNIX and Windows, the initial address space of the child process is a copy of the parent process, that is, the full copy of the parent process’s address space, but there are two different address Spaces involved. Non-writable memory areas are shared, and writable memory areas are not shared. However, it is possible for a newly created process to share other resources of its creator, such as a file that was opened before the process was created. In addition, file sharing in the address Spaces of different processes generally falls into two categories:
- Unwritable memory area shared.
- Parent and child processes have their own address Spaces. Because program text cannot be modified, some UNIX systems allow program text to be shared between the two.
- Memory is shared by copy-on-write.
- Parent and child processes have their own address Spaces. But the child still can share the address of the parent all memory, in this case copy Shared memory by writing, realistic reproduction means between once want to modify this part of the Shared memory, first of all, to copy the memory copy content to its own address space, in order to ensure that changes occur in private memory area, namely, its own address space.
To sum up, no matter what system, parent and child processes have their own, independent address Spaces. Rather, each process has its own, independent address space. Writable memory areas are not shareable.
Termination of a process
A process is created and exits, and the moment a process begins, it ends, because there is no eternity. The termination of inheritance is usually caused by four conditions:
- Normal exit (voluntary)
- Exit normally when work is completed. Exit is called on UNIX and ExitProcess is called on Windows
- Error exit (voluntary)
- The process found an error and exited. For example, the compiler failed to compile a. C file and quit
- Critical error exit (involuntary)
- Exit due to an internal program error. Such as an illegal call, reference to memory that does not exist, or division by zero.
- On some systems, such as UNIX, processes can tell the operating system to handle certain types of errors themselves. When these errors occur, the process receives a signal from the system (interrupted), that is, the process is interrupted by a signal from the system, so that the process knows that an error has occurred, rather than exiting unexpectedly when such errors occur.
- Killed by another process (involuntarily)
-
Process A makes A system call (such as kill in UNIX) to tell the operating system to kill some other process B.
-
On UNIX systems, the system call is kill; In Windows, the system call is TerminateProcess.
-
Processes that execute kill or TerminateProcess must have certain authorization.
-
On some systems, when a process terminates, all processes created by that process are killed immediately. But UNIX, Linux, Windows and other common systems don’t work this way.
The hierarchy of processes
In UNIX, after the parent process creates the child process, the parent and child processes form a hierarchy of parent and child nodes. The child process can also continue to create its own children, so that the parent and descendant processes form a tree hierarchy. In UNIX, a process and all its children and descendants form a process group. When a user sends a signal from the keyboard, that signal is sent to all members of the process group associated with the keyboard. That is, all processes in the same group can receive the keyboard signal. Each process can catch the signal, ignore it, or take the default action of being killed by it.
In WIndows, there is no such thing as a process group. All processes are of the same status. The only similarity to the UNIX process hierarchy is that when a process is created, the parent process gets a special token, a handle, which can be used to control the child process, but the parent process has the right to pass the token to other processes.
Process status
Processes have three states:
- Running state
- Running. The current process occupies CPU at this time
- The ready state
- Ready to run. However, it is temporarily stopped because other processes are zhanyongCPU, waiting for other processes to release CPU
- Blocking state
- Cannot run. Even if the CPU is idle, the process cannot run unless some external event occurs
Above, there are four possible transitions between the three states of the process, as follows:
- The process is blocked waiting for input
- The process entered the ready state because the scheduler chose another process (using CPU resources)
- The process is running because it was selected by the scheduler (using CPU resources)
- A process moves from a blocked state to a ready state because of valid input
As for the next state of the current state of the process, the author also makes a summary as follows:
- The current state is running
- The next state can be blocked, waiting for some external event, such as user input, to occur
- The next state can be a ready state, where the scheduler allocates CPU resources to another process
- The current state is ready
- The next state can only be a running state, waiting for the scheduler to allocate CPU resources to itself
- The current state is blocked
- The next state can only be ready, that is, ready after an external event occurs, waiting for CPU resources
We already know that the process model is an abstraction of the running program, and using the process model makes it easy to imagine the operating state inside the system. According to the state transition of process model, the operating system needs to schedule process switchover according to some rules. For example, when process A is waiting for user input, the operating system can switch THE CPU to process process B’s task to achieve the maximum CPU utilization. So, at the bottom of the operating system is the scheduler, and there are many processes on top of the scheduler. All the details of interrupt handling, starting the process, and stopping the process are hidden in the rescheduler. In fact, the scheduler is a very short program. Ideally, the rest of the operating system would be simply organized into processes that run on top of the scheduler, but few real systems are implemented this way. The diagram below shows a simple diagram of a scheduler and process.
Process implementation
To implement the process model, the operating system maintains this table (an array of structures), known as the Process Table. Each process occupies a process table entry, which represents a process. This table contains the process status of important information, including: the program counter (PC), the stack pointer (FP, SP), memory allocation state, the state of the open file, accounts, scheduling information, and the other in the process of running state to the ready state information or blocking state must be preserved, thus ensuring process then can start again, just like never be interrupted.
Multiprogramming model
We already know that the characteristics of multiprogramming systems are:
- Multiple independent processes exist in memory at the same time
- Macroparallelism
- Microserial
This feature also brings the advantage of multi-programming model: can improve CPU utilization, reduce CPU idle wait time. Strictly speaking, the CPU will always run at full load if the split time a process takes to calculate is 20% of its time in memory and there are five processes in memory at the same time. However, this model is too optimistic in reality because it assumes that all five processes will not wait for I/O at the same time, which is itself not valid.
A better model is to look at CPU utilization in probability. Assume that the ratio of the time a process waits for I/O operations to the time it stays in memory is p. When there are n processes in memory, the probability that all n processes are waiting for I/O is PNP ^ NPN (all waiting for I/O means the CPU is idling at this time). The CPU utilization formula is as follows:
CPU usage = 1 – PNP ^ NPN
This concludes the concept of a process, and the next article will introduce the concept of threads.
By Kimiko VV (original author)
PS: Unless otherwise stated, all articles are original works, copyright belongs to the author, please contact the author for authorization and indicate the source!