# Linux #-process communication between processes and threads #

The concept of process

Process is the concept of the operating system, whenever we execute a program, in terms of the operating system creates a process, in this process, along with the allocation and deallocation of resources. It can be considered a process of execution of a program.

Process (Process) is a computer program run on a set of data on the activities of the system is the basic unit of resource allocation and scheduling, is the operating system infrastructure. In the computer architecture for the early design process, the basic process is the execution of the program entity; in contemporary computer architecture design for the thread, the thread is the process vessel. Program is instruction, organization of data and its description of the process is a solid program.

  • Narrow definition: the process is an instance of a program running (an instance of a computer program that is being executed).
  • Broadly defined: a process is run activities must have a separate function on a data collection program. It is the operating system dynamic execution of a basic unit , in the traditional operating system , the process is both a basic allocation unit , is the basic unit of execution.

There are two main concepts of the process:

  • First, the process is an entity. Each process has its own address space, under normal circumstances, including the text area (text region), the data region (data region) and stack (stack region). Code is executed by a processor stored text area; dynamically allocated memory area stores data used during execution process variables; stack area stores instructions and local variables of the calling procedure activities.
  • Second, the process is an "execution procedures." Program is a lifeless entity, only the processing (execution of the operating system) when the device attached to the program of life, it can become an active entity, we call process

Operating system process is basic and important concepts. After the system is a multi-channel program appears to characterize the dynamics of internal systems appeared, describing a concept system activity patterns within each channel programs introduced, all multiprogramming operating systems are based on the process.

Status of the process:

Intermittent during the process of implementation, determine the process may have multiple states. In fact, the running process could have the following three basic states.

  • 1) the ready state (Ready): the process has been required in addition to processor resources , waiting for allocation of processor resources; as long as the processor allocation process can be executed. Ready process can press more priority to divide the queue . For example, when a time slice expires since the process enters the ready state when discharged into the low priority queue ; when a process by the I / O operation is completed into the ready state when discharged into the high priority queue.
  • 2) operating state (Running): process uses processor resources; in this state the number of processes is less than or equal the number of processors. When no other process can execute (such as all processes are blocked state ), usually automatically execute the system idle process.
  • 3) blocking state ( Blocked ): As the process is waiting for some conditions (such as I / O operation or process synchronization ), can not proceed until the conditions are met. Even before this event occurs to allocate processor resources to the process and can not run.

The concept of process

Thread (English: thread) is an operating system capable of operation scheduling smallest units. It is included in the process of being, it is the process of the actual operation of the unit. A thread refers to a process in a single sequential flow of control, a process multiple threads concurrently, each thread in parallel to perform different tasks. In the Unix System V and SunOS it is also referred to as lightweight processes (lightweight processes), but more lightweight process refers to kernel threads (kernel thread), while the user thread (user thread) called threads.

A thread is the basic unit of scheduling and dispatching of independence. For the operating system kernel threads can be scheduled kernel threads, such as Win32 threads; voluntarily by the user process scheduling user threads, such as the Linux platform, the POSIX  the Thread; or by the kernel and user processes, such as Windows 7 thread, mixed scheduling.

The same process multiple threads to share all system resources of the process, such as virtual address space, file descriptors , and signal processing , and so on. However, multiple threads in the same process has its own call stack (call stack), register your environment (register context), own thread local storage (thread-local storage).

The main concept of the process there are five points:

1) Light entities: the thread is basically an entity does not own system resources , just a little essential to ensure the independent operation of the resource. Thread entities, including programs, data and TCB. A thread is a dynamic concept, which is described by the dynamic characteristics of the thread control block TCB (Thread Control Block). TCB includes the following information:

  • (1) thread state.
  • (2) When the thread is not running, it is stored on-site resources.
  • (3) a group of the execution stack.
  • (4) local variable storage area of ​​the main memory for each thread.
  • (5) access to the same process of main memory and other resources.

Is used to indicate a sequence of instructions to be executed the program counter , to retain local variables , a small number of state parameters and return addresses like a set of registers and stack .

2) the basic unit of scheduling and dispatching of independence: in a multithreaded OS, the thread is independently run basic unit, which is also independent of the scheduling and dispatch of basic units. As the thread is "light", so switching threads quickly and very little overhead (in the same process of).

3) can be executed concurrently: In a process among multiple threads can execute concurrently, and even allow all the threads can execute concurrently in one process; likewise, different threads in the process can be executed concurrently, full utilization the processor and peripheral devices operating in parallel capabilities.

