A process is an independent resource allocation unit. Different processes are independent of each other.
The inter-process communication mechanism supported by Linux:
(1) Data interaction mechanism between processes on the same host: Unknown pipe, famous pipe (FIFO), Message Queue (Message
Queue) and shared memory (share memory ).
(2) synchronization mechanism between processes on the same host: semaphore)
(3) asynchronous mechanism between processes on the same host: Signal)
(4) Data Interaction between network hosts: socket ).
1. Unknown pipeline (PIPE ):
Normal Linux shells allow redirection, and redirection uses pipelines.
Int pipe (int fd [2]);
MPs queue Creation
Note: FD [0] is used to read the MPs queue, and FD [1] is used to write data to the MPs queue. MPs queues are mainly used for communication between different processes. In fact, a pipeline is usually created first, and then a sub-process is created through the fork function.
2. Famous pipelines (FIFO)
The Nameless pipeline is temporary and will automatically disappear after communication is completed, and communication can only be performed between unrelated processes. A famous pipeline can communicate with each other in different processes.
Int mkfifo (const char * pathname, mode_t mode );
Note: When using a famous Pipeline, you must use two processes to open the reading and writing ends respectively.
3. signal ):
The signal is software interruption. The signal mechanism is one of the oldest processes in UNIX systems. It is used to transmit asynchronous signals between one or more processes.
1,
Signal sending and capturing
Kill () and raise ()
Kill () can not only stop a process, but also send other signals to the process.
Unlike the kill function, the raise () function sends signals to the process itself.
# Include <sys/types. h>
# Include <signal. h>
Int kill (pid_t PID, int signo );
Int raise (INT signo );
2,
Signal Processing
Void (* signal (INT signo, void (* func) (INT)
The first parameter signo is the received signal, and the second parameter is the processing code entry after the signal is received (that is, the processing subroutine)
Iv. System V
Inter-process communication
The IPC Mechanism provided by System V mainly includes three mechanisms: Message Queue, semaphore, and shared memory. Like files in Linux, System V has its own read/write attributes, however, its read/write operations cannot use the read/write method of common files. Any IPC communication object uses a unique ID to identify itself, so that other processes can access it through this ID value to achieve information interaction.
1,
Message Queue
It is mainly used to transmit a small amount of data between two processes, and the receiver can selectively receive messages based on the type of messages in the message queue.
Key_t ftok (char * pathname, char proj );
It returns a key value corresponding to the path pathname.
Int msgget (key_t key, int msgflg)
A new message queue will be created
Int msgrcv (INT msqid, struct msgbuf * msgp, int msgsz, long msgtyp, int msgflg );
The system calls to read a message from the Message Queue represented by msgid, and store the message in the msgbuf structure pointed by msgp.
Int msgsnd (INT msqid, struct msgbuf * msgp, int msgsz, int msgflg );
Send a message to the Message Queue represented by msgid. The message to be sent is stored in the msgbuf structure pointed to by msgp. The message size is specified by msgze.
Int msgctl (INT msqid, int cmd, struct msqid_ds * BUF );
The system calls the CMD operation on the message queue identified by msqid. There are three kinds of CMD operations: ipc_stat, ipc_set
And ipc_rmid.
2,
Semaphore Communication Mechanism
The semaphore communication mechanism is mainly used to synchronize processes, and the signal value is used to identify the number of available resources of the system.
Int semget (key_t key, int num_sems, int sem_flags );
Create a new semaphore or obtain an existing semaphore key value.
Int semctl (INT sem_id, int sem_num, int command ,...);
Allow direct control of semaphore information:
Int semop (INT sem_id, struct sembuf * sem_ops, size_t num_sem_ops );
Change the semaphore Value
The first parameter, sem_id, is the semaphore identifier returned by the semget function. The second parameter, sem_ops, is a pointer to a structure array. Each structure contains at least the following members:
Struct sembuf {
Short sem_num;
Short sem_op;
Short sem_flg;
}
3,
Shared Memory
It is mainly used for massive data transmission between processes. When using shared memory for data access, it is generally used with binary semaphores.
Int
Shmget (key_t key, size_t size, int Oflag); create and operate the shared memory zone:
Int shmctl (INT shmid, int cmd, struct shmid_ds * BUF );
Shared Memory Control Functions
Char * shmat (INT shmid, void * shmaddr, int shmflag );
The shared memory area objects are mapped to the address space of the calling process.
Int shmdt (char * shmaddr );
Out of shared memory block