day28 summary

review

1. The sticking problem Package:
1) can not confirm the size of the transmission data.
Transmission 2) a short time many small data amount and short interval of one-time data is sent packing.

2.struct
may be a very long data, compressed to a fixed length, such as four.

3. stick package solve the problem:
1. get struct module by a header.
The dictionary can be compressed into a header and sent to the server.

2. Upon receipt of the service termination header decompression, get the real length of the data dictionary.
Receiving real data dictionary.

Note: The data dictionary can carry descriptive information to be sent, as well as real data size.

4. upload large files
1. The client sends to the server dictionary
2. Upon receipt of the service termination dictionary, using the name in the dictionary file, create a file name to the name.
3. The dictionary contains the data file size is large, and reception cycle little by little.

5.UDP:
- without establishing a connection
- no stick package problem
- send data without waiting for the return successfully received
- data secure, easily lost

6.socketserver
socket-based module package having a built-in theadingTcp (), can support multiple users simultaneously connected to the server.

7.FTP:
Client: start
server: start

History of operating system development

1) punch cards:
a computer room, a card can only be used once.
Drawback:
the CPU utilization low.

2) online batch processing system
to support multiple users to use a computer room.

3) offline batch processing system
high-speed disk:
improve the reading speed of the file.
Benefits:
increase CPU utilization

3) Multi-channel technology (Based on the mononuclear case):
- Single channel:
a plurality of CPU is used when using a serial.

  • Multi-channel technology:
    • Multiplexing (*******) in space:
      a CPU may be provided to a plurality of users to use.

    • Multiplexing (*******) in time:
      the switching state of preservation +

      IO operations:
      INPUT ()
      Print ()
      the time.sleep (. 3)

      1) If the CPU has encountered an IO operation, the program will execute the current CPU use right off immediately.
      Advantages:
      High CPU utilization.

      2) If a program uses the CPU time is too long, the current CPU executes the program the right to use immediately disconnected.
      Disadvantages:
      execution of the program is reduced.

  • Parallel and Concurrent:
    Concurrent: it refers looks like running simultaneously, a plurality of switching programs + kept save state.
    Parallel: run on a real sense, in the case of multi-core (multiple CPU), the simultaneous execution of multiple programs.

process

  • Procedures and processes
    - the program: a bunch of code.
    - Process: The process of a bunch of code to run.

      - 进程调度:
          当代操作系统调度:
              时间片轮转法 + 分级反馈队列
    
          1)先来先服务调度:
              a,b程序,若a程序先来,先占用CPU。
    
          缺点:
              程序a先使用,程序b必须等待程序a使用cpu结束后才能使用。
    
          2)短作业优先调度:
              a,b程序,谁的用时短,先优先调度使用cpu。
    
          缺点:
              若程序a使用时间最长,有N个程序使用时间短,
              必须等待所有用时短的程序结束后才能使用。

3) round-robin process
time executed by the CPU 1 second, N program loading, to 1 second and the like into a plurality of N time slices.

4) grading feedback queue
execution priority level is divided into multiple layers.
- Class 1; highest priority.
- Level 2: The priority of the second, inferior race, and so on.
- Class 3
....

  • Three state of the process:
    • Ready state:
      will enter the ready state when all processes created, ready to dispatch.

    • Running state:
      after the process scheduler, enter the running state.

    • Blocking state:
      Those who encountered IO operation process, will enter the blocking state.
      If the IO end, they must re-enter the ready state.

Synchronous and asynchronous

It refers to the submission of the task.

  • Sync:
    If two tasks required to submit, at the time of submission of the first task,
    you must wait for the end of the execution of the task, you can continue to submit and execute the second task.

  • Asynchronous:
    If two tasks required to submit, at the time of submission of a task,
    do not need to wait and immediately can submit and execute the second task.

  • Blocking and non-blocking:
    • Blocking:
      blocking state. IO will encounter obstruction.

    • Non-blocking:
      ready state
      to run state

Synchronous and asynchronous, blocking and non-blocking is the same concept it?
He stressed: not the same concept, can not be confused!

Maximize improve CPU utilization:
minimize unnecessary IO operations.

import time


def test():
    # IO操作
    # time.sleep(3)

    # 计算操作
    num = 1
    for line in range(1000000000000000000000000):
        num += 1


if __name__ == '__main__':
    test()
    print('hello tank')
# from multiprocessing import Process
# import time

