Skip to main content

Computer Science


Process v.s. Thread

Definition

  • Process: A process is an instance of a program running on a computer. It is close to the concept of a task or a job.
  • Thread: A thread is a unit of execution within a process. A process can have one or more threads.

...

做个简单的比喻:进程=火车

  • 线程在进程下行进(单纯的车厢无法运行)
  • 一个进程可以包含多个线程(一辆火车可以有多个车厢)
  • 不同进程间数据很难共享(一辆火车上的乘客很难换到另外一辆火车,比如站点换乘)
  • 同一进程下不同线程间数据很易共享(A车厢换到B车厢很容易)
  • 进程要比线程消耗更多的计算机资源(采用多列火车相比多个车厢更耗资源)
  • 进程间不会相互影响,一个线程挂掉将导致整个进程挂掉(一列火车不会影响到另外一列火车,但是如果一列火车上中间的一节车厢着火了,将影响到所有车厢)
  • 进程可以拓展到多机,进程最多适合多核(不同火车可以开在多个轨道上,同一火车的车厢不能在行进的不同的轨道上
  • 进程使用的内存地址可以上锁,即一个线程使用某些共享内存时,其他线程必须等它结束,才能使用这一块内存。(比如火车上的洗手间,"互斥锁")
  • 进程使用的内存地址可以限定使用量(比如火车上的餐厅,最多只允许多少人进入,如果满了需要在门口等,等有人出来了才能进去,"信号量")

GIL(Global Interpreter Lock)

  • GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once.
  • For I/O-bound tasks, GIL is not a problem because the GIL will be released when the thread is waiting for I/O.

Time slice rotation

  • Time slice rotation is a scheduling algorithm that assigns CPU time to each process/thread in equal portions and in order, handling all processes/threads without priority.
  • Advantages
    • Responsive: each process/thread gets some CPU time. For example, if a process/thread is waiting for I/O, other processes/threads can still run in the next time slice.
    • Fair: each process/thread gets the same amount of CPU time.

Data Storage

Reference

jsdelivr