当前位置:网站首页>Interview process and thread

Interview process and thread

2022-06-09 05:18:00 wolf_ break

0. Reference article

1. Definition

1.1 Process definition

Process is the smallest unit of operating system resource allocation , Threads are the smallest unit of task scheduling and execution .

process , It is the basic unit of allocating and managing resources during the execution of concurrent programs , Represents a logical control flow , Have a separate virtual memory address space . The following is the virtual space allocation model of the process .
 Virtual space allocation model diagram
For further discussion on the virtual space of processes, you can browse linux Step by step exploration of process address space One article .

1.2 Thread definition

Threads are the basic unit of independent scheduling and dispatching , It can also execute concurrently , Sharing process resources , Is a lightweight entity .
The multithreaded process address space is as follows :
 Multithreaded process address space

2. expenses

Process passing fork Create , Threads pass through clone Create .

2.1 Process switching overhead

fork It's expensive , When a child process is created, the memory image of the parent process is copied to the child process ,fork Not just copying the page table structure ( The virtual address corresponds to the physical address ), The file descriptor table of the parent process is also copied (linux Everything below is a document ), Signal control table , Process information , Register resources, etc . It is a deep replication .
Learn more about : Despite the write - time replication technology (fork When it was first created, it adopted shared technology , Only pointer to the physical resources of the parent process , When a child process needs these resources to write , Only then can a piece of physical resources be copied for the child process to use ).
The operation of a process depends on these resources , When switching process context , These resources need to be saved . This is also the defect of multi process switching .

2.2 Thread cost

We know , A thread is one that shares the resources of a process , When a thread makes a context switch , Just save some simple information about the thread running , So thread switching is lightweight .
say concretely , All threads in the same process share not only global variables, but also :

  • Most of the data
  • File descriptor
  • Signal processing functions and signal processing
  • Current working directory
  • user id And groups id
  • Process instruction
    Threads have their own
  • Threads id
  • Register set ( Program counter and stack pointer )
  • Stack
  • errno
  • Signal mask
  • priority

3. communication mode

3.1 Interprocess communication

Different processes have different virtual address spaces , Therefore, the communication between processes cannot simply share memory directly . Common process methods are

  • The Conduit : Half duplex communication , It can only flow in one direction , Suitable for communication between parent and child processes
  • Famous pipeline : Similar pipe , Allow unrelated processes to communicate .
  • Shared memory : Shared memory is mapping a piece of memory that can be accessed by other processes , This shared memory is created by a process , But multiple processes can access . Shared memory is the fastest IPC The way , The semaphore is used to realize the synchronization and communication between processes
  • Message queue : Link table of messages , Stored in the kernel . A message queue consists of an identifier ( That's the queue ID) To mark
  • Semaphore : It's a counter , Used to realize mutual exclusion and synchronization between processes , Do not store messages .
  • Socket

Specific explanations and demo For examples, please refer to the article Interprocess communication (IPC)

3.2 Inter thread communication

A thread is the memory space of a shared process , So consider mutual exclusion and synchronization . Common measures are :

  • lock ( Mutually exclusive ): The mutex 、 spinlocks 、 Read-write lock . Lock mutex before accessing shared resources , Release the mutex after the access is completed , Used to prevent simultaneous access to a shared variable .
  • Condition variables, ( Sync ): It is applicable to a thread that sends a signal to other threads through condition variables after completing a certain action , Other threads do some more actions . Go to sleep while waiting for certain conditions .
  • Semaphore ( Sync ): Using thread semaphores can efficiently complete thread based resource counting

Brief introduction and function use reference unp Chapter 26 or [linux Basics ——linux Summary of inter thread communication and synchronization mechanism (https://blog.csdn.net/a987073381/article/details/52029070).

原网站

版权声明
本文为[wolf_ break]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021429257067.html