'''
创建进程方式一:
'''
# # 1.定义一个任务
# def task(name):
#     print(f'{name}的任务开始执行')
#     time.sleep(1)
#     print(f'{name}的任务已经结束')
#
#
# # 在linux/mac系统下不会报错
# # p = Process(target=task, args=('jason',))
#
# if __name__ == '__main__':
#     # target=执行函数的地址
#     p = Process(target=task, args=('jason',))
#     # 向操作系统提交创建进程的任务
#     p.start()
#     print('主进程')

'''
windows:
    创建子进程,windows会将当前父进程代码重新加载执行一次。
    
    
linux/mac:
    会将当前父进程代码重新拷贝一份,再去执行。
'''


# 创建进程方式二:
# 1.自定义一个类,并继承Process
# class MyProcess(Process):
#
#     # 父类的方法
#     def run(self):
#         print('任务开始执行')
#         time.sleep(1)
#         print('任务已经结束')
#
#
# if __name__ == '__main__':
#     p = MyProcess()
#     p.start()
#     # p.start()
#     print('主进程')

'''
join方法: 用来告诉操作系统,让子进程结束后,父进程再结束。
'''
# from multiprocessing import Process
# import time
#
#
# def task(name):
#     print(f'{name} start...')
#     time.sleep(2)
#     print(f'{name} over..')
#
#
# if __name__ == '__main__':
#     p = Process(target=task, args=('jason', ))
#     p.start()  # 告诉操作系统,开启子进程
#     p.join()  # 告诉操作系统,等子进程结束后,父进程再结束。
#     print('主进程')


from multiprocessing import Process
import time


def task(name, n):
    print(f'{name} start...')
    time.sleep(n)
    print(f'{name} over..')


if __name__ == '__main__':
    p1 = Process(target=task, args=('jason', 1))
    p2 = Process(target=task, args=('egon', 2))
    p3 = Process(target=task, args=('sean', 3))
    p1.start()
    p2.start()  # 告诉操作系统,开启子进程
    p3.start()  # 告诉操作系统,开启子进程

    p1.join()  # 告诉操作系统,等子进程结束后,父进程再结束。
    p2.join()
    p3.join()

    print('主进程')
'''
进程间数据相互隔离:
    主进程与子进程会产生各自的名称空间。

'''
from multiprocessing import Process

x = 100


def func():
    print('执行func函数...')
    global x
    x = 200


if __name__ == '__main__':
    p = Process(target=func)
    p.start()
    print(x)
    print('主')
'''
current_process().pid: 获取子进程号
os.getpid(): 获取主进程pid号

cmd中查看进程号: tasklist |findstr 进程号

进程号回收的两种条件:
    1.join,可以回收子进程与主进程。
    2.主进程正常结束,子进程与主进程也会被回收。

os.getppid()
'''
from multiprocessing import Process
from multiprocessing import current_process
import os  # 与操作系统交互
import time


def task(name):
    print(f'{name} start...', current_process().pid)
    time.sleep(1)
    print(f'{name} over..', current_process().pid)


if __name__ == '__main__':
    p = Process(target=task, args=('jason', ))
    p.start()  # 告诉操作系统,开启子进程

    # 判断子进程是否存活
    print(p.is_alive())

    # 直接告诉操作系统,终止 子进程
    p.terminate()
    time.sleep(0.1)

    # 判断子进程是否存活
    print(p.is_alive())

    p.join()  # 告诉操作系统,等子进程结束后,父进程再结束。

    print('主进程', os.getpid())
    print('主主进程', os.getppid())
    time.sleep(100)

Two conditions recycling process ID:
1.join, the child can be recycled to the main process.
2. The main process ends normally, the child process will be recycled to the main process.

Zombie process and orphaned

Zombie process:
refers to the child process has ended, but there are PID number, not destroyed.
Disadvantages:
occupation PID number, occupation operating system resources.

Orphan process:
refers to the child process is still running, but the parent process ends unexpectedly.
Operating system optimization mechanisms:
a orphanage, to help you recover the child without a father.

Daemon

It refers to the end of the primary process, all child processes that followed the end of the primary process generated and recycled.

from multiprocessing import Process
from multiprocessing import current_process
import time


def task(name):

    print(f'{name} start...', current_process().pid)

    time.sleep(5)

    print(f'{name} over..', current_process().pid)

    print(f'管家{name}')


if __name__ == '__main__':
    p1 = Process(target=task, args=('jason', ))

    # 添加守护进程参数
    p1.daemon = True  # True代表该进程是守护进程
    p1.start()

    print(f'egon 驾鹤西去...')

Guess you like

Origin www.cnblogs.com/zhm-cyt/p/11715310.html