1. Inter- process Communication
inter-process communication mainly includes pipelines, system IPC (including message queue, semaphore, shared storage), SOCKET.
System IPC
Anonymous pipe (pipe): An anonymous pipe is a half-duplex method of communication that is typically used between parent and child processes.
: A named pipe is also a half-duplex communication method, but it allows non-affinity interprocess communication.
: Semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent a process from accessing the shared resource while other processes are accessing the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.
: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, only unformatted byte stream, and limited buffer size.
: A signal is a more complex form of communication that notifies the receiving process that an event has occurred.
: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
: Sockets are also an inter-process communication mechanism, unlike other communication mechanisms, which can be used for process communication between different processes.
faq1: pipeline with file descriptor, the relationship of the file pointer? A: In fact, the use of pipelines similar to files, can use Read,write,open and other ordinary IO functions. A pipe descriptor is similar to a file descriptor. In fact, the descriptor used by the pipeline, the file pointer, and the file descriptor will eventually be translated into the system's socket descriptor. are limited by the socket descriptor in the system kernel. Essentially, the Linux kernel source pipeline is implemented by an empty file. FAQ2: How to use the pipe? A: There are several methods: 1) pipe, create a pipe, and return 2 pipe descriptors. Typically used for communication between parent-child processes. 2) Popen, Pclose: This method returns only one pipe descriptor, which is often used for communication the other party is stdin or stdout; 3) Mkpipe: Named pipes that interact with many processes. FAQ3: What is the comparison between pipeline and system IPC? A: The advantage is that all UNIX implementations are supported, and the pipeline is completely removed after the last access pipeline's process terminates, and the flaw is that the pipeline allows only one-way transmission or between parent and child processes. System IPC: The advantages are powerful, able to communicate between unrelated processes; The flaw is that the keyword key_t uses kernel identities, consumes kernel resources, and can only be explicitly deleted, and cannot use some of the socket's mechanisms, such as select,epoll. Faq4:windos the relationship between interprocess communication and Linux interprocess communication? A: In fact, most of WinDOS's process communications are ported to UNIX, WinDOS's clipboard, file mappings, and so on, all of which can be found in shared storage for UNIX process communication. FAQ5: What is the relationship between interprocess communication and inter-thread communication? A: Because Windows runs on a thread, interprocess communication in the narrow sense refers to the communication between threads belonging to different processes. The thread synchronization problem between individual processes can be merged into a special process communication. It uses the kernel-supported system calls to keep threads synchronized. Some common thread synchronization methods are: Event, Mutex, Semaphore semaphore, critical area resource, and so on.
2. Write and read about the socket
Baidu interview A question: If write the data of the byte, then read how much?
First Look at these two function prototypes:
Write function
ssize_t write (int fd,const void*buf,size_t nbytes);
Write The function writes the contents of nbytes bytes in buf to the file descriptor, returns the number of bytes written successfully, and returns 1. and sets the errno variable. In a network program, there are two possible ways to describe comfortable writing data to a socket file:
1 The return value of write is greater than 0, which means that some or all of the data is written, so that the data is written continuously with a while loop, but the BUF parameter and the nbytes parameter in the loop are updated by ourselves, that is to say, Network programming in the Write function is not responsible for all the data after the return, perhaps halfway back!
2 , the return value is less than 0, and there is an error, which needs to be handled according to the error type.
If the error is Eintr indicates that an interrupt error occurred while writing, the epipe indicates a problem with the network connection.
Read function
ssize_t Read (int fd,void*buf,size_t nbyte)
Read The function is responsible for reading from the FD , and when read succeeds, read returns the number of bytes actually read, if the return value is 0, indicating that the end of the file has been read, and that less than 0 indicates a read error.
if the error is Eintr indicates that an interrupt error occurred while writing, the epipe indicates a problem with the network connection.
Although it writes a byte, it does not guarantee that read reads 100 bytes at a time, and it does not write 100 to succeed, the number of bytes written and read actually successfully read and write is determined by the return value.
Baidu interview (process communication, socket)