Learning tips about interprocess communication

  
 

Process: Process refers to the instruction sequence of the independent address space

The five states of the process: New, Ready, Run, Sleep, Zombie

Interprocess communication: is between different processes Do some "contact", this kind of contact is simple and complicated. The mechanism is different and the complexity is different. Communication is a broad sense, not only refers to the transmission of large quantities of data, but also the transmission of control information, but the method of use is basically the same.

Basic Process Communication Mechanisms

1. Traditional UNIX-IPC Mechanisms: Signals and Pipes

2.SystemV's IPC Mechanism: Shared Memory, Semaphores, and Message Queues< Br>

3. Sockets from Unix BSD versions

4. Remote Procedure Call (RPC)

Signal: The oldest process used in Unix systems One of the methods of inter-communication, which is used to send a signal of an asynchronous event to one or more processes. Signals can be analogized to INT under DOS or to events under Windows
. When a signal occurs, the corresponding signal is sent to the corresponding process.

Implementation of Signal Mechanisms

1. Signals include signals to be processed and blocked signals

2. If a blocked signal is generated, it remains pending, Until it is unblocked.

3. The system saves how each process handles every possible signal.

4. The system judges whether the process wants to ignore this signal or let the kernel process it. The process changes the default signal processing by executing a system call.

Processing the signal

1. Initializing the signal set, only the signal in the signal set will be considered

2. Install the signal processor. The so-called signal processor is to specify some methods for processing the signal. When installing, be sure to give the correct signal processing function to a specific signal.

Signal related functions

int sigaction(int signo, const struct sigaction *act, struct sigaction *oact); Install signal processor for process, struct sigaction data structure is used to save signal processing Information about the device.

int sigemptyset(sigset_t *set); Clear the signal set.

int sigfillset(sigset_t *set); Set the signal set to contain all the signals, and must initialize the signal set before operating the signal.

int sigaddset(sigset_t *set, int signo); Add a new signal corresponding to signo to the signal set.

int sigdelset(sigset_t *set, int signo); removes a signal corresponding to signo from the signal set.

int sigismember(const sigset_t *set, int signo); Determine if a signal is in the signal set.

int sigprocmask(int how,const sigset_t *set, sigset_t *oset); Set the signal mask of the process. The signal mask can be used to block signals in some signal sets for a certain period of time.

Pipeline Communication: It is the oldest Unix IPC tool. A process writes data from one end of the pipeline, and another process reads data from the other end of the pipeline to implement the sharing of communication between them, also known as a pipe file. Since both transmission and reception are communicated using pipes, they are called pipe communication. The communication method is one-way. Pipe types are divided into: unnamed pipes, named pipes

pipe communication ideas

1. The sending process can continuously write data streams from the pipe end, the maximum length of the specified pipe file Within the range (eg, 4096 bytes), the length of information written per write is variable.

2. The receiving process can read data from the other end of the pipe when needed, and the read unit length is also variable.

Basic pipe call function

int do_pipe(int *fd);Create pipe

static int pipe_release(struct inode *inode, int decr, int decw); pipe release

Unknown Pipeline II

shows that each file data structure contains pointers to the vector table of different file manipulation routines: one for writing and one for reading from the pipeline. This masks the difference from the general system call for reading and writing ordinary files. When the write process writes to the pipeline, the bytes are copied to the shared data page, and when read from the pipeline, the bytes are copied from the shared page.

Named Pipeline: Also known as FIFO, it is not a temporary object, but an entity in the file system, which can be created with the mkfifo command. The system must process the process of opening the FIFO read before the write process opens the FIFO, and the process that is read before the write process writes the data. It uses the same data structures and operations as an anonymous pipe.


(Write side)[Fd1]
→pipe(fd)→[Fd0](readout)

Semaphore : Semaphore and signal are different things, the signal is a fixed value that implements the convention, and the semaphore is a variable that records certain information. Its use is mainly used to protect shared resources, so that the resource is Only one process is owned by one process at a time.

Data Structure of Semaphores: Use the semid_ds data structure to express semaphores. All semid_ds data structures in the system are pointed to by the semary pointer vector table. Each semaphore array has sem_nsems, which is described by a sem data structure pointed to by sem_base.

Implementation of semaphore mechanism

1. There are only two operations on semaphores: P, V .

2. In order to logically organize semaphores, one concept in the semaphore mechanism is the semaphore group. We create related semaphores in a semaphore group so that they are logically clear and easy to manage.

3. They also need to be initialized before use: generate or open a semaphore group to generate or delete the specified semaphore.

4. A semaphore must belong to a semaphore group, otherwise it cannot be used by the system.

5. The semaphore and semaphore groups are not automatically cleaned up by the system, so it is necessary to clean up the generated semaphores in time before the process exits.

Semaphore related functions

int semget(key_t key, int nsems, int semflg); Create a new semaphore group or get an existing semaphore group.

int semop(int semid, struct sembuf *sop, int nsops); operates on one or more semaphores at a time for P and V operations.

Int semctl(int sem_id, int semnum, int cmd, union semun arg); used to get some semaphore usage information or to control the semaphore.

Common Memory

Shared memory is an important method of process communication, providing a means of direct communication for processes. Operating system

One or more processes communicate through memory in their virtual address space, which is referenced by the page table of each shared process, their addresses No need to be the same. Process access to shared memory is controlled, semaphores and other mechanisms implement synchronization of shared memory access

Simple principle of shared memory

Each newly created memory area uses a shmid_ds Data structure to express. These data structures are stored in the shm_segs vector table. The Shmid_ds data structure describes how large, how many processes are using it, and how shared memory maps to their address space. The creator of shared memory controls access to this memory and its keys are public or private. It can also lock shared memory in physical memory if it has sufficient permissions. Every process that wants to share this memory must be attached to virtual memory through a system call. This creates a new vm_area_struct data structure for the process that describes this shared memory. The process can choose to share the location of the memory in its virtual address space or select a sufficient free area by Linux. This new vm_area_struct structure is placed in the list of vm_area_struct pointed to by shmid_ds. Connect them together via vm_next_shared and vm_prev_shared.

Copyright © Windows knowledge All Rights Reserved