[Linux] Process easy to get started

Table of contents

1. Von Neumann architecture

1. Storage structure

​edit

Second, the operating system 

1. Concept

2. The purpose of designing the OS

3. Positioning

4. How to understand "management"

5. Summary 

Three, the process

1. Concept

 So how to distinguish process & program?

2. PCB - describe the process

3. Organizational process

Supplement: cwd

 4.  task_struct ——PCB

 (1) PID - identifier

5. Parent-child process 

Supplement: fork() function

fork function - if shunt

Parent and child processes - respective priorities

6. Process status [Linux operating system]

1. New:

2. Running status:

3. Blocking:

4. Suspend:

6. Process state in liunx kernel 

(1) S sleep state (sleeping):

 (2) R running status (running):

(3) D disk sleep state (Disk sleep):

(4) T stop state (stopped):

T differs from S:

(5) X death status (dead):

(6) Z zombie state:

The dangers of zombie processes

(7) Orphan process

 7. Process priority

modify priority


1. Von Neumann architecture

1. Storage structure

         We know the execution speed of a program, which requires data input and output. Then if the CPU directly interacts with the input device, it will be like this, the input device inputs data for 10s, the CPU takes 1ms to process, and then the output takes another 10s, which will lead to poor machine performance;

And von Royman thought of using memory to deal with the CPU, which turned into 4 seconds of data input, 1ms of processing, and 4 seconds of output in a disguised form to improve computer efficiency .

 

To give an example: I communicate with an old cousin on WeChat . The flow chart of data flow between them is as follows:

Second, the operating system 

1. Concept

Any computer system contains a basic collection of programs called an operating system (OS). Generally speaking, the operating system includes:
Kernel (process management, memory management, file management, driver management)
Other programs (such as function libraries, shell programs, etc.)

2. The purpose of designing the OS

Interact with hardware and manage all hardware and software resources
Provide a good execution environment for user programs (applications)

3. Positioning

In the entire computer hardware and software architecture, the positioning of the operating system is: a pure "management" software

4. How to understand "management"

Management steps:
(1.) Describe the managed object
(2.) Organize managed objects

About the operating system, bank example:

5. Summary 

  The operating system is to provide users with a safe, stable and simple execution environment .

1. Operating system management: first describe, then organize

2. The external service of the operating system is provided by: system call interface 

Three, the process

1. Concept

Textbook concepts: an execution instance of a program, a program being executed, etc.
Kernel point of view: Acts as the entity that allocates system resources (CPU time, memory).

(In fact, we start a software, essentially creating a process ; and entering a command in linux creates a process at the system level)  

 So how to distinguish process & program?

Question: Can a large number of processes exist in a system? The answer is: yes;

When a large number of processes enter the memory, the CPU needs to sort the priority of the processes, but it can't do it  , it can only read data, then it needs to manage the processes at this time, so how do we create processes?

1. Describe first    (through the structure (PCB), describe the process attribute block)
2. Reorganization      (organize the process through data structures, such as linked lists, sequential lists, red-black trees, etc.)

Then we start to manage the process:

2. PCB - describe the process

         Process information is placed in a data structure called a process control block, which can be understood as a collection of process attributes. The textbook is called PCB (process control block), and the PCB under the Linux operating system is: task_struct

In windows, PCB is a structure representing a process module ;

