Exploring the concept of linux process

programs and processes

Program: An ordered collection of instructions to accomplish some task

​ Program = code + data, the size command can see the allocation of the program in the memory space

process:

  1. To users: a process is a dynamic execution of a program
  2. For the operating system: a process is the basic unit and the smallest unit for system allocation of resources, and each process has an independent address space and running state

​ Process = code + data + heap + stack

PCB: A data structure used to save process-related information, called task struct in Linux.

Due to the clock interrupt technology, the process has the ability to execute concurrently, that is, the time slice method.

The difference between a process and a program:

  1. program is static
  2. The life of a program is permanent
  3. Program has no PCB information
  4. Process -> one program, program -> multiple processes

MMU address mapping

When we write a program, we can only determine the offset address of the variable, and the physical address in the memory must be added to the segment address.
If the offset (virtual) addresses of the variables created by the two processes overlap, it will be messed up, so the segment address is determined by the operating system.
The MMU is responsible for mapping virtual addresses to physical addresses.
There is also the function of checking the access rights of the address space.

running state of the process

Classic: Ready to run blocking

write picture description here

Seven states of Linux:

User space: user running state

kernel: run-ready uninterruptible sleep interruptible sleep suspend zombie

write picture description here

zombie process

The child process dies, the parent process still exists, and the child still occupies system resources

orphan process

The parent process dies, and the child process is adopted by process No. 1

Detailed explanation of PCB

The process related information of PCB storage is as follows

  1. Process identifier, process state, priority
  2. Program entry address
  3. Timing information
  4. interprocess communication
  5. used internal memory
  6. swap partition
  7. IO devices, cache related data structures
  8. File system related information
  9. Site protection

process identifier

Each process has a unique unsigned int

Process 0: used by the os kernel

Process 1: User process created by 0

The process of creating a general process

  1. Allocate a descriptor and copy most of the data of the parent process PCB
  2. Copy the environment of the parent process
  3. Allocate system resources, code, stack
  4. The contents of the parent process address space are copied to the child process
  5. Set ready state, put into ready queue

Use fork to create a process: https://blog.csdn.net/hanzheng6602/article/details/79971887

  • The /proc/(pid)/ directory holds all the information about the process

Command to view process status

process list ps -ef | grep ""

Page up and down to view top shift >

View process tree pstree

The process priority defaults to 0, and the smaller the value, the higher the priority, between -20 ~ 19

Three ways to adjust priorities

  1. nice -n priority ./a.out before starting program execution
  2. Adjust the existing process renice priority -p pid
  3. top r command

Environment variables and local variables

Local variable definition: name=value can only be used in this process

Environment variable: $ PATH $HOME can be used in this process and child process, not in parent process

Local variables become environment variables: export local variables

Environment variable /etc/profile for all users

env View all environment variables, get all environment variables with the third parameter of the main function.
getenv() function: Get an environment variable and return a char* pointer. It can be used to avoid reading configuration files and to easily obtain environment variable values.

Classification of Kernels

The kernel is the most basic part of the operating system. It is a piece of software that provides many applications with secure access to computer hardware, this access is limited, and the kernel determines when and how long a program operates on a certain piece of hardware. The classification of kernels can be divided into single-kernel and dual-kernel and microkernel. Strictly speaking, the kernel is not a necessary part of a computer system.
A single kernel (Monolithic kernel) is a very large process. Its interior can be divided into several modules, such as the Linux kernel.
Microkernel (Microkernelkernel, the structure consists of a very simple hardware abstraction layer and a set of more critical primitives or system calls, these primitives only include establishing a Several parts necessary for the system, such as thread management, address space and inter-process communication, etc. The goal of the microkernel is to separate the implementation of system services from the basic operating rules of the system. The
hybrid kernel, which is very similar to the microkernel structure, only However, more of its components run in the core state to obtain faster execution speed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904738&siteId=291194637