And comparing the difference between process and thread

First, what is the process, what is the thread?

1.1 Process

  • The process includes multiple threads, processes and between processes are relatively independent.
  • There is a process of logical memory , each process will have to allocate a separate memory space, but also assigned a file / network handles,
  • Handle a similar identifier, all the processes are more or less have a certain number of references to handle, the handle is actually a pointer to a memory that contains specific information data, so the handle is made when you want to access the process and they are finished using must be released. (An analogy, I want to use redis need to open port 6379, this time need to access the handle, get access to the memory address to obtain corresponding resources to carry out the process)
  • A process can have more than the number of handles, but only one process ID
  • The number of handles different processes referenced time is not necessarily


     
    Process Structure

1.2 thread

  • There is a thread stack memory (a lot of people call it the stack), mainly used to store the memory address of a local variable or resources
  • There is also a PC block, a main memory address of the next instruction is executed, a PC is pointing to a common memory (a process which is assigned to the logical memory system).
  • TCS: ThreadLocalStorage variable to store the main thread of their own definition, do not want to share with other threads, understood as a private variable thread.
  • Resources threads share the process are: global and static variables block process, process, process open file descriptors, signal processor, process the current directory, process ID, process group ID .

Second, the difference between process and thread

  • The process is the smallest unit of resource allocation procedures, the thread is the smallest unit of program execution.
  • Process has its own memory address space, threads contained in the address space of a process.
  • With respect to communication between threads and processes between the process more convenient, threads can share the process assigned to the logical memory resources. In other words, the thread in the same process share global variables, static variables and other data, sharing specific content 1.2 Introduction thread already explained.
  • Overhead allocation process than the large thread, but the robustness of the process is higher than the thread because interprocess not affect each other, hang up the threads may cause a process to crash.
  • You can also say, the thread is a unit of program execution, the process just thread storage container, the container resources thread of execution.

Communication among the three, processes and process

Because the processes and between the processes are independent of each other, so regarding the communication between the data sharing process complicated, need the IPC; data is separate, the synchronization is simple, but the memory for more complex switching, the CPU utilization rate.

IPC typically have ways: pipe (named pipes including pipes and unnamed), message queues, semaphores, shared memory, Socket, Streams and the like .

3.1 Pipeline

Pipeline, usually unnamed pipes, UNIX system is the oldest form of IPC.

Features 3.1.1 pipeline

  • It is half-duplex (i.e., data flows only in one direction) , having a fixed end and a read write terminal.
  • It can only be used in the process of having the genetic relationship of communication between (also between parent and child or sibling processes).
  • It can be seen as a special kind of file that can be read for its use ordinary read, write and other functions. But it is not a regular file, it does not belong to any other file system, and exists only in memory .

3.1.2 prototype structure

When a pipeline is established, it creates two file descriptors: fd [0] is opened for reading, fd [1] opened for writing. As shown below: To turn off the pipeline can be closed only the two file descriptors.


 
Pipeline prototype

Example 3.13

A single process piping almost no use. So, the process is usually called pipe then calls the fork, thus creating IPC channel between the parent and the child. As shown below:


 
Examples

To the data stream flows from the parent child process, the parent of the closed end of the read (fd [0]) and the terminal processes the write (fd [1]); otherwise, the data stream can be made to flow from the parent child process .

3.2 Named Pipes FIFO

FIFO, also known as named pipes, it is a file type.

3.2.1 Features

  • 有名管道也是半双工的通信方式,但是它允许无亲缘关系进程间的通信。
  • FIFO可以在无关的进程之间交换数据,与无名管道不同。
  • FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中

3.2.2 介绍

FIFO的通信方式类似于在进程中使用文件来传输数据,只不过FIFO类型文件同时具有管道的特性。在数据读出时,FIFO管道中同时清除数据,并且“先进先出”。

3.3 消息队列

消息队列,是消息的链接表,存放在内核中。一个消息队列由一个标识符(即队列ID)来标识。用户进程可以向消息队列添加消息,也可以向消息队列读取消息。

3.3.1 特点

  • 消息队列是面向记录的,其中的消息具有特定的格式以及特定的优先级
  • 消息队列独立于发送与接收进程。进程终止时,消息队列及其内容并不会被删除,所以另一个进程读写的时候要判断前进程是否已经读完数据。
  • 消息队列可以实现消息的随机查询,消息不一定要以先进先出的次序读取,也可以按消息的指定类型读取

3.4 信号量

信号量(semaphore)与已经介绍过的 IPC 结构不同,它是一个计数器。信号量用于实现进程间的互斥与同步,而不是用于存储进程间通信数据。

3.4.1 特点

  • 信号量用于进程间同步,若要在进程间传递数据需要结合共享内存
  • 信号量基于操作系统的 PV 操作,程序对信号量的操作都是原子操作
  • PV each semaphore operation on the semaphore value is not limited to plus or minus 1 1, and can be any positive integer addition and subtraction .
  • Semaphore support group.

3.5 Shared Memory

Shared memory (Shared Memory), refers to two or more processes share a given memory area.

3.5.1 Features

  • Shared memory is one of the fastest IPC , because the process is a direct memory access.
  • Because multiple processes can operate simultaneously, so the need for synchronization.
  • + Shared memory semaphore commonly used together, semaphore used to synchronize access to the shared memory.

3.6 Comparison of the five kinds of communication

1. Line: slow, limited capacity , only a parent and child can communicate
2.FIFO: can any interprocess communication, but slower
3. Message Queue: when system capacity is limited, and pay attention to the first reading, to consider a problem not read data
4. semaphore: complex message is not transmitted, can be used to synchronize
the shared memory area 5.: can be easily controlled capacity, high speed, but to maintain synchronization , such as a process of writing when another process to pay attention to the issue of literacy, which is equivalent thread-safe threads, of course, it can also be used as a shared memory area inter-thread communication, but will not be necessary, which would have shared between threads in the same process a piece RAM.

Fourth, communication (multi-threaded) between threads and threads

4.1 Synchronization

4.2 wait and notify / notifyAll mechanism

4.3 while polling way

4.4 a pipe communication

4.5 Semaphore Semaphore



Author: light blue best friend
link: https: //www.jianshu.com/p/00bd894ff86c
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/sandea/p/11777416.html