4) shared process resources: in the same process each thread, can share resources owned by the process, which first manifested in: All threads have the same address space (process's address space), which means that you can thread open You can also have access to the process; each virtual address to access the address space of the file , timers, semaphores institutions. Since the same process threads within the shared memory and file , it does not have to communicate with each other thread calls the kernel .

State of the thread:

Thread of the life cycle can be divided into five states. .

1.  New state (New)          : After the thread object is created, entered the new state. For example, Thread thread = new Thread () .
2.  ready state (Runnable) : also known as "executable state." After the thread object is created, other thread calls the start () method of the object, so as to start the thread. For example, thread.start (). Thread a state of readiness at any time may be executed by the CPU scheduling.
3.  Run state (Running)  : CPU thread to obtain permission for execution. Note that, the thread can only be entered from the ready state to the running state.
4.  blocked (Blocked)   : the thread is blocked for some reason to give up the right to use CPU temporarily stops running. Until the thread into the ready state, a chance to go running. Case of obstruction of three categories:

  •     (01) waits for blocking - by calling the thread wait () method, thread to wait for the completion of a job.
  •     (02) synchronous blocking - a thread get synchronized synchronous lock failure (because the lock was occupied by another thread), it will enter synchronous blocking state.
  •     (03) other obstruction - through the sleep () or join the calling thread () or issue an I / O request, the thread into the blocked state. When sleep () timeout, the Join () or a timeout wait for a thread to terminate, or I / O processing is completed, the thread into the ready state again.

5.  death state (Dead)     : thread execution is over or due to abnormal exit the run () method, the thread end of the life cycle.

The concept of process communication

Process is an entity. Each process has its own address space. Therefore, the user space process is independent of each other, generally can not access each other. But in many cases required inter-process communication with each other, to accomplish a function of the system. Process to coordinate their behavior with each other through communication between the kernel and other processes.

Process communication refers to the transmission of data (exchange of information) between the process. How much communication process according to the level and efficiency of the exchange of information, divided into low-level communications and advanced communications.

  • Lower communication: only transmission state and integer values, mainly used for synchronization, mutual exclusion between the process terminates, control is passed to hang like information. Common low-level interprocess communication: semaphores (semophore), signal (sinal).
  • Advanced Communication: improve the efficiency of signal communication, large amounts of data transfer, reduce the complexity of programming, mainly for the exchange and sharing of data between processes blocks. High common-process communication: pipes (pipe), named pipes (named pipe), shared memory (shared memory), socket (Socket)

Process communication scenarios

  • Data transfer: a process needs to transmit its data to another process, an amount of data transmitted in bytes to several megabytes.
  • Share Data: multiple processes want to share operational data, a process modifications to shared data, other process should be seen immediately.
  • Notification events: a need to send a message to another process or group of processes, informing it (them) some event occurs (such as when to notify the parent process terminates).
  • Resource sharing: sharing the same resources among multiple processes. In order to do this, we need to provide the kernel locking and synchronization mechanisms.
  • Process Control: Some want to completely control the process of execution of another process (such as Debug process), and the control will process all want to be able to intercept and exceptions into another process, and be able to timely know its status changed.

 

Way communication process

 

Pipes (pipe):

Pipeline includes three types:

 

Semaphore (semophore):

Semaphore is a counter, multiple processes can be used to control access to shared resources. It is often used as a locking mechanism to prevent access to shared resources is a process, other processes can also access the resource. Therefore, the main as well as inter-process synchronization means between the different threads within the same process.

 

Message queue (message queue):

Message queue is a linked list of messages, by message queue identifier stored in kernel. Signaling message queue overcome the less information, only the carrier pipe plain byte stream buffer size is limited, and other shortcomings.

 

Signal (sinal):

Signal is a more sophisticated means of communication, an event used to notify the receiving process has occurred.

 

Shared memory (shared memory):

Shared memory is memory-mapped some can be accessed by other processes, this shared memory created by a process, but can be accessed by multiple processes. IPC shared memory is the fastest way, it is for the other inter-process communication running low efficiency specifically designed. It is often associated with other communication mechanisms, such as two signals, used in conjunction to achieve synchronization and communication between processes.

 

Socket (socket):

Solutions sleeve port is an inter-process communication mechanism, with various other communication mechanism is that it can be used for communication between processes on different machines.

 

Ipc operating system-related commands

 

  • ipcs: View ipc information
  • ipcs -q: View the message queue
  • ipcs -m: view shared memory
  • ipcs -s: View semaphore
  • ipcrm: delete ipc
  • ipcrm -q msqid delete the specified message queue
  • ipcrm -m msqid delete the specified shared memory
  • Semaphore ipcrm -s msqid delete the specified
Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103729312