The operating system

1. What is the difference between threads and processes?

Process: The execution of a program, that is, once the program is loaded into memory and ready to execute, it is a process. Process is the basic concept of resource allocation, the basic unit of scheduling operation, and the unit of concurrent execution in the system.

Process is an execution process of the program, is a dynamic concept, is the basic unit of program allocation and management of resources in the execution process, each process has its own address space, at least five basic states, they are: initial state, execution state, waiting state, ready state, termination state.

Threads: Each task executed in a single process is a thread. A thread is the smallest unit of operation performed in a process.

The thread is the basic unit of CPU scheduling and dispatching. It can share all the resources owned by the process with other threads belonging to the same process.

A thread can belong to only one process, but a process can have multiple threads. Multithreading allows multiple tasks to be performed at the same time in a single process.

Similarities: Both processes and threads have ID/ register groups, status and priorities, information blocks, and can change their own attributes after creation. Both processes can share resources with their parents, and neither process or thread can directly access resources of other unrelated processes or threads.

Connection: Threads are part of a process, a thread can only belong to one process, and a process can have multiple threads, but at least one thread.

A program has at least one process, and a process has at least one thread. 2. In addition, the process has an independent memory unit in the execution process, and multiple threads share the memory, thus greatly improving the running efficiency of the program. 4. Threads are different from processes in their execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must depend on the application, which provides multiple thread execution control. 5. From a logical point of view, the meaning of multithreading is that multiple parts of an application can be executed simultaneously. However, the operating system does not treat multiple threads as multiple independent applications to achieve process scheduling and management and resource allocation. This is an important difference between a process and a thread.

When to use threads and when to process

The selection of processes and threads depends on the following points: (1) priority threads that need to be created and destroyed frequently; Because it is expensive for a process to create and destroy a process. ② The thread switching speed is fast, so the use of threads in the need for a lot of calculations, frequent switching, and time-consuming operations using threads can improve the response of the application. ③ Because the efficiency of the CPU system on the use of threads is more dominant, so it may be developed to multi-machine distribution process, multi-core distribution with threads. (4) Threads are used in parallel operations, such as C/S server concurrent threads responding to user requests. ⑤ When more stable and safe is needed, it is suitable to choose the process; When speed is needed, threads are better.

2. The status of the thread

There are five basic states, they are: initial state, execution state, wait state, ready state, terminate state.

3. What is the way of interprocess communication? Can threads communicate?

InterProcess Communication (IPC) refers to the dissemination or exchange of information between different processes. There are pipes (including nameless pipes and named pipes), message queues, semaphores, shared storage, sockets, Streams, etc. Socket and Streams support two IPC processes on different hosts.

Common thread communication methods include wait/notify wait, Volatile memory sharing, CountDownLatch concurrency, and CyclicBarrier concurrency

4. Multi-threading and multi-process

5. Blocking and non-blocking

Synchronization: When a function call is made, it does not return until the result is received. That means you have to do one thing at a time and wait until the first thing is done before you can do the next thing.

For example, normal B/S mode (synchronous) : Submit the request -> wait for the server to process it -> return the client browser cannot do anything during this time

Asynchrony: Asynchrony is the opposite of synchronization. When an asynchronous procedure call is made, the caller does not get the result immediately. The part that actually handles the call notifies the caller through status, notifications, and callbacks after completion.

For example, an Ajax request (asynchronous) : the request is triggered by an event -> processed by the server (which the browser can still do other things) -> processed

Block: A blocked call is when the current thread is suspended until the result of the call is returned (the thread enters a non-executable state, in which the CPU does not allocate time slices to the thread, i.e. the thread is suspended). The function returns only after the result is obtained.

One might equate blocking calls with synchronous calls, but they are different. For synchronous calls, many times the current thread is still active, but logically the current function does not return, it preempts the CPU to perform other logic, and it proactively checks if the IO is ready.

Non-blocking: As opposed to blocking, the function does not block the current thread but returns immediately until the result is not immediately available.

