Process-related concepts
I. Theoretical Basis of process control: in the operating system, a process is defined as a running activity of a program with certain independent functions. A program is called a process during running.
2. The process has the following features:
1. Dynamic: The program is static and dynamic when running.
2. Concurrency: multiple processes run simultaneously
3. Independence: processes are independent.
4. Asynchronization: processes are also asynchronous.
3. process three-state diagram: Think about the status of a process after it is created? What is the status of running or blocking?
4. process ID
Process ID (PID): the unique number of the identity process, the ID of the parent process (ppid), and the user ID (UID) of the Startup Process)
5. Process mutex
Process mutex means that when several processes use a shared resource, the process that allows a process to use other resources at any time must wait, the resource is released by the resource owner.
Vi. Critical resources:
In the operating system, only one process is allowed to access resources at a time.
VII. critical section:
The program code that accesses critical resources in a process is called a critical section. To achieve mutex access to critical resources, ensure that processes are mutually exclusive to their respective critical zones.
8. Process Synchronization:
A group of concurrent processes are executed in a certain order. A group of concurrent processes with synchronization relationships are called cooperative processes. signals sent between cooperative processes are called messages or events.
9. process scheduling:
Process Scheduling refers to selecting one of the processes to run from a group of processes to occupy the CPU.
1. Scheduling Method: preemptible and non-preemptible. A preemptible process is forced to stop. Non-preemptible instances are still running.
2. Scheduling Algorithm: service scheduling algorithm first
Short Process Priority Scheduling Algorithm: Short Process Priority refers to the process that takes short CPU running time priority.
High-priority scheduling algorithm
Time slice rotation method: it refers to dividing the CPU time into several processes in turn (the process will not run completely ).
10. Concept of deadlock
Multiple processes form a deadlock due to competition in resources. Without external force, these processes will never be able to move forward.
Process Programming
1. Obtain the process ID
Parent process: the process that generates the currently running process
Child process: parent process creation process
Obtain the ID function vpid_t getpid (void) of the current process)
Obtain the ID function vpid_t getppid (void) of the parent process)
The experiment procedure is as follows:
PID is the id of the current process, and PPID is the parent process of the current process!
2. Create a process
Process Creation using the fork function pid_t fork (void)
Function: Creates a sub-process.
The wonder of fork is that it is called once but returned twice. It may have three different return values:
1. In the parent process, fork returns the PID of the newly created child process;
2. In the sub-process, fork returns 0;
3. If an error occurs, fork returns a negative value.
The Process Creation Program is as follows:
3. Process use instance 2
Process
The child process created using fork is shared with the code segment of the parent process, but the data segment is independent! So the statement printed by the above program should be!
The data space and stack space of the child process are all copied from the parent process, rather than shared. Adding 1 to the Count row in the sub-process does not affect the Count value in the parent process. The Count value in the parent process is still 0.
4. Differences between the pid_t vfork (void) function and the fork Function
Differences:
1. fork: The child process copies the data segment and stack of the parent process.
Vfork: A child process shares data segments, stacks, and code segments with its parent process.
2. fork: The execution sequence of parent and child processes is uncertain.
Vfork: the sub-process runs first, and the parent process runs later.
5. Exec function family
Exec replaces the program that calls it with the executed program.
Differences:
Fork creates a new process and generates a new PID.
Exec starts a new program to replace the original process, so the PID of the process will not change.
1. # include <unistd. h>
Int execl (const char * path, const char * arg1 ,....)
Parameters:
Path: name of the program to be executed (including the complete path ). Arg1-argn: command line parameter required by the program to be executed
Number, including the program name. End with a null pointer.
The procedure is as follows:
2. # include <unistd. h>
Int execlp (constchar * path, const char * arg1 ,...)
Parameters:
Path: name of the program to be executed (excluding the path, which will be searched for in the path environment variable ).
Arg1-argn: the command line parameter required by the program to be executed, including the program name. End with a NULL pointer.
The procedure is as follows:
3. # include <unistd. h>
Int execv (const char * path, char * const argv [])
Parameters:
Path: name of the program to be executed (including the complete path ).
Argv []: array of command line parameters required by the program to be executed.
The procedure is as follows:
6. System functions
# Include <stdlib. h>
Int system (const char * string)
Function:
Call fork to generate a sub-process. The sub-process calls/bin/sh-c string to execute the string parameter.
The System function generates a sub-process !!!
7. Process waiting
# Include <sys/types. h>
# Include <sys/wait. h>
Pid_t wait (int * status)
Function:
Blocks the process until a sub-process exits. It returns the ID of the sub-process.
The procedure is as follows:
If you run the sub-process first, print the first statement and go to sleep for 3 seconds! Run the parent process in the last three seconds.
The parent process starts to wait. Wait for 3 seconds and then execute the child process until the child process exits. Then, continue to execute the parent process!