Multi-task communication and synchronization in Linux-Overview

Source: Internet
Author: User

Embedded Development Exchange Group 280352802, welcome to join!

We have learned two ways to implement multiple tasks: process and thread. because the process is working in an independent memory space, different processes cannot directly access the memory space of the other side.CommunicationWhile the threads in the same process share the memory space, it is easy to achieve data sharing, but the multi-thread access to the same memory address needs to be strictly controlled, so some way is required.Synchronization.

In linux, there are some multi-task communication and Synchronization Methods. Some methods can achieve both communication and synchronization, and some methods can not only be used by threads, therefore, we will put these things together for explanation. These methods mainly include:

Pipe and named pipe): Pipelines can be used for communications between kinship-related processes. A famous Pipeline overcomes the pipe's no-name restriction. Therefore, in addition to the pipeline's functions, it also allows communication between unrelated processes.
(2)Signal (Signal ):A signal is a simulation of the interrupt mechanism at the software level. It is a complex communication method used to notify a process of an event, except for inter-process communication, A process can also send signals to the process itself. A process receives a signal and the processor receives an interrupt request, which has the same effect. in addition to the early Unix signal semantic function sigal, linux also supports the Posix-compliant signal function sigaction (in fact, this function is based on BSD, BSD in order to achieve reliable signal mechanism, it can also unify external interfaces and implement the signal function again using the sigaction function ).
(3)Messge Queue ):A message queue is a chain table of messages, including Posix Message Queue SystemV message queue. it overcomes the disadvantages of limited information in the first two communication modes. A process with write permission can add new messages to the Message Queue according to certain rules; processes that have read permission on the Message Queue can read messages from the message queue. the message queue overcomes the disadvantages of low signal carrying information, the pipeline can only carry unformatted byte streams, and the limited buffer size.
(4)Shared memory (Shared memory ):It can be said that this is the most effective way of inter-process communication ,. it allows multiple processes to access the same memory space. Different processes can view updates to data in the shared memory in a timely manner. this communication mode depends on a synchronization mechanism, such as mutex lock and semaphore.

Socket ):
(6)Semaphores (Semaphore ):Synchronization and mutex between processes and different threads of the same process.

Mutex ):The mutex lock is mainly used for thread synchronization. It can lock shared resources. Any other thread attempting to lock the mutex will be blocked until the current thread releases the mutex lock, ensure that only one thread can access Shared resources at a time.

In the preceding seven methods, the first five are mainly used for multi-task communication, and the last two are mainly used for multi-task synchronization.

Related Article

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.