Inter-process communication IPC: Inter-processcommunication (four types are described here)

Source: Internet
Author: User
Inter-process communication IPC: Inter-processcommunication (four types described here) I. pipelines 1. pipelines (with kinship) and famous pipelines (without kinship), signal, message queue, shared memory, Semaphore, socket. Unknown pipe 2. the pipe is congested. when the pipe's read end exists... inter-process communication IPC: Inter-process communication (four types described here) I. pipeline 1. pipelines (with kinship) and famous pipelines (without kinship), signal, message queue, shared memory, Semaphore, socket. ===== Unknown MPs queue 2. MPs queue: the MPs queue is blocked. it is meaningful to write data to the MPs queue when the read end of the MPs queue exists. It has fixed read and write ends. 3. when a pipeline creates pipe (fds), it creates two file descriptors fds [0] and fds [1]. Close (fds [0]); close (fds [1]); 4. Generally, two pipelines are created first and then fork. Fds [0] is fixed for reading pipelines, while fd [1] is fixed for writing pipelines with parent-child relationship. To read and write data between parent and child processes, you only need to disable the file descriptor of the unrelated read or write end. Read and write. Www.2cto.com 5. wait until the child process ends and the parent process ends. Prevents zombie processes and no one collects them. 6. note: 1. it is meaningful to write data to the MPs queue only when the read end of the MPs queue exists. 2. when writing data to an MPs queue, linux does not guarantee the atomicity of writing data. as soon as the buffer zone of the MPs queue is idle, the write process tries to write data to the MPs queue. If the read process does not read data from the buffer area of the MPs queue, the write operation will be blocked all the time. 3. the sequence of parent and child processes is not guaranteed during running. therefore, to ensure that the parent process has disabled the read descriptor, you can call the sleep function in the child process. ======== Standard streaming pipeline 1. standard streaming pipeline: used to create a pipeline to connect to another process. Therefore, it does not belong to a famous or unknown pipeline. 2. popen and "r" only use the result as the output, but there is no output. The file descriptor of the MPs queue is returned. "W" seems unavailable-lai. = Corresponding to pclose (fd); ======= famous pipeline (equivalent to connecting the created file as a pipeline and then reading and writing it like a common file. Files, that is, pipelines can be identified by path names, which are visible in the file system.) 1. famous pipelines: two processes that are irrelevant to each other can communicate with each other. (Mkfifo) the MPs queue can be identified by the path name or by the mknod MPs queue name p. 2. normal file read/write is not blocked, but the MPs queue read/write is blocked. Non-blocking mark: O_NONBLOCK.3. if the file already exists, it can also be successful, but the "cross-read-write" phenomenon may occur, so avoid it as much as possible. 4. but the above test is still a problem. it is best to delete it after use, otherwise, the second compilation won't take place = Lai 5, # define export o_server "/linux_basic_study/pro_communication/myfifo" 6, // The file will exist once it is created, you can open it directly later. // You can see that the pipeline file starting with P is the global variable defined by the system in/linux_basic_study/myfifo // errno. when a function exception occurs, the errno variable (including errno is required) is usually used. h) assign an integer. www.2cto.com // different values indicate different meanings. you can view the cause of the error by checking the value. 7. // when blocking a read, it is required to open each read and block it only for the first read. Otherwise, after the first blockage, the backend will not be blocked. // When the open file is not blocked, it must be placed outside the while file; otherwise, data cannot be received. 8. unlink (FIFO); // delete the file. Remember: make sure there are no files in it. Corresponding to Article 4. ========================================================== ========================================= 2. Signal Communication 1. signals are a simulation of the interrupt mechanism at the software level and an asynchronous communication method. The signal can directly interact between the user space process and the kernel process. Equivalent to our interrupt processing function 2. three important phases of the signal life cycle ==> four screens: signal generation (kernel), signal registration in the process (user process), and signal cancellation in the process (user process) and execute the signal processing function. 3. 32 unreliable signals (first). if the same signal has been registered, ignore it. The reliable signal will be registered again. 4. note: the generation, registration, and cancellation of signals refer to the internal implementation mechanism of signals, rather than the function implementation of signals. 5. three types of => ignore signal (SIGKILL and SIGSTOP cannot be ignored): do not process the signal; capture signal: when the signal occurs, execute the corresponding processing function; execute the default operation: linux specifies the default operation for each signal. 6. important six signals: SIGINT => (CTRL + C) f issued; SIGQUIT => (CTRL + \) sigkill sigalarm sigstop sigchld => (CTRL + Z) 7. an Alarm () process can only have one Alarm time. After the time is reached, the SIGALRM signal is sent to automatically terminate program execution. 8. Pause () is used to suspend the calling process until the signal is captured. 9. Raise () is similar to the kill function that allows a process and can only send signals to itself. 10. Kill () can not only abort a process (actually sending a SIGKILL signal), but also send other signals to the process. 11. There are two main methods for signal processing. one is to use the simple signal function eg: signal (SIGINT, my_func); and the other is to use a signal set function Group. 12. if the parent process is 1, it is an orphan process and is out of the console. Kill fails. SIGSTOP is generated .. The orphan process can only be disabled after the system is restarted. Therefore, it is best to send the KILL signal instead of the stop (3) shared memory (create a shared memory, which can be mapped to its own memory through the synchronization mechanism .) There is a problem with the use of shared memory: if it is in a loop, it will read the read content cyclically. You can also use the message queue mentioned later to solve this problem. 1. shared memory is the most efficient way to communicate between processes. ==> Non-blocking... You can delete the file once. an error occurs when you delete the file twice. 2. map the memory to be accessed to your own private address space. Therefore, it is equivalent to accessing your own memory. 3. step: 1. create shared memory: it is best to use ipc_creat (no e)... otherwise, if the memory of this key is not created, the file will not be found and an error will be reported. The returned value is the shared memory area identifier shmid = shmget (key_t) 100, BUFSZ, 0666 | IPC_CREAT). 2. it is mapped to the shared memory. The returned result is the pointer shmadd = shmat (shmid, 0, 0) mapped to the specified location. 3. read or write if (! Strncmp (shm_ptr, "end", 3); if (! Strcmp (shm_ptr, "end") // No, don't use it. you can do it for the first time. 4. undo the mapped memory shmdt (shmadd). 5. delete the shared memory. remember: you can delete the read or write data, or an error will be reported. 6. during input, use fgets (temp, 1024, stdin); keyboard input, standard stdin; scanf ("% s", temp); // no space in the middle, it ends with a space or press Enter. So do not use. IV. Message Queue 1. message queue has certain FIFO features, but it can implement random query of messages, which has a greater advantage than FIFO. 2. it is also a sending and receiving service. Two methods: one uses a struct and the other does not (actually required in the project ). 3. step: 1. create or open a message queue. the Message Queue IDmsg_id = msgget (key_t) 1234,066 6 | IPC_CREAT) is returned. 2. read the message: msgrcv (msg_id, buf, BUFSIZ, 0, 0) 2. Send and add a message: msgsnd (message_id, buf, BUFSZ, 0) 3. control the message queue: undo or delete. Msgctl (message_id, IPC_RMID, 0) 4. Use a struct to send data. However, only strings are sent in the past, while message_type is only a flag for receiving information. So we can use another one.

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.