It is easy to understand:

  1. Synchronization is when I call a function and the function doesn’t end before I die waiting for the result.

  2. Asynchronous is when I call a function, I don’t need to know the result of the function, and I am notified when the result of the function is available (callback notification).

  3. Blocking is calling me and I won’t return until I’ve received the data or the result.

  4. Non-blocking, which means calling me, I immediately return, notifying the caller by select

6. Five IO models

1) Blocking I/O (blocking I/O)

2) Non-blocking I/O (nonblocking I/O)

3)I/O multiplexing (select and poll)

4) Signal Driven I/O (SIGIO)

5) Asynchronous I/O (Asynchronous I/O (the POSIX AIo_functions)

LINUX

1. Common Commands in Linux

1) restart reboot

②关机poweroff、shutdown -h now

3 Run the ifconfig command to query the local IP address

④ Vi and Vim editors

General mode, insert mode, bottom line mode

Normal mode (by pressing iaoIAO key)–> Insert Mode Insert Mode (by pressing Esc key)–> Normal mode

General mode (by pressing: key)–> Bottom row Mode Bottom row Mode (by pressing Esc key)–> General mode

In bottom line mode, wq = write quit writes and exits

wq! If you can’t save the exit, use wq! ! Forced out of

q! = quit ! Force No write Force exit

⑤ Check the contents in the directory

​ ls = list

Grammar:

Ls [directory name]

Example:

Ls Displays all contents in the current directory

Ls /etc View all contents in the etc directory (absolute path)

All files in the directory

Ls spring/ If spring exists in the current directory, you can view it using the relative path

​ ls spring/springmvc

-a Displays all files in the directory, including hidden files

-l Displays all files in a directory in long format (displays detailed information about files or directories).

Ls minus L can be simplified to ll

-t Displays the contents in a directory in reverse order by update time

ls -a /etc

ls -l /etc

Ls -l -t /etc is equivalent to ls -lt /etc

⑥ Change directory CD = change directory

⑦ Deleting a file rm =remove

2. Details about the grep command in Linux

Grep command is a powerful text search tool in Linux system. It can use regular expression to search text and print matched lines. Grep is the Global Regular Expression Print version. All users are allowed to use it.

grep [options]

The main parameter is grep –help

-c: displays the count of matched rows.

-i: case insensitive.

-h: The file names are not displayed when multiple files are queried.

-l: Only the file names containing matching characters are displayed when multiple files are queried.

-n: Displays the matching line and its number.

-s: does not display the error message that does not exist or has no matching text.

-v: displays all lines that do not contain the matching text.

–color=auto: the keyword part can be found with color display.

3. Print the file list in chronological order and by file size

ls

Sort by size: [root@localhost ~]# ls-sh

[root@localhost ~]# ls-rt

4. How does Linux view the files associated with a process?

What is the lsof command? Can list information about files opened by the process.

5.Linux startup process

The startup process of Linux system is not as complicated as many people think. The process can be divided into five stages:

Kernel boot. Run init. The system is initialized. Set up the terminal. The user logs in to the system. How does Linux view processes

6. Linux checks which command the thread uses

1. Use the top command, top -h with this option, each line of top is not a process, but a thread.

2. Run the ps command, which is ps -xh

This allows you to see all existing threads, or you can use grep for further filtering.

3. Use the ps command (ps-mq PID) to view the number of threads generated by the specified process.

Hand rip code

1. Calculate the output size of the convolutional network

The calculation formula of convolutional neural network is as follows: N=(W-F+2P)/S+1 where N is the output size

W: input size F: convolution kernel size P: filling value size S: step size

2. NMS

