The previous article outlines the mutex programming of the signal in a Linux system, which is exactly the same as the companion of the previous article----signal volume synchronization. They're sisters because they're all using the kernel's semaphore mechanism to communicate between processes. Because the problems they solve are different, the scenarios they use will differ.
The main problem of the mutual exclusion of semaphores is that the processes need to access a resource at the same time, but their operations on the resource affect each other's operation results, thus requiring a mechanism to enable the process to prevent other processes from accessing the same resources while accessing the resources. The synchronization of semaphores solves another classic problem: The problem of co-operation between producers and consumers.
First describe the producer and consumer issues: process A is responsible for producing products (creating and writing files), Process B is responsible for consuming products (copying files), the ideal process is that when process a creates and writes data to a file, process B takes the file away. However, the uncertainty about the timing of two processes often makes the process confusing, that is, process A's production is unfinished (such as creating a file but not writing data), process B copies the file, causing process B to get an incomplete file. In fact, the problem arises because there is no synchronization mechanism between the two processes.
This problem is solved by the proposed signal volume. The implementation of the semaphore is described in detail in the previous article, which simply points out the difference between the use of the semaphore synchronization process and the mutex, and then gives an example description.
The following is the author implementation of the Code of Mind map:
Unlike mutexes, where the initial value of a semaphore is not assigned a value of 1, it is assigned a value of 0, and no semaphore is obtained in the producer, and no signal is released in the consumer. This is done so that the order in which they are run does not affect the final result.
Can be analyzed: If the producer program runs first, it will create and write to the file sequentially, assuming that the consumer program is running at this time, but the semaphore initial value is 0, so the consumer program is suspended. After the producer program executes and releases the semaphore, the consumer program continues to run and finally gets the correct file.
If the consumer program runs first, it tests that the initial value of the semaphore is 0, so it is suspended until the producer program runs out, so that it can also get the correct file.
It is important to note that in the 1.7 release semaphore, contains an operation to set the initial value to 0, so that in order to run the next two programs, if first run the consumer, the initial value is still 0.
Here is the author's test code:
Producer Code:
#include <sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<sys/ipc.h>#include<sys/sem.h>intMainintargcChar**argv) { intFD =0; key_t key; intSemid; structsembuf ops; //Create a Semaphore collectionKey = Ftok ("/home/application/semapthore",1); Semid=semget (Key,1, ipc_creat); //set the initial value to 0Semctl (Semid,0, Setval,0); //Create a fileFD = open ("./product.txt", o_rdwr| O_creat,0755); //RestSleep -); //Write DataWrite (FD,"Product is finished!", +); //Close FileClose (FD); //release Semaphoreops.sem_num=0; Ops.sem_op=1; OPS.SEM_FLG=Sem_undo; Semop (Semid,&ops,1); Semctl (Semid,0, Setval,0); return 0; }View Code
Consumer Code;
#include <stdlib.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intMainintargcChar**argv) {key_t key; intSemid; structsembuf ops; //Open Semaphore CollectionKey = Ftok ("/home/application/semapthore",1); Semid=semget (Key,1, ipc_creat); //get the SemaphoreOps.sem_num =0; Ops.sem_op= -1; OPS.SEM_FLG=Sem_undo; Semop (Semid,&ops,1); //Consumer DocumentsSystem"CP./product.txt./comsum/"); return 0; }View Code
Simultaneous programming of Linux semaphore volume