In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Linux system process communication is the main way, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
At-a-glance common ways of interprocess communication are as follows:
Pipeline
FIFO
Message queue
Semaphore
Shared memory
UNXI domain socket
Socket (Socket)
Pipeline is an ancient form of IPC communication. It has two characteristics:
Half-duplex, that is, data cannot be transmitted in both directions at the same time. Some systems may support full duplex.
Only between parent and child processes. The classic form is that the pipe is created by the parent process, and after the process forks the child process, it can be used between the parent and child processes.
Using a combination of the popen function and the pclose function to execute system commands, pipes are used, which are declared as follows:
FILE * popen (const char * command,const char * type); int pclose (FILE * stream); although the system () function can also execute system commands, it cannot obtain the execution status code, and the execution of system commands essentially requires the creation of child processes, so the output of child processes can be easily obtained by using pipes. This article is not carried out in detail.
Let's look at a simple example of using a pipe, where the pipe function is used to create the pipe:
# include # define MAX_LEN 128int main (void) {/ * 0 is read, 1 is write * / int fd [2] = {0}; / / descriptor pid_t pid = 0; char line [Max _ LEN] = {0}; int n = 0 / * to create a pipe, you need to pass in two file descriptors * / if (pipe (fd) 0) {/ * close the write descriptor of the pipe * / close (fd [1]); / * read data from the pipe * / n = read (fd [0], line,MAX_LEN); printf ("read% d bytes from pipe:% s\ n", nline) } / * Child process * / else {/ * closes the pipe read descriptor * / close (fd [0]); / * writes data to the pipe * / write (fd [1], "www.yanbinghu.com", sizeof ("www.yanbinghu.com"));} return 0 } in the program, we create a pipe in which the parent process closes the write channel and the child process closes the read channel The child process writes a string into the pipe, while the parent process reads the string from the pipe and outputs it.
Running result:
Read 18 bytes from pipe: www.yanbinghu.comFIFOFIFO, also known as named pipes, unlike pipes, unrelated processes can also exchange data.
The main functions involved in FIFO operations are:
Int mkfifo (const char * path, mode_t mode); and FIFO often has the following two uses:
No need to create intermediate temporary files, copy the output stream
In multi-client-service process applications, FIFO is used as an aggregation point to transfer data between client processes and service processes.
Let's look at a simple example and write the process code as follows:
# include # define FIFO "/ tmp/fifo" # define MAX_LEN 128 int main (void) {int writeFd; char line [Max _ LEN] = {0}; if (mkfifo (FIFO,S_IRUSR | S_IWUSR) it first creates a FIFO, writes a string into it, and then closes the exit. The read process code is as follows: # include # define FIFO "/ tmp/fifo" # define MAX_LEN 128 int main (void) {int readFd,n; char line [Max _ LEN] = {0}; / * Open FIFO, where it may fail, and the return value should be processed * / readFd = open (FIFO,O_RDONLY,0) / * read data from FIFO * / n = read (readFd,line,MAX_LEN); printf ("read% d bytes from pipe:% s\ n", njinlin); close (readFd); / * delete FIFO*/ unlink (FIFO); return 0;} it first opens a known FIFO and then reads the data from FIFO. First run the write process on a terminal, and then run the read process.
Two unrelated processes can communicate through FIFO. Message queuing can be thought of as a message linked list, stored in the kernel from which the process can read and write data. Unlike pipes and FIFO, a process can write without another process waiting to read. On the other hand, once the pipeline and FIFO are closed and exited, the data in it is gone, but for message queuing, one process writes data to the message queue and exits, while the other process can still open and read the message. Message queuing does not have much speed advantage over the UNIX domain sockets described later. A semaphore is a counter that is mainly used when multiple processes need to access shared data.
Considering this situation, you can't have two processes accessing the same data at the same time, so you can do this with the help of semaphores. Its main flow is as follows: check the semaphore that controls the resource if the semaphore value is greater than 0, the resource is available, and subtract it by 1, indicating that the process is currently used if the semaphore value is 0, then the process hibernates until the signal value is greater than 0, that is to say, it actually provides a means to access synchronization between different processes or different threads of a process. Shared memory allows multiple processes to share a given storage area, which is very fast because they share a piece of memory data. However, it is necessary to provide additional means to ensure synchronous access to shared memory, for example, it can use the semaphores mentioned earlier to achieve access synchronization. UNIX domain sockets UNIX domain sockets are similar to sockets, but it is more efficient because it does not need to perform protocol processing, such as calculating checksums, sending acknowledgements, and so on, it just copies data. Of course, it only applies to interprocess communication on the same computer. For example, after the redis service configuration unixsocket starts, you can specify a UNIX domain socket to connect to the redis server through the-s parameter of redis-cli. $redis-cli-s / tmp/redis.sock
Redis / tmp/redis.sock > it will be faster than using network sockets.
Needless to say, network sockets use the network to communicate. Unlike the communication methods mentioned earlier, it can be used for inter-process communication between different computers. Summary this article briefly introduces the common ways of interprocess communication, in which we use an example of pipes and named pipes, because we may see it often. For FIFO, when the last process referencing it terminates, the data left in FIFO will also be deleted, while for message queuing, it will remain until it is displayed for deletion or bootstrapped by the system, and message queuing has no special advantage over other methods. Semaphores are actually often used for synchronous access to shared data. Shared memory is very efficient to transfer data between processes, but the system does not synchronize access, so it is necessary to synchronize data access. Socket (socket) is the most widely used way of inter-process communication.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.