import numpy as np
def py_cpu_nms(dets, thresh) :
    """Pure Python NMS baseline."""
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]
    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = scores.argsort()[::-1]  #[::-1] indicates descending sort, and the output is its corresponding sequence number
    keep = []                     # Bounding boxes that need to be preserved
    while order.size > 0:
        i = order[0]              # select the box with the highest confidence
        keep.append(i)            # use it as a reserved box
        # The IOU of the following boxes with the highest computational confidence (ORDER [0]) and all other boxes (Order [1:], i.e. the second to last) are represented and calculated in vector form below
        xx1 = np.maximum(x1[i], x1[order[1:]]) # Calculate the Max of Xmin, namely Xmin of overlap
        yy1 = np.maximum(y1[i], y1[order[1:]]) # Calculate the Max of Ymin, namely Ymin of overlap
        xx2 = np.minimum(x2[i], x2[order[1:]]) # Calculate the min of xmax, namely xmax of overlap
        yy2 = np.minimum(y2[i], y2[order[1:]]) # Calculate the min of ymax, namely ymax of overlap
        w = np.maximum(0.0, xx2 - xx1 + 1)      # Calculate width of overlap
        h = np.maximum(0.0, yy2 - yy1 + 1)      # Calculate hight of overlap
        inter = w * h                           Calculate the area of overlap
        ovr = inter / (areas[i] + areas[order[1:]] - inter) -inter because the intersection part is added twice.
        inds = np.where(ovr <= thresh)[0]In this round, order only keeps subscripts whose IOU is not greater than the threshold
        order = order[inds + 1]                    # Delete the box whose IOU is greater than the threshold
return keep

Copy the code

3. Calculate IOU codes by hand

def IOU(x1,y1,X1,Y1, x2,y2,X2,Y2) :
    xx = max(x1,x2)
    XX = min(X1,X2)
    yy = max(y1,y2)
    YY = min(Y1,Y2)
    m = max(0., XX-xx)
    n = max(0., YY-yy)
    Jiao = m*n
    Bing = (X1-x1)*(Y1-y1)+(X2-x2)*(Y2-y2)-Jiao
return Jiao/Bing

def bb_intersection_over_union(boxA, boxB) :
    boxA = [int(x) for x in boxA]
    boxB = [int(x) for x in boxB]
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])
    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
    iou = interArea / float(boxAArea + boxBArea - interArea)
return iou

Copy the code

4. Hand SoftNMS

import numpy as np
def soft_nms(dets, sigma=0.5, Nt=0.5, method=2, threshold=0.1) :
    box_len = len(dets)   # number of boxes
    for i in range(box_len):
        tmpx1, tmpy1, tmpx2, tmpy2, ts = dets[i, 0], dets[i, 1], dets[i, 2], dets[i, 3], dets[i, 4]
        max_pos = i
        max_scores = ts
        # get max box
        pos = i+1
        while pos < box_len:
            if max_scores < dets[pos, 4]:
                max_scores = dets[pos, 4]
                max_pos = pos
            pos += 1
        # add max box as a detection
        dets[i, :] = dets[max_pos, :]
        # swap ith box with position of max box
        dets[max_pos, 0] = tmpx1
        dets[max_pos, 1] = tmpy1
        dets[max_pos, 2] = tmpx2
        dets[max_pos, 3] = tmpy2
        dets[max_pos, 4] = ts
        Assign the box with the highest confidence to the temporary variable
        tmpx1, tmpy1, tmpx2, tmpy2, ts = dets[i, 0], dets[i, 1], dets[i, 2], dets[i, 3], dets[i, 4]
        pos = i+1
        # NMS iterations, note that box_len changes if detection boxes fall below threshold
        while pos < box_len:
            x1, y1, x2, y2 = dets[pos, 0], dets[pos, 1], dets[pos, 2], dets[pos, 3]
            area = (x2 - x1 + 1)*(y2 - y1 + 1)
            iw = (min(tmpx2, x2) - max(tmpx1, x1) + 1)
            ih = (min(tmpy2, y2) - max(tmpy1, y1) + 1)
            if iw > 0 and ih > 0:
                overlaps = iw * ih
                ious = overlaps / ((tmpx2 - tmpx1 + 1) * (tmpy2 - tmpy1 + 1) + area - overlaps)
                if method == 1:    # linear
                    if ious > Nt:
                        weight = 1 - ious
                    else:
                        weight = 1
                elif method == 2:  # gaussian
                    weight = np.exp(-(ious**2) / sigma)
                else:              # original NMS
                    if ious > Nt:
                        weight = 0
                    else:
                        weight = 1
                Give the box a new confidence level
                dets[pos, 4] = weight * dets[pos, 4]
                # If box scores below threshold thresh, the box is discarded by swapping with the last box
                if dets[pos, 4] < threshold:
                    dets[pos, 0] = dets[box_len-1.0]
                    dets[pos, 1] = dets[box_len-1.1]
                    dets[pos, 2] = dets[box_len-1.2]
                    dets[pos, 3] = dets[box_len-1.3]
                    dets[pos, 4] = dets[box_len-1.4]
                    box_len = box_len-1
                    pos = pos-1
            pos += 1
    keep = [i for i in range(box_len)]
    return keep
