Inter-process communication (IPC) Learning

Source: Internet
Author: User

 

1. Communication Methods Between processes in Linux: signals, pipelines and named pipelines, message queues share Primary memory, semaphores, sockets 3. Signal communication 3.1. In a Linux environment, you can run the "kill-l" command to obtain the signal list supported by Linux. 3.2. Signal lifecycle: signal Generation, signal registration in process, signal cancellation in process, signal processing function execution completed 3.3, signal processing function a), signal INSTALLATION function: # clude <signal. h>, mainly including signal (), sigaction () function B), and signal sending function: the five most commonly used signal sending functions are: kill (), raise (), alarm (), settimer (), pause (), must contain the header file <signal. h> and <sys/types. h> c), signal operation function: int sigemptyset (sigset-t * set); // clear int sigfillset (sigset_t * set) in the signal set ); // set the complete set containing all signals in T sigaddset (sigset_t * set, int signo); // Add a signal to the signal set int sigdelset (sigset_t * set, int signo ); // delete a signal from the set int sigismember (const sigset_t * set, int signo); // judge whether the signal is contained in the given set int sigprocmask (int how, const sigset_t * set, sigset_t * oset); // sets the process interrupt shielding code how = [SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK], * oset backs up the pre-configured shielding Code 4. Pipeline communication: 4.1. Overview: pipelines transmit data first and foremost through the kernel buffer. pipelines can be treated as one-way connectors connecting two commands or applications. 4.1 pipeline communication is divided into anonymous pipeline (pipe) and named pipeline (FIFO ). 4.2 limitations of the anonymous pipeline: a. Only one-way data streams are supported. B. It can only be used for Kinship-related processes. c. It has no name. d. the buffer zone is limited. The size is a page. # Include <unistd. h> int pipe (int fd [2]) 4.3. Named pipe: # include <sys/types. h> # include <sys/stat. h> int mkfifo (const char * pathname, mode_t mode) 5. Message Queue 5.1. Basic principles of Message Queue: FLAG (msgtyp> 0) of the child process for sending the first registration) and the child process number to the server process server registration, in the server segment using msgrcv (Q_MSG_KEY, & recv_buf, sizeof (Message)-sizeof (long), FLAG, 0) receive, the Message body does not contain the flag of the Message header. Then, the server sends the server process number, and receives the Message marked as the sub-process number to the sub-process to receive the previous Message. The message queue communicates with each other through the message mark (process number). If either of the Client/Server Processes exits, messages may be lost. That is, the message marked by the process Number of the exiting party will not be received by any process, because other process numbers do not match the message mark. General process: establish connection start data communication reply feedback information send Registration Information Server Client Message Queue communication 5.2 Message Queue basic operations: these operations are included in the following three header files: # include <sys/types. h> # include <sys/ipc. h> # include <sys/msg. h> a) open or create a message queue: int msgget (key_t key, int flag) B), read/write operation: int msgsnd (int msqid, void * msgp, size_t msgsz, int msgflg); int msgrcv (int msqid, void * msgp, size_t msgsz, long int msgtyp, int msgflg); // receives message c), Message Queue attribute operations: int msgctl (int msqid, int cmd, struct msqid _ Ds * buf); 6. semaphore 6.1 and semaphore basic operations: the file must contain the following three headers: # include <sys/types. h> # include <sys/ipc. h> # include <sys/sem. h> a), enable or create a semaphore: int semget (key_t key, int nsems, int flag); B), signal value operation: int semop (int semid, struct sembuf * sops, unsigned nsops); can be used to increase or decrease the signal value. C) semaphore attribute operation: int semctl (int sem_id, int semnum, int cmd); 7. Shared primary memory 7.1. Overview: shared memory is the physical memory space shared by multiple processes at one end, by ing a physical memory address to a different virtual space, Message Queue replicates data from the application buffer to the core buffer. Therefore, the communication efficiency of shared memory is higher than that of message queues, but there is a complicated synchronization mutex. 7.2 basic operations: int shmget (key_t key, int size, int shmflg); // create or obtain a shared memory int shmctl (int shmid, int cmd, struct shmid_ds * buf); // void * shmat (int shmid, const void * shmaddr, int shmflg) for shared memory operations )); // get the pointer to the shared memory int shmdt (const void * shmaddr); // detach/Delete the shared memory block from the process

This article is from the "Change" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.