Preparing for the autumn strokes - Operating Systems

 

Processes and threads:

concept:

Thread: is the smallest unit of an operating system capable of operation scheduling . An execution flow process, a process can run multiple threads.

Process: An example of a program execution .

 

The difference between processes and threads

The process has a separate memory unit in the implementation process , and multiple threads share memory

The main difference between processes and threads is that they are different operating system resource management. Processes have separate address space, after a process crashes, will not have an impact on other processes in protected mode, and the thread is just a process of different execution paths. Thread has its own stack and local variables, but there is no separate address spaces between the threads, a thread to die is tantamount to the whole process dies, so the program than multi-process multi-threaded programs robust, but in the process of switching, consuming greater resource efficiency to be worse. But for some of the requirements at the same time and have to share the concurrent operation of certain variables, only with thread, the process can not be used.

 

The process has a separate memory unit in the implementation process, and multiple threads share the process of memory. (Resources allocated to the process, all threads in the same process of sharing all the resources of the process. The same process multiple threads share code segments (code and constant), the data segment (global variables and static variables), the extended segment (heap memory) but each thread has its own stack segment, stack segment called running time, used to store all local variables and temporary variables.

The process is the smallest unit of resource allocation, the thread is the smallest unit CPU scheduling;

Overhead: Because when you create or undo the process, the system must be recomputed or recycled resources, such as memory space, I / o devices. Therefore, Caozuoxitong pay the cost will be significantly greater than the cost when you create or undo the thread. Similarly, during the process of switching involves saving the entire environment and the current process CPU CPU setting new environmental process is scheduled to run. And thread switching and saving of setting only a small number of registers, the operation does not involve memory management. Visible, the process is much greater than the cost of switching thread switching overhead.

Communication: Because multiple threads in the same process with the same address space, resulting in synchronous and communicate between them, it becomes relatively easy. Interprocess communication IPC, the threads can read and write directly process data segment (such as global variables) to communicate - need aid process synchronization and mutual exclusion means to ensure data consistency. In some systems, the thread switching, synchronization and communication are without the intervention of the operating system kernel

Interprocess not affect each other; thread a thread hang the whole process will lead to hang

 

Interprocess communication

Several primary means of inter-process communication Introduction In linux:

  1. Pipes (Pipe) and named pipes (named pipe) : Pipeline: transport resources. It is the kernel of a buffer nature. (Characteristics: half-duplex, one-way communication) . Linux everything is a file, the operating system provides for the operation of the pipeline method: file operations. Pipeline can be used to have communication between the kinship process, named pipes overcomes the limitations of the pipeline without a name, therefore, in addition to a pipe with the functions, it also allows no communication between the kinship process;

  2. Signal (Signal) : the signal is more complex communication mode for receiving the notification process has some event occurs, except for inter-process communication, the process can also send a signal to the process itself; Linux early signal Unix semantics in addition to supporting function sigal but also support signal sigaction function semantics conform Posix.1 standard (in fact, the function is based on the BSD, BSD order to achieve reliable signal mechanism, but also to unify external interface, use sigaction function to re-implement the signal function);
  3. Message queue (the Message) :

    Message Queue is actually a queue in the operating system kernel created for us by the queue identifier Key , each process can open this queue, each process can insert a node to the queue by the queue or Gets a node to complete the communication between different processes.
    Type of tissue with a user data block added to the queue, the process of acquiring the other data blocks from the queue, i.e., the message queue is sent with a block type; message queue is a full-duplex communication, readable write (data can be sent, you can also receive data) .

    Message queue is a linked list message, comprising the message queue Posix system V message queue. Has sufficient rights process can add a message to the queue, was given permission to read a process you can go read messages in the queue. Signal bearing the message queue overcome small amount of information, only the carrier pipe plain byte stream buffer size is limited, and other shortcomings.

  4. ** ** Shared Memory: allows multiple processes can access the same memory space, is the fastest form of IPC available. For other communication mechanism is not efficient design. Often, as with other communication mechanism for semaphores used in combination, to achieve synchronization and mutual exclusion between processes.

  5. Semaphore (semaphore) : mainly as a means of inter-process synchronization as well as between the different threads in the same process.

  6. Socket (the Socket) : more general inter-process communication mechanism can be used for inter-process communication between the different machines. It was originally developed by BSD Unix systems branch out, but they are generally portable to other Unix-like systems: Linux and System V variants support sockets.

And compare the advantages and disadvantages of various means of communication:

  1. Pipeline: slow , limited capacity, only a parent and child can communicate

  2. Named pipe (named pipe): can any interprocess communication, but slow

  3. Message Queuing: capacity is restricted system, and care should be first read, not to consider the last data read

  4. Semaphore: complex message is not transmitted only for synchronization

  5. Shared Memory: can easily control capacity, fast speed, but to keep pace , such a process at the time of writing, another process should pay attention to the issue of literacy, which is equivalent thread-safe threads, of course, can also be shared memory area used as communication between threads, but will not be necessary, inter-thread already has shared a piece of memory within the same process

 

Guess you like

Origin www.cnblogs.com/DSKer/p/10932739.html