if __name__ == '__main__':
    dets = [[0.0.100.101.0.9], [5.6.90.110.0.7], [17.19.80.120.0.8], [10.8.115.105.0.5]]
    dets = np.array(dets)
    result = soft_nms(dets, 0.5)
    print(result)

Copy the code

5. Write a k – means

import pandas as pd
import numpy as np
import random as ran
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d # 
from sklearn.cluster import KMeans
 
def model_test() :
    data = open_file("C:\\Users\\happy\\Desktop\\Iris1.csv")
    dataset = np.delete(data,-1,axis=1) Drop the last column
    k_means = KMeans(n_clusters=3) # Build a model
    k_means.fit(dataset)
    km4_labels = k_means.labels_
    ax = plt.subplot(projection='3d')
    ax.scatter(dataset[:,0],dataset[:,1],dataset[:,2],\
               c=km4_labels.astype(np.float))
    ax.set_zlabel('Z')  # axis
    ax.set_ylabel('Y')
    ax.set_xlabel('X')
    plt.show()

Copy the code

6. Write basic operations for Python sets

Two common scenarios for collections are: 1. De-weighting (e.g. list de-weighting); 2. 2. Relational testing (e.g., intersection, union, difference, etc.)

7. Write a cross entropy loss function

Cross entropy loss function: the distance between the actual output (probability) and the expected output (probability), that is, the smaller the cross entropy value, the closer the two probability distributions will be.


H ( p . q ) = x ( p ( x ) log q ( x ) + ( 1 p ( x ) ) log ( 1 q ( x ) ) ) H(p, q)=-\sum_{x}(p(x) \log q(x)+(1-p(x)) \log (1-q(x)))
def cross_entropy(a,y) :
    return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))
# tensorflow version
loss = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))

# numpy version
loss = np.mean(-np.sum(y_*np.log(y),axis=1))
Copy the code

8. Softmax function

Softmax function: Associates the activation value with the output value of all neurons, the activation value of all neurons adds up to 1. The activation output of the JTH neuron of layer L (the last layer) is:


a j L = e Z j L k e Z t L a_{j}^{L}=\frac{e^{Z_{j}^{L}}}{\sum_{k} e^{Z_{t}^{L}}}
def softmax(x) :
    shift_x = x - np.max(x)# prevent nan output when input increases
    exp_x = np.exp(shift_x)
    return exp_x / np.sum(exp_x)
Copy the code

9. Hand deduce BN formula

In the above formula, M refers to the mini-batch size.

The source code to achieve

m = K.mean(X, axis=-1, keepdims=True)# Calculate the mean
std = K.std(X, axis=-1, keepdims=True)# Calculate standard deviation
X_normed = (X - m)/(std + self.epsilon)# normalization
out = self.gamma * X_normed + self.beta# Reconstruction transform
Copy the code

10.Python opens a file and finds the fastest way to find a string

Python string look-ups have four methods: 1 find, 2 index, 3 rfind, and 4 rindex.

11. Write down the formulas for confusion matrix, accuracy and recall

Accuracy = (TP+TN)/(TP+TN+FN+TN)

Accuracy (PRECISION, or PPV, positive predictive value) = TP/(TP + FP)

Recall (or sensitivity, TPR, True Positive Rate) = TP/(TP + FN)

F1-score = 2TP/(2TP+FP+FN)

12. Required to draw the structure diagram of LSTM (including cell state, hidden State, door structure and signal transmission process)

Refer to the link

Blog.csdn.net/u013348164/…

Blog.csdn.net/A_snail/art…

Blog.csdn.net/weixin_4220…

Blog.csdn.net/weixin_4653…

Blog.csdn.net/jacke121/ar…

Blog.csdn.net/zhangliaobe…

Blog.csdn.net/weixin_4207…