Python notes: #006#Program execution principle

Principles of program execution (popular science)

Target

  • Three things in a computer
  • The principle of program execution
  • The role of the program

01. The three major components of a computer

The computer contains a lot of hardware, but to run a program, there are three core hardware, namely:

  1. CPU
    • The central processing unit is a very large-scale integrated circuit
    • Responsible for processing data / computing
  2. RAM
    • Temporarily store data (data will disappear after power failure)
    • high speed
    • Small space (high unit price)
  3. hard disk
    • Permanently store data
    • slow
    • Large space (low unit price)
CPU RAM hard disk
CPU RAM hard disk

thinking questions

  1. Which hardware device in the computer is responsible for executing the program?
    • CPU
  2. Is the memory faster or the hard drive faster?
    • RAM
  3. Are our programs installed in memory, or in hard disk?
    • hard disk
  4. I bought a memory stick with 500G of space! ! ! ,is this sentence correct?
    • No, memory sticks usually only have // 4G​​/8G16G32G
  5. After the computer is turned off, the data in the memory will disappear , is this sentence correct?
    • correct

02. The principle of program execution

Schematic diagram of program execution

  1. Before the program runs , the program is saved in the hard disk
  2. when running a program
    • The operating system will first let the CPU copy the program into memory
    • The CPU executes the program memory

For a program to execute, it must first be loaded into memory

2.1 Principle of Python program execution

Schematic diagram of Python program execution

  1. The operating system will first let the CPU copy the program of the Python interpreter into memory
  2. Python interpreter Let the CPU translate the code in the Python program from top to bottom according to the grammar rules
  3. The CPU is responsible for executing the translated code

How big is the Python interpreter?

  • Execute the following terminal command to see the size of the Python interpreter
# 1. 确认解释器所在位置
$ which python

# 2. 查看 python 文件大小(只是一个软链接)
$ ls -lh /usr/bin/python

# 3. 查看具体文件大小
$ ls -lh /usr/bin/python2.7

Tip: The purpose of establishing a soft link is to facilitate users not to remember which

03. The role of the program

Programs are for processing data !

  • News content, commentary... is data provided by news software
  • Product information, delivery information provided by e-commerce software ... is data
  • Motion data provided by motion software ... is data
  • Map information, positioning information, vehicle information provided by map software is data
  • Chat information, friend information provided by instant messaging software ... is data
  • ……

3.1 Think about the startup process of the QQ program

  1. Before QQ runs , it is saved in the hard disk
  2. After running , the QQ program will be loaded into memory

QQ login interface

3.2 Think about the login process of the QQ program

  1. Read the QQ number entered by the user
  2. Read the QQ password entered by the user
  3. Send the QQ number and QQ password to Tencent's server and wait for the server to confirm the user information

thinking 1

Before the QQ program sends the QQ number and QQ password to the server, do you need to store the QQ number and password?

Answer

Definitely needed! - Otherwise, the QQ program will not know what to send to the server!

thinking 2

Where does the QQ program save the QQ number and QQ password ?

Answer

Saved in memory , because the QQ program itself is in memory

thinking 3

How does the QQ program save the user's QQ number and QQ password ?

Answer

  1. Allocate a space for QQ number and QQ password in memory
    • Before the QQ program ends, these two spaces are managed by the QQ program, and no other programs are allowed to use them.
    • Before QQ is used by itself, these two spaces are always only responsible for saving QQ numbers and QQ passwords
  2. Use an alias mark the location of the QQ number and QQ password in memory

Schematic diagram of QQ number and password memory

  • Inside the program, the space allocated in memory for QQ number and QQ password is called a variable
  • Programs are used to process data, and variables are used to store data

Guess you like

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