Operating System-process/thread internal communication-Race Conditions, system-conditions
Starting from this article, we will introduce inter-process communication. Problems Encountered during inter-process communication and their methods are actually the same as those between threads. Therefore, all theoretical knowledge of inter-process communication can be used on threads, the next series of articles will introduce the communication between processes as templates. The main content of this article is as follows:
- Introduction to inter-process communication (IPC)
- Race Conditions)
1. Introduction to inter-process communication
InterProcess Communication (IPC ). Inter-process communication mainly involves three aspects:
- How does one process transmit information to another process?
- Make sure that multiple processes do not overlap. For example, if two processes order the last train ticket at the same time, what should they do?
- Process execution sequence. For example, if process A generates data and process B prints data generated by process A, process A must run before process B can run.
Ii. Race Conditions)
As shown in.
Spooler Directory is the cache delay of a printer. When a process wants to print a file, it writes the file name to the cache pool. There is a dedicated printer that regularly reads this cache pool. If there is a file, print it,
Out and In are two shared variables used to identify the location of the next file to be printed and the location of the next file to be written, because only one printer reads the Spooler Directory, so no one will compete for the value of Out. Next we will mainly talk about the variable In.
- Because multiple processes write files to the Spooler Directory, assume that the In value is 6 when the ProcessA writes data. After the ProcessA reads the IN, it first stores its own local variables, at this time, the CPU considers that PRocessA has been running for a period of time and intends to give the CPU to Process B just started.
- At this time, because Process A has just read In and has not written the file, the In value is still 6, so Process B also obtains 6 in, then Process B writes the file to 6th locations, and changes the value of In ++ to 7.
- Next, the CPU is switched to Process A again. Previously, after switching the CPU, the recovered Process (thread) will continue to run from the original state. For Process, in or 6, Process A also writes its own file to the 6th location, and changes the value of In to 7 after ++.
Problem: the file of Process B is overwritten by Process A during the second operation. Process B files are not printed.
The above problem is the competition condition between processes: multiple processes read, write, and share data. The final result depends on the running time of the process.
The above problem is obvious. Changing a process to a thread also produces the same problem.