Some concepts related to linux process

Some knowledge to know before understanding the process.
Von Neumann architecture
1: Input device for inputting data and programs;
2: Memory for storing programs and data;
3: Computing unit to complete data processing;
4: Controlling program execution ; a
5: output device outputs the processing results.
Insert picture description here
Operating system
Any computer system contains a basic set of programs called the operating system (OS).
That is, the operating system includes the
kernel (process management, memory management, file management, driver management) and
other programs (such as function libraries, shell programs, etc.)

The operating system can also be understood as a software dedicated to management

System calls and library functions
The set of all system calls provided by the implementation of the operating system is a program interface or application programming interface (API). It is the interface between the application and the system.
In the use of system calls, the functions are relatively basic, and the requirements for users are relatively high. Therefore, deliberate developers can appropriately encapsulate some system calls to form a library. With a library, it is very beneficial to higher-level users or Developers carry out secondary development.
Process and process management
Process concept: the program being executed, an instance of the program.
How is the
process information stored? The process information is placed in a data structure called the process control block, which can be understood as a collection of process attributes, collectively referred to as For the PCB control block (including pid, program counter, process status, memory pointer, context data, process priority, IO information and accounting information), under linux pcb is a structure named task_struct.
task_struct is through a linked list The form is stored in the linux kernel

The method of viewing the process
can be viewed under the /proc/ system file, or through the top, ps command

Each process has a unique identifier, which is the process identifier (pid). The
process pid can be viewed through getpid, and the parent process pid is through getppid.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
    
    
 printf("pid: %d\n", getpid());
 printf("ppid: %d\n", getppid());
 return 0;
}

We can call
fork() once and return twice through the **fork()** provided by the system call to create a process . These two returns bring back their respective return value functions, namely the child process and the parent process.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
    
    
 int ret = fork();
 if(ret < 0){
    
       //小于0,创建失败
 perror("fork");
 return 1;
 }
 else if(ret == 0){
    
     //==0,child
 printf("I am child : %d!, ret: %d\n", getpid(), ret);
 }else{
    
     //father
 printf("I am father : %d!, ret: %d\n", getpid(), ret);
 }
 return 0;
}

When we execute this program, we will find that the program will have two results, as if there is a problem with if else, but it is not.
When we execute fork, he will copy the original process information and generate a new process , Which is a child process, (so if else goes twice, once is the original, once is the copy), the code sharing data between them is independent.
Process status
There are many states of process, and I will list the common ones below. The process status can be viewed through the ps command to view the
R running status (running): it does not mean that the process must be running, it indicates that the process is either running or in the run queue.
S sleep state (sleeping): means that the process is waiting for the event to complete (sleep here is sometimes called interruptible sleep).
D Disk sleep is sometimes called uninterruptible sleep. Processes in this state usually wait for the end of IO.
T stop state (stopped): You can stop (T) the process by sending a SIGSTOP signal to the process. The suspended process can continue to run by sending the SIGCONT signal.
X dead state (dead): Processes that have died, this state is just a return state, you will not see this state in the task list.
Z (zombie)-Zombie process:
Zombie state (Zombies) is a special state. When the process exits and the parent process (using the wait() system call, described later) does not read the return code of the child process exit, a zombie (corpse) process will occur. The zombie process will remain in the process table in a terminated state and will Have been waiting for the parent process to read the exit status code. Therefore, as long as the child process exits, the parent process is still running, but the parent process does not read the status of the child process, and the child process enters the Z state.
Therefore, the zombie process will bring the harm of memory leakage and should be avoided.
Orphan processes
when the parent process to exit before the child process, the child process becomes an orphan process, but it will be itchy init process, that is, as a child init process will not cause a resource leak.
Environment Variables
Environment variables (environment variables ) Generally refers to some parameters used in the operating system to specify the operating environment of the operating system, such as the location of the temporary folder and the location of the system folder.
These parameters have certain functions. For example, path tells the system that when the system is required to run a program without telling it the complete path where the program is located, in addition to searching for the program in the current directory, the system should also go to which directories to find
and Some commands related to environment variables

  1. echo: display the value of an environment variable
  2. export: set a new environment variable
  3. env: display all environment variables
  4. unset: clear environment variables
  5. set: Display locally defined shell variables and environment variables.
    We can also get environment variables through the following code
#include <stdio.h>
int main(int argc, char *argv[], char *env[])
{
    
    
/*argc:命令行参数的个数
argv:命令行参数
env:某一个环境变量的值*/
 int i = 0;
 for(; env[i]; i++){
    
          //环境变量指针数组以null结束  
 printf("%s\n", env[i]);
 }
 return 0;
}

Get the value of an environment variable

#include <stdio.h>
#include <stdlib.h>
int main()
{
    
    
 printf("%s\n", getenv("PATH"));   //getenv(const char* name)
                                   //获取名为PATH环境变量的值
 return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45295598/article/details/103447046