April 23 python learning to summarize socket UDP and operating system theory, multi-channel theory

1. Socket UDP

  udp is unconnected, and no error will be reported on which end is started first.  

UDP (user datagram protocol, user datagram protocol) is connectionless, message-oriented, and provides efficient services. The block merge optimization algorithm will not be used. Since UDP supports a one-to-many mode, the skbuff (socket buffer) at the receiving end adopts a chain structure to record each arriving UDP packet. There is a message header (information source address, port, etc.) in the packet, so that it is easy for the receiving end to distinguish and process. That is, message-oriented communication has message protection boundaries.

    server

#Server 
 ss = socket()    #Create a server 
socket 
 ss.bind() #Bind        the server socket 
 inf_loop: #Server        infinite loop 
     cs = ss.recvfrom()/ss.sendto() #Dialog ( receive with send) 
 ss.close(                          ) #Close the server socket

  client  

#Client 
cs = socket()    #Create client 
socket 
comm_loop: #Communication       loop cs.sendto 
    ()/cs.recvfrom() #Dialog    ( send/receive) 
cs.close() #Close                       client socket

  Simple example of udp socket  

 1 #_*_coding:utf-8_*_
 2 __author__ = 'Linhaifeng'
 3 import socket
 4 ip_port=('127.0.0.1',9000)
 5 BUFSIZE=1024
 6 udp_server_client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 7 
 8 udp_server_client.bind(ip_port)
 9 
10 while True:
11     msg,addr=udp_server_client.recvfrom(BUFSIZE)
12     print(msg,addr)
13 
14     udp_server_client.sendto(msg.upper(),addr)
server
 1 #_*_coding:utf-8_*_
 2 __author__ = 'Linhaifeng'
 3 import socket
 4 ip_port=('127.0.0.1',9000)
 5 BUFSIZE=1024
 6 udp_server_client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 7 
 8 while True:
 9     msg=input('>>: ').strip()
10     if not msg:continue
11 
12     udp_server_client.sendto(msg.encode('utf-8'),ip_port)
13 
14     back_msg,addr=udp_server_client.recvfrom(BUFSIZE)
15     print(back_msg.decode('utf-8'),addr)
client

 

2. Operating system

   Refer to the teacher's blog: http://www.cnblogs.com/linhaifeng/p/6295875.html

   1. What is an operating system?

    Operating system is a control program located between computer hardware and software for coordinating management and control of computer hardware and software resources

   2. Two major functions of the operating system:

     1. Encapsulate complex and ugly hardware operations into beautiful interfaces and provide them to applications.

     2. Make the competition of the process for the hardware in an orderly way

 

3. Multi-channel theory

    Multi-channel in multi-channel technology refers to multiple programs. The implementation of multi-channel technology is to solve the orderly scheduling problem of multiple programs competing or sharing the same resource (such as cpu). The solution is multiplexing . Multiplexing is divided into multiplexing in time and multiplexing in space .

    1. Spatial reuse : Divide the memory into several parts, and put each part into a program, so that there are multiple programs in the memory at the same time. 

    2. Time multiplexing : when one program is waiting for I/O, another program can use the cpu. If enough jobs can be stored in the memory at the same time, the cpu utilization rate can be close to 100%, similar to our primary school. Co-ordination methods learned in mathematics . (After the operating system adopts the multi-channel technology, it can control the switching of processes, or compete for the execution rights of the CPU between processes. This switching will not only be carried out when a process encounters io, but                                             a process will occupy the CPU for too long . It will also switch , or the execution authority of the cpu is taken away by the operating system)   

    Detailed explanation:    

Modern computers or networks are multi-user. Multiple users not only share hardware, but also share information such as files and databases. Sharing means conflict and disorder.

The operating system mainly uses

    1. Document which program uses what resource

    2. Allocate resource requests

    3. Mediation of conflicting resource requests for different programs and users.

We can summarize the functions of the above operating systems as: processing multiple (multiple or multiple) shared (shared or multiplexed) resource requests initiated by multiple programs, referred to as multiplexing. There are two types of multiplexing. Method to realize

1. Multiplexing in time When a resource is multiplexed in time, different programs or users use it in turn. After the first program acquires the resource and finishes using it, it is the second one's turn. . . The third. . .

     For example: there is only one cpu, and multiple programs need to run on the cpu. The operating system first allocates the cpu to the first program, and the program runs for a long enough time (the length of time is determined by the algorithm of the operating system) or When I/O is blocked, the operating system allocates the cpu to the next program, and so on, until the first program is reassigned to the cpu and then runs again. Since the cpu switching speed is very fast, the feeling to the user is these Programs run concurrently, or concurrently, or pseudo-parallel. As for how resources are time-multiplexed, or who should be the next program to run, and how long a task needs to run, these are the work of the operating system.

2. Spatial multiplexing Each client acquires a small part of a large resource, thereby reducing the time of queuing for resources.

  For example, when multiple running programs enter the memory at the same time, the hardware layer provides a protection mechanism to ensure that their respective memories are separated and controlled by the operating system, which is much more efficient than a program that monopolizes the memory and queues up to enter the memory one by one. Other resources for space reuse are disks, and in many systems, one disk holds files for many users at the same time. Allocating disk space and keeping track of who is using which disk blocks are typical tasks of operating system resource management. The combination of these two methods is a multi-channel technology

 

The biggest problem with spatial multiplexing is that the memory between programs must be divided. This division needs to be implemented at the hardware level and controlled by the operating system. One program can access the memory of another program if the memory is not partitioned from each other

            The first loss is security. For example, your qq program can access the memory of the operating system, which means that your qq can get all the permissions of the operating system.

       The second loss is stability. When a program crashes, it may also reclaim the memory of other programs. For example, if the memory of the operating system is reclaimed, the operating system will crash.

 

 

 

 

 

Guess you like

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