2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
There needs to be coordination between processes. For example, the various management levels in a school are interconnected, and cannot be managed vertically. It is precisely because processes need to collaborate, and the prerequisite for collaboration is that processes need to communicate. Data is classified, some data is ready for notification, some data is simply transmitted data, and some is control-related data.
Fact: The process is independent, the process = kernel data structure process code and data
Purpose of process communication:
The cost of communication between processes may be slightly higher: processes are independent, and resources opened by any process cannot be seen by another process. When we talked about parent-child processes before, the data of the parent process was inherited by the child process, which does not belong to communication. It can be inherited but not always inherited. There is a difference between being able to transmit information and being able to transmit information all the time.
The premise of inter-process communication is to let different processes see the same (operating system) resource ("a piece of memory"). The two processes are independent. To achieve communication, a tool, namely the operating system, is needed to make the two processes have the same piece of memory. The reason why the operating system does this is determined by the user.
How to let the operating system create resources:
A pipeline is essentially a memory-level file that does not need to be flushed to disk.
First, the parent process opens a file twice in read-write mode. The reason for opening it twice is to obtain two struct file objects. In this way, there are two read and write pointers for a file, so that the read and write operations use their own independent pointers, so that the read and write operations will not affect each other. The read and write pointers record the current file reading or writing position.struct file
There is only one read-write pointer in the file. When writing (or reading) to the file, the read-write pointer will move, and then read (write) again. At this time, the read-write pointer is no longer in the original position, and the content just written cannot be read. Therefore, it is necessary to open the same file twice in different ways. Then create a child process. The child process will inherit the file opened in the parent process, that is, inherit the file descriptor table of the parent process. At this time, the parent and child processes will share the same file resources. The child process can write to the file through file descriptor No. 4, and the parent process can read from the file through file descriptor No. 3. At this time, the parent and child processes have realized data transmission, that is, communication. The parent and child processes see the same memory buffer, which we call a pipe file here. Pipes only allow one-way communication because it is simple.
Why do the parent and child processes print data to the same display terminal?
Because the corresponding child process will inherit the file descriptor table corresponding to the parent process, and then point to the same file, which means that the parent process writes to a file, the child process will also write to a file, and both will be written to the same buffer, and the operating system will refresh to the same display.
The process will open three standard input and standard output by default: 0, 1, 2... How to open 0, 1, 2 by default?
All the commands arebash
As long as the child processbash
Once it is turned on, all default subprocesses are turned on.
Why does the child process take the initiative to clos(0/1/2) without affecting the parent process's continued use of the display file?
Memory-level reference counting--
, when the memory-level reference count drops to 0, the file resources are released.
The parent and child processes close unneeded file descriptors, so why did they need to be opened before?
In order to allow the child process to inherit, it is not necessary to close it, but it is recommended to close it to prevent accidental writing.
Why are pipes one-way communication?
The method is simple and reduces development costs. It only allows one-way communication. Any file is refreshed into the buffer, and then the data is refreshed into the file. This process itself is one-way.
The simple pipes we see in life are all one-way, such as water pipes, which have one inlet and one outlet, which conforms to the characteristics of pipes.
can usepipe
To create an unnamed pipe, the parameters do not require a file path and file name
int pipe(int pipefd[2]);