The Semget (), Semctl (), Semop () and its basic experiments of signal traffic
This signal is not easy to understand, ah, I read the book several times before I know what is going on. In this section of the signal before, I still want to say a few small knowledge points, this is my final understanding after learning the "essence", haha!
What does the semaphore do?
Semaphore is a kind of interprocess communication mechanism to solve the problem of synchronization and mutual exclusion between processes.
A popular understanding of synchronization and mutual exclusion
We can literally understand these two nouns. For example, how do you guarantee the order in which the parent-child process executes when you create a subprocess? I did it in the previous time through the sleep () function, for example, I want the subprocess to run first and let the parent process run, so I add a sleep () function to the parent process's program so that the parent process sleeps first, so that the child process can be executed first. Sometimes we don't know in advance which one of the parent process and the subprocess executes first, but to use the sleep () function as I do, I can only guarantee that the child process is executed first, but it does not guarantee that the child process is finished and then executes the parent process, so that's understandable. So if we want the subprocess to run completely and then execute the parent process, we can use the semaphore to solve the synchronization problem between them. Do not understand also does not matter, I will be in the back of the experiment in the actual talk. A more popular example, a food production line, assuming a, b together to complete a food packaging task, A is responsible for the food in the box, B and C is responsible for the box packaging. Must be a first pack food B and then pack it, if B does not follow the rules to pack first, that a also put what, so need a mechanism method to ensure that a first b before, "signal volume" is this mechanism method, the relationship between AB is the synchronization relationship; then suppose that the package uses a knife, and the workshop has a knife, At this point B and C form a mutually exclusive relationship.
The difference between the signal quantity and the signal
To be honest with you, when I first started learning, I understood that "the signal is a lot of signals", of course it is wrong ha! Light from English is not the same: the signal is semaphore, and the signal is Signal. In fact, the signal and signal volume are different. Although they can all achieve synchronization and mutual exclusion, the former is done using a signal processor, which is implemented using the P,V operation. (The PV operation has a speaking behind)
All right, get to the point.
Signal Volume Overview
In a multitasking operating system environment, multiple processes run at the same time, and some processes may have some associations between them. Multiple processes may work together to accomplish the same task, which creates a synchronization relationship between processes. And between the different processes, in order to compete for limited system resources (hardware or software resources) will enter a competitive state, this is the mutex relationship between processes.
The root of the mutual exclusion and synchronization relationship between processes lies in the critical resources. A critical resource is a resource that allows only a limited (usually only one) process to access (read) or modify (write) at the same time. Typically includes hardware resources (processors, memory, storage, and other peripherals) and software resources (shared code snippets, shared structures, variables, and so on). The code that accesses a critical resource is called a critical section, and the critical region itself is called a critical resource.
Semaphores are a interprocess communication mechanism used to resolve synchronization and mutual exclusion between processes, including a variable called semaphores and a process waiting queue to wait for resources under the semaphore, and two atomic operations (P/V Operations) on the semaphore. The signal quantity corresponds to a certain resource and takes a nonnegative shaping value. A semaphore value (commonly used sem_id) refers to the amount of the resource currently available, if equal to 0 means there is currently no available resource.
PV Atomic operation (very important)
The specific definition of PV atomic operation is as follows: (good understanding, very important AH)
P Action: If there are available resources (semaphore >0), the process in which the operation is located consumes a resource (at which point the semaphore value is reduced by 1 and enters the critical area code); If no resources (semaphore value =0) are available, the process in which the operation is located is blocked until the system assigns the resource to the process (into the wait queue, Wait until the resource turns to the process.
V Operation: If there is a process waiting for the resource in the waiting queue for the semaphore, a blocking process is awakened, and if no process waits for it, a resource is freed (that is, the semaphore value plus 1).
The common pseudo code that uses the semaphore access critical area is as follows Figure 1:
The Non-critical and critical sections in Fig. 1 Here is the code, and the understanding of this diagram should be combined with the experiment in the later.
The simplest semaphore can take only 0 and 1 values, which are called two-dimensional semaphores, and in this section, the two-dimensional signal volume is mainly discussed. Two-dimensional signal to learn, it is easier to expand to the use of multidimensional signal.