1/ Operating system
<1> What is an operating system
(1) OperatingSystem is the interface between users and computer, as well as the interface between computer hardware and other software, realizing the "dialogue" mechanism between program developers and computer hardware. Is the most basic system software running directly on the "bare metal", any other software must be supported by the operating system to run. (3) Currently the world's more popular computer operating systems are: Windows OS series of operating systems (developed by Microsoft, including Win95, Win98, Win7, Win10) MacOS (developed by Apple) Linux Chrome OS (developed by Google)Copy the code
<2> Structure of the operating system
<1> Operating systems can be divided into two main parts: the kernel and some programs outside the kernelCopy the code
<3> The role of operating system
<1> Processing Management <2> Memory Management <3> File System <4> Networking <5> Security <6> User Interface <7> Device DriversCopy the code
2 / program
<1> Programs in a computer Generally, a computer program is written in a programming language and runs on a target architecture. Programs can be divided into system programs (drivers, etc.) and applications (browsers, office software, etc.) <2> The relationship between applications and the operating system The application program can notify the operating system to perform a specific action, so that the application program indirectly operates on the hardware through the OS. In fact, the programmer only needs to focus on the interaction between the application and the operating system, and only needs to have a general understanding of how the operating system controls the hardwareCopy the code
3 / process
A program is a process when it runs. A process is an application program (browser, office software, etc.) that is running in the system. Once the program is running, the process <1> process can be regarded as an instance of program execution. Processes are the basic unit of system resource allocation, and each process has its own address space. One process cannot access another process's variables and data structures. If you want one process to access another process's resources, you need to use interprocess communication, such as pipes, files, sockets, etc. <2> A process can have multiple threads (at least one thread), and all threads share the stack space of the process they belong to. One of the main differences between threads and processes is that multiple threads within the same process share some state, and multiple threads can read and write to the same block of memory (one process cannot directly access another's memory). Threads within the same process cooperate with each other.Copy the code
4 / thread
<1> A thread is an entity of a process and an execution path for the process. Can be understood as: the thread is concrete to work. <2> When a thread modifies a process's resources, its sibling threads can see the change immediately. At the same time, each thread has its own register and stack, which other threads can read and write. <3> A process can have multiple threads, each thread using the stack space of its own process. One of the main differences between threads and processes is that multiple threads within the same process share some state, and multiple threads can read and write to the same block of memory (one process cannot directly access another's memory). At the same time, each thread has its own register and stack, which other threads can read and write. <4> Processes are independent of each other, and threads of the same process are shared. Threads in one process are not visible in other processes. <5> Communication: inter-process communication IPC (pipe, semaphore, shared memory, message queue), threads can directly write process data segment (such as global variables) to process communication -- need process synchronization and mutual exclusive means of assistance, to ensure data consistency. <6> Scheduling and switching: Thread context switching is much faster than process context switching. Since switching between processes involves a series of resource/space switches, and threads share process resources, switching between threads does not involve resource switching.Copy the code
5/ Thread synchronization
<1> If multiple threads work together to modify a data, unexpected results may occur. To ensure data correctness, multiple threads need to be synchronized. <2> Simple Thread synchronization can be achieved using Lock and Rlock of Thread class objects, both of which have acquire and release methods. For data that requires only one Thread at a time, Its operations can be placed between acquire and Release methods. <3> The advantage of multithreading is that you can run multiple tasks at the same time (at least it feels that way). However, when threads need to share data, there can be data asynchronism. <4> Consider a case where all elements in a list are 0, the thread "set" changes all elements to 1 from back to front, and the thread "print" reads the list from front to back and prints. So, maybe when the thread "set" starts changing, the thread "print" prints the list, and the output is half 0 and half 1, and that's out of sync. To avoid this, the concept of locking was introduced. <5> Locks have two states: locked and unlocked. Whenever a thread such as "set" wants to access shared data, it must first acquire a lock; If another thread, such as "print", has been locked, then the thread "set" is suspended, that is, synchronous blocking; Wait until the thread "print" is finished and the lock is released, then let the thread "set" continue. After this processing, the list can be printed with either all zeros or all ones, without the awkward situation of half zeros and half ones.Copy the code
5 / code
Use the concurrent.futures module, which is native to PYTHon3, but python2.7 or higher can also be installed and used as follows:Copy the code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from concurrent.futures import ThreadPoolExecutor
import time
def sayhello(a) :
print("hello: "+a)
time.sleep(2)
def main() :
seed = ["a"."b"."c"]
# single thread
start1=time.time()
for each in seed:
sayhello(each)
end1=time.time()
print("time1: "+str(end1-start1))
# Multithreading 1
start2=time.time()
with ThreadPoolExecutor(3) as executor:
for each in seed:
executor.submit(sayhello,each) Call submit(). The output order is out of order
end2=time.time()
print("time2: "+str(end2-start2))
# Multithreading 2
start3=time.time()
with ThreadPoolExecutor(3) as executor1:
executor1.map(sayhello,seed) # call map() to ensure the order of output
end3=time.time()
print("time3: "+str(end3-start3))
if __name__ == '__main__':
main()
Copy the code
6/python3 three modules commonly used in the python3 thread
<1>thread
The module has been discarded. Users can use the threading module instead. Therefore, the "thread" module is no longer available in Python3. Python3 renames thread to _thread for compatibilityCopy the code
<2>_thread
_thread provides a low-level, primitive thread, and a simple lock, which is relatively limited compared to the threading module. Call the start_new_thread() function in the _thread module to generate a new thread. Start_new_thread (function, args[, kwargs]) function - thread function. Args - The argument passed to the thread function. It must be of type tuple. Kwargs - This parameter is optional.Copy the code
Example:#! /usr/bin/python3
import _thread
import time
Define a function for the thread
def f1(threadName, delay) : # thread function, passing in 2 arguments
count = 0
while count < 5:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
# create two threads
try:
_thread.start_new_thread( f1, ("Thread-1".2, ) )
_thread.start_new_thread( f1, ("Thread-2".4)),except:
print ("Error: unable to start thread")
Copy the code
<3> Threading (recommended)
In addition to all the methods in the _thread module, the threading module provides other methods. We can start a new Thread by directly inheriting a new subclass from threading.thread and instantiating it by calling the start() method, which calls the Thread's run() methodCopy the code
Example:#! /usr/bin/python3
import threading
import time
exitFlag = 0
class myThread (threading.Thread) : # create a class that inherits threading.Thread
def __init__(self, threadID, name, counter) :
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self) : # run()
print ("Start thread:" + self.name)
print_time(self.name, self.counter, 5)
print ("Exit thread:" + self.name)
def print_time(threadName, delay, counter) :
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
Create 2 new threads
thread1 = myThread(1."Thread-1".1)
thread2 = myThread(2."Thread-2".2)
# Start a new thread
thread1.start()
thread2.start()
thread1.join()
thread2.join() Block the main thread
print ("Exit main thread")
Copy the code