Operating System-process/thread internal communication-Race 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, in the next series of articles, we will introduce the communication between processes as a template. The main content of this article is: Introduction to inter-process communication (IPC) competition Conditions between processes (Race Conditions) 1. InterProcess Communication (IPC ). Inter-process communication mainly involves three aspects: how can one process transmit information to another process to ensure that multiple processes do not overlap? For example, two processes simultaneously order the last train ticket, how can we process the execution sequence of A process? For example, IF process A generates data and process B prints the data generated by process A, process A must run first, B can run 2. Race Conditions between processes, 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. Then, the CPU switches to Process A again, the recovered Process (thread) will continue to run from the original state. For Process A, In or 6, Process A also writes its own files to the 6th location, and change 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.