In Linux, the specificity of PCB is struct task_struct {......//All attributes of the process}

task_struct is a type of PCB , and the structure describing the process in Linux is called task_struct. task_struct is a data structure of the Linux kernel that is loaded into RAM (memory) and contains process information.

3. Organizational process

It can be found in the kernel source code. All processes running in the system are stored in the kernel in the form of task_struct linked list. 

So how do we check a process? 

(1). We make an infinite loop program, and then execute the program.

 

(2) Open another window to view the process.

Command: ps axj | grep "programs to check" 

 Going back to the infinite loop program, we exit the infinite loop program through ctrl + c , and we can find that the infinite loop process disappears by checking again.

Similarly, we can enter the top command, and this is the task manager under Linux 

Supplement: cwd

Enter ls /proc // The function is: to display the process in the form of a file

 Let's enter a process at random and check the file properties inside. Here is the cwd file we are looking for:

 Turn into a detailed page: ls /proc/13712 // 13712 is the PID of the Test process

What are these PPIDs and PIDs ? We will analyze them below.

 4.  task_struct ——PCB

 We can see earlier that there are these classes

  • Identifier (PID) :   The unique identifier describing this process is used to distinguish other processes .
  • Status :   Task status, exit code, exit signal, etc.
  • Priority :   Priority relative to other processes. (Permissions determine whether you can or cannot, and the priority is the order after you can)
  • Program Counter :   The address of the next instruction to be executed in the program . (So ​​the program counter is constantly being modified)
  • Memory pointers :   including pointers to program code and process-related data, as well as pointers to memory blocks shared with other processes. ( The data in the process can be found through the PCB )
  • Context data :    The data in the registers of the processor when the process is executed.   [ Example of suspension of school, need to add CPU , register ]
  • I / O status information : including the displayed I/O requests , the I / O devices assigned to the process and the list of files used by the process.
  • Billing information : May include sum of processor time, sum of clocks used, time limit, billing account number, etc.
  • other information

 (1) PID - identifier

Function:  identifier (PID) :   the unique identifier describing this process , used to distinguish other processes .

 So how do we get the PID of a specific process in the main function ?

getpid() // Get the PID of this process

getppid() // Get the PID of the parent process

 Need to include the system function header file: #include <sys/types.h>  

In the source code, we use the getpid() function to obtain the PID  of this program

The result is shown in the figure:

In addition to ctrl + c to terminate the process when the program is running , you can also enter the command   kill -9 PID   to end the process. Kill is not explained here.

5. Parent-child process 

Supplement: fork() function

Function: At the fork function call, a child process is created independent of the parent process, and the code is shared between the father and the child after the fork .

Return value: 1. Return the PID of the child process in the parent process ; 2. Return 0 in the child process ; 3. Return -1 on failure

Q: Why are there two return values?

Answer: In the fork() function, the parent and child processes each execute their own return statement

 

fork function - if shunt

 We know that the parent and child processes are common, the code after the fork function. But most of the parent and child processes run different codes, so how do we separate them?

Use the return value of fork to perform if shunting:

id is the PID of the child process in the parent process; 0 in the child process.

Regarding the internal logic of the fork function:

Create a process whose internal attributes are templated by the internal attributes of the parent process .

Parent and child processes - respective priorities

Question: In the parent-child process, should the parent process be executed first, and then the child process be executed?

uncertain

Reason: The CPU may run a parent process for 10ms, then stop executing it, and enter the run queue again, while the child process is executing at this time. Regarding who runs first, not necessarily, this depends on the scheduler decision of the operating system .

6. Process status [Linux operating system]

1. New :

literal meaning. It is to create a process for a program.

2. Running status :

When task_struct starts queuing in the run queue, it is in the running state.

3. Blocking :

Waiting for non-CPU resources is called blocking state.

For example: when we perform scanf / cin for keyboard input, we are waiting for keyboard data input, which is a blocking state.

4. Suspend :

When the memory is insufficient, the OS properly replaces the code and data of the process to the disk. At this time, the process is called a suspended state.

6. Process state in liunx kernel 

In order to figure out what a running process means, we need to know the different states of the process. A process can have several states (in the Linux kernel, a process is sometimes called a task.)

(1) S sleep state (sleeping):

It means that the process is waiting for the event to complete (sleep here is sometimes called interruptible sleep - you can interrupt S through the kill -2 command, and it is T state )

Corresponding to blocking state 

Input: while :; do ps axj | head -1 && ps axj | grep running file name | grep -v grep ; sleep 1; done // You can view the latest process running status, the cycle is 1 second

 (2)  R running status (running):

Does not mean that the process is necessarily running, it indicates that the process is either running or in the run queue .

 Take the above code as an example, comment out the cout code, so that the output device resources will not be occupied, and it will always be in the R state.

 Details : We found that there are S+ and R+ in the status bar,what does "+" mean? The meaning of "+" here is a foreground process , the result will occupy the chat interface between us and bush, we can't input commands, we can exit with ctrl + c; similarly, the status without "+" is a background process , which will not occupy the chat box, we can continue to enter instructions. But when you start the program, you need to ./program & , and it will return its PID. When not in use, kill -9 PID to kill it.

(3) D disk sleep state (Disk sleep):

Sometimes also called uninterruptible sleep (uninterruptible sleep) - deep sleep, the process in this state usually waits for the end of IO. (It cannot be passively interrupted, even kill cannot be killed unless the power is pulled out )

(4) T stop state (stopped):

A process can be stopped (T) by sending a SIGSTOP signal to the process. The suspended process can be resumed by sending the SIGCONT signal. (eg: gdb debugging )

 

kill -19 // suspend the process

kill -18 // continue the process

T differs from S:

 The sleep process is waiting for non-CPU resources, corresponding to the blocking state. Paused state, originally running state, not waiting for resources, just paused.

(5) X death status (dead):

This status is just a return status, you will not see this status in the task list. (When a large number of processes end, the operating system is too busy, then the process will be marked as X process and ready to be recycled at any time )

(6) Z zombie state:

A process has exited, but cannot be recycled by the OS, and is in a state waiting to be detected , which is called a zombie state.

Then why is there such a state? For state preservation , let the parent process or OS detect it. (The so-called retention: means that the code and data are recycled, and the PCB of the process is still retained. )

The dangers of zombie processes

  1. The exit status of the process must be maintained, because he wants to tell the process (parent process) that cares about it, the task you entrusted to me, and how I am doing. But if the parent process never reads, the child process will always be in the Z state ? Yes!
  2. Maintaining the exit status itself requires data maintenance, which also belongs to the basic information of the process, so it is stored in task_struct (PCB) . In other words, the Z state has not been exited, and the PCB has always been maintained ? Yes!
  3. That parent process creates a lot of child processes, but if they don't recycle, will it cause a waste of memory resources ? Yes!    Because the data structure object itself will occupy memory, think about defining a structure variable (object) in C, it is to open up space in a certain location of memory!
  4. memory leak ? yes

(7) Orphan process

concept:

If the parent process exits early, then the child process exits later and enters Z, what should I do? The parent process exits first, and the child process is called an "orphan process". The orphan process is adopted by the No. 1 init process, and of course the init process must be recycled.

We simply create a scenario where the parent process exits first and the child process exits later. Discover:

 7. Process priority

 concept:

The order of cpu resource allocation refers to the priority of the process. Processes with higher priority have the right to execute first. Configuring process priority is useful for Linux in a multitasking environment and can improve system performance.
You can also run the process on a specified CPU. In this way, assigning unimportant processes to a certain CPU can greatly improve the overall performance of the system.

To view the priority, enter:

ps -al // will display all processes on the machine 

We can easily notice several important information, as follows:
UID: represents the identity of the executor
PID: represents the code name of this process
PPID : Represents which process this process is derived from, that is, the code name of the parent process
PRI : Represents the priority level at which this process can be executed, the smaller the value, the earlier it will be executed by the CPU
NI : represents the nice value of this process, the correction value of the priority, ranging from -20 to 19, a total of 40 levels. (Every time the nice value is set, the PRI is the default value of 80)

The specific method of Linux priority:

priority = old priority ( PRI ) + nice value ( NI

modify priority

Input: top command;  press "r" after entering top –> input process PID –> input nice value 

 Sometimes it is necessary to increase the authority, just sudo (provided: the trusted user has been set)

epilogue

This section is over here, thank you friends for browsing, if you have any suggestions, welcome to comment in the comment area; if you bring some gains to your friends, please leave your likes, your likes and concerns will become bloggers The driving force of the master's creation.

Guess you like

Origin blog.csdn.net/qq_72112924/article